diff --git a/examples/Face.js b/examples/Face.js deleted file mode 100755 index 3582303..0000000 --- a/examples/Face.js +++ /dev/null @@ -1,20 +0,0 @@ -var cv = require('../lib/opencv'); - -var COLOR = [0, 255, 0]; //default red -var thickness = 2; // default 1 - -cv.readImage('./mona.png', function(err, im) { - - im.detectObject('../data/haarcascade_frontalface_alt2.xml', {}, function(err, faces) { - - for(var k = 0; k < faces.length; k++) { - - face = faces[k]; - im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], COLOR, 2); - } - - im.save('/tmp/salida.png'); - - }); - -}); diff --git a/examples/addweighted.js b/examples/addweighted.js index 9f0743d..dc9379f 100755 --- a/examples/addweighted.js +++ b/examples/addweighted.js @@ -1,16 +1,14 @@ var cv = require('../lib/opencv'); +cv.readImage("./files/mona.png", function(err, orig) { + if (err) throw err; -cv.readImage("./mona.png", function(err, orig) { - - cv.readImage("./over_text.png", function(err, over_text) { - - var result = new cv.Matrix(orig.width(), orig.height()); - - result.addWeighted(orig, 0.7, over_text, 0.9); - result.save("/tmp/weighted.png"); - }); + cv.readImage("./files/over_text.png", function(err, over_text) { + if (err) throw err; + var result = new cv.Matrix(orig.width(), orig.height()); + result.addWeighted(orig, 0.7, over_text, 0.9); + result.save("./tmp/weighted.png"); + console.log('Image saved to ./tmp/weighted.png'); + }); }); - - diff --git a/examples/camera.js b/examples/camera.js index 45cf27e..abe885b 100755 --- a/examples/camera.js +++ b/examples/camera.js @@ -1,13 +1,12 @@ var cv = require('../lib/opencv'); var camera = new cv.VideoCapture(0); - +var window = new cv.NamedWindow('Video', 0) setInterval(function() { - - camera.read(function(err, im) { - - //im.save('/tmp/cam.png'); - }); - -}, 1000); + camera.read(function(err, im) { + if (err) throw err; + window.show(im); + window.blockingWaitKey(0, 50); + }); +}, 20); diff --git a/examples/car-detection.js b/examples/car-detection.js new file mode 100644 index 0000000..58c2d61 --- /dev/null +++ b/examples/car-detection.js @@ -0,0 +1,18 @@ +var cv = require('../lib/opencv'); + +cv.readImage("./files/car1.jpg", function(err, im){ + if (err) throw err; + if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); + + im.detectObject("../data/hogcascade_cars_sideview.xml", {}, function(err, cars){ + if (err) throw err; + + for (var i=0; i < cars.length; i++){ + var x = cars[i]; + im.rectangle([x.x, x.y], [x.width, x.height]); + } + + im.save('./tmp/car-detection.jpg'); + console.log('Image saved to ./tmp/car-detection.jpg'); + }); +}); diff --git a/examples/car_detection.js b/examples/car_detection.js deleted file mode 100644 index ea56311..0000000 --- a/examples/car_detection.js +++ /dev/null @@ -1,16 +0,0 @@ -var cv = require('../lib/opencv') - , assert = require('assert') - , fs =require('fs') - - cv.readImage("./car1.jpg", function(err, im){ - - im.detectObject("../data/hogcascade_cars_sideview.xml", {}, function(err, cars){ - - for (var i=0;i maxArea) { - var moments = contours.moments(i); - var cgx = Math.round(moments.m10/moments.m00); - var cgy = Math.round(moments.m01/moments.m00); - big.drawContour(contours, i, GREEN); - big.line([cgx - 5, cgy], [cgx + 5, cgy], RED); - big.line([cgx, cgy - 5], [cgx, cgy + 5], RED); - } - } + for(i = 0; i < contours.size(); i++) { + if(contours.area(i) > maxArea) { + var moments = contours.moments(i); + var cgx = Math.round(moments.m10 / moments.m00); + var cgy = Math.round(moments.m01 / moments.m00); + big.drawContour(contours, i, GREEN); + big.line([cgx - 5, cgy], [cgx + 5, cgy], RED); + big.line([cgx, cgy - 5], [cgx, cgy + 5], RED); + } + } - all.drawAllContours(contours, WHITE); + all.drawAllContours(contours, WHITE); - - big.save('./big.png'); - all.save('./all.png'); + big.save('./tmp/big.png'); + all.save('./tmp/all.png'); + console.log('Image saved to ./tmp/big.png && ./tmp/all.png'); }); diff --git a/examples/convert-image.js b/examples/convert-image.js new file mode 100755 index 0000000..f7bdaeb --- /dev/null +++ b/examples/convert-image.js @@ -0,0 +1,24 @@ +var cv = require('../lib/opencv'); + +cv.readImage('./files/mona.png', function(err, im) { + if (err) throw err; + if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); + + img_hsv = im.copy(); + img_gray = im.copy(); + + + img_hsv.convertHSVscale(); + img_gray.convertGrayscale(); + + console.log(img_gray.pixel(100,100)); + + im.save('./tmp/nor.png'); + img_hsv.save('./tmp/hsv.png'); + img_gray.save('./tmp/gray.png'); + + img_crop = im.crop(50,50,250,250); + img_crop.save('./tmp/crop.png'); + + console.log('Image saved to ./tmp/{crop|nor|hsv|gray}.png'); +}); diff --git a/examples/convert_image.js b/examples/convert_image.js deleted file mode 100755 index db6d31a..0000000 --- a/examples/convert_image.js +++ /dev/null @@ -1,24 +0,0 @@ -var cv = require('../lib/opencv'); - - -cv.readImage("./mona.png", function(err, im) { - - img_hsv = im.copy(); - img_gray = im.copy(); - - - img_hsv.convertHSVscale(); - img_gray.convertGrayscale(); - - console.log(img_gray.pixel(100,100)); - - im.save("/tmp/nor.png"); - img_hsv.save("/tmp/hsv.png"); - img_gray.save("/tmp/gray.png"); - - img_crop = im.crop(50,50,250,250); - img_crop.save("crop.png"); - - console.log("Guardado"); -}); - diff --git a/examples/detect-shapes.js b/examples/detect-shapes.js index 1fa49c2..f465d23 100755 --- a/examples/detect-shapes.js +++ b/examples/detect-shapes.js @@ -1,9 +1,4 @@ -#!/usr/bin/env node - -// // Detects triangles and quadrilaterals -// - var cv = require('../lib/opencv'); var lowThresh = 0; @@ -11,42 +6,46 @@ var highThresh = 100; var nIters = 2; var minArea = 2000; -var BLUE = [0, 255, 0]; //B, G, R -var RED = [0, 0, 255]; //B, G, R -var GREEN = [0, 255, 0]; //B, G, R -var WHITE = [255, 255, 255]; //B, G, R +var BLUE = [0, 255, 0]; // B, G, R +var RED = [0, 0, 255]; // B, G, R +var GREEN = [0, 255, 0]; // B, G, R +var WHITE = [255, 255, 255]; // B, G, R -cv.readImage('./shapes.jpg', function(err, im) { +cv.readImage('./files/shapes.jpg', function(err, im) { + if (err) throw err; - var out = new cv.Matrix(im.height(), im.width()); + width = im.width() + height = im.height() + if (width < 1 || height < 1) throw new Error('Image has no size'); - im.convertGrayscale(); - im_canny = im.copy(); + var out = new cv.Matrix(height, width); + im.convertGrayscale(); + im_canny = im.copy(); + im_canny.canny(lowThresh, highThresh); + im_canny.dilate(nIters); - im_canny.canny(lowThresh, highThresh); - im_canny.dilate(nIters); + contours = im_canny.findContours(); - contours = im_canny.findContours(); + for (i = 0; i < contours.size(); i++) { - for(i = 0; i < contours.size(); i++) { + if (contours.area(i) < minArea) continue; - if(contours.area(i) < minArea) continue; + var arcLength = contours.arcLength(i, true); + contours.approxPolyDP(i, 0.01 * arcLength, true); - var arcLength = contours.arcLength(i, true); - contours.approxPolyDP(i, 0.01 * arcLength, true); + switch(contours.cornerCount(i)) { + case 3: + out.drawContour(contours, i, GREEN); + break; + case 4: + out.drawContour(contours, i, RED); + break; + default: + out.drawContour(contours, i, WHITE); + } + } - switch(contours.cornerCount(i)) { - case 3: - out.drawContour(contours, i, GREEN); - break; - case 4: - out.drawContour(contours, i, RED); - break; - default: - out.drawContour(contours, i, WHITE); - } - } - - out.save('./out.png'); + out.save('./tmp/detect-shapes.png'); + console.log('Image saved to ./tmp/detect-shapes.png'); }); diff --git a/examples/face-detection-rectangle.js b/examples/face-detection-rectangle.js new file mode 100755 index 0000000..f108eee --- /dev/null +++ b/examples/face-detection-rectangle.js @@ -0,0 +1,22 @@ +var cv = require('../lib/opencv'); + +var COLOR = [0, 255, 0]; // default red +var thickness = 2; // default 1 + +cv.readImage('./files/mona.png', function(err, im) { + if (err) throw err; + if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); + + im.detectObject('../data/haarcascade_frontalface_alt2.xml', {}, function(err, faces) { + if (err) throw err; + + for (var i = 0; i < faces.length; i++) { + face = faces[i]; + im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], COLOR, 2); + } + + im.save('./tmp/face-detection-rectangle.png'); + console.log('Image saved to ./tmp/face-detection-rectangle.png'); + }); + +}); diff --git a/examples/face-detection.js b/examples/face-detection.js new file mode 100755 index 0000000..0cc5fee --- /dev/null +++ b/examples/face-detection.js @@ -0,0 +1,18 @@ +var cv = require('../lib/opencv'); + +cv.readImage("./files/mona.png", function(err, im){ + if (err) throw err; + if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); + + im.detectObject("../data/haarcascade_frontalface_alt.xml", {}, function(err, faces){ + if (err) throw err; + + for (var i = 0; i < faces.length; i++){ + var face = faces[i]; + im.ellipse(face.x + face.width / 2, face.y + face.height / 2, face.width / 2, face.height / 2); + } + + im.save('./tmp/face-detection.png'); + console.log('Image saved to ./tmp/face-detection.png'); + }); +}); diff --git a/examples/face-proxy.js b/examples/face-proxy.js index 93c04c1..541ba7b 100755 --- a/examples/face-proxy.js +++ b/examples/face-proxy.js @@ -1,46 +1,30 @@ -/* +// Face recognition proxy +var http = require('http'), + request = require('request'), + cv = require('../lib/opencv'); -Face recognition proxy +http.createServer(function(req, resp){ + var url = req.url.slice(1); + request({uri:url, encoding:'binary'}, function(err, r, body){ + if (err) return resp.end(err.stack); + if (!/image\//.test(r.headers['content-type'])) return resp.end('Not an image'); -*/ + cv.readImage(new Buffer(body, 'binary'), function(err, im){ + if (err) return resp.end(err.stack); + if (im.width() < 1 || im.height() < 1) return resp.end('Image has no size'); -var http = require('http') - , request = require('request') - , cv = require('../lib/opencv') - , face_cascade = new cv.CascadeClassifier("./data/haarcascade_frontalface_alt.xml") - - - - http.createServer(function(req, resp){ - var url = req.url.slice(1); - console.log(url); - - if (url.indexOf('http') != 0){ - return request({uri:'http://google.com'}).pipe(resp) - } + im.detectObject('../data/haarcascade_frontalface_alt.xml', {}, function(err, faces) { + if (err) return resp.end(err.stack); - // TODO make sure image - if (url.indexOf(".jpg", url.length - 4) !== -1 || - url.indexOf(".png", url.length - 4) !== -1){ - - request({uri:url, encoding:'binary'}, function(err, r, body){ - if (err) throw err; - - cv.readImage(new Buffer(body, 'binary'), function(err, im){ - im.faceDetect(im, {}, function(err, faces){ - for (var i=0;i>", x, ":" , rec) + console.log('>>', x, ':' , rec) if (x % 10 == 0){ m2.rectangle([rec[0], rec[1]], [rec[2], rec[3]]) - // m2.save('./out-motiontrack-' + x + '.jpg') + // m2.save('./out-motiontrack-' + x + '.jpg') } if (x<100) iter(); @@ -22,4 +24,3 @@ vid.read(function(err, mat){ } iter(); }) - diff --git a/examples/quad-crosses.js b/examples/quad-crosses.js index 3632f9f..341631f 100755 --- a/examples/quad-crosses.js +++ b/examples/quad-crosses.js @@ -1,9 +1,4 @@ -#!/usr/bin/env node - -// // Finds quadrilaterals and fills them with an X -// - var cv = require('../lib/opencv'); var lowThresh = 0; @@ -17,39 +12,41 @@ var RED = [0, 0, 255]; //B, G, R var GREEN = [0, 255, 0]; //B, G, R var WHITE = [255, 255, 255]; //B, G, R +cv.readImage('./files/quads.jpg', function(err, im) { + if (err) throw err; + if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); -cv.readImage('./quads.jpg', function(err, im) { + var out = im.copy(); - var out = im.copy(); + im.convertGrayscale(); + im_canny = im.copy(); - im.convertGrayscale(); - im_canny = im.copy(); + im_canny.canny(lowThresh, highThresh); + im_canny.dilate(nIters); - im_canny.canny(lowThresh, highThresh); - im_canny.dilate(nIters); + contours = im_canny.findContours(); - contours = im_canny.findContours(); + for (i = 0; i < contours.size(); i++) { - for(i = 0; i < contours.size(); i++) { + var area = contours.area(i); + if (area < minArea || area > maxArea) continue; - var area = contours.area(i); - if(area < minArea || area > maxArea) continue; + var arcLength = contours.arcLength(i, true); + contours.approxPolyDP(i, 0.01 * arcLength, true); - var arcLength = contours.arcLength(i, true); - contours.approxPolyDP(i, 0.01 * arcLength, true); + if (contours.cornerCount(i) != 4) continue; - if(contours.cornerCount(i) != 4) continue; + var points = [ + contours.point(i, 0), + contours.point(i, 1), + contours.point(i, 2), + contours.point(i, 3) + ] - var points = [ - contours.point(i, 0), - contours.point(i, 1), - contours.point(i, 2), - contours.point(i, 3) - ] + out.line([points[0].x,points[0].y], [points[2].x, points[2].y], RED); + out.line([points[1].x,points[1].y], [points[3].x, points[3].y], RED); + } - out.line([points[0].x,points[0].y], [points[2].x, points[2].y], RED); - out.line([points[1].x,points[1].y], [points[3].x, points[3].y], RED); - } - - out.save('./out.png'); + out.save('./tmp/quad-crosses.png'); + console.log('Image saved to ./tmp/quad-crosses.png'); }); diff --git a/examples/salt.js b/examples/salt.js index 9c22b6e..49f19b5 100755 --- a/examples/salt.js +++ b/examples/salt.js @@ -1,27 +1,17 @@ var cv = require('../lib/opencv'); - -cv.readImage("./mona.png", function(err, im) { - salt(im, 3000); - im.save("/tmp/salt.png"); +cv.readImage("./files/mona.png", function(err, im) { + salt(im, 3000); + im.save("./tmp/salt.png"); + console.log('Image saved to ./tmp/salt.png'); }); - - function salt(img, n) { + if ((var channels img.channels()) != 3) return console.log('Image has only %s Channel. It\'s not possible to salt this image.', channels) - - if (img.channels() == 1) { - - console.log("1 Canales"); - } else if (img.channels() == 3) { - - for(k = 0; k < n; k ++) { - i = Math.random() * img.width(); - j = Math.random() * img.height(); - - img.set(j, i, 255); - } - } - + for(var i = 0; i < n; i ++) { + x = Math.random() * img.width(); + y = Math.random() * img.height(); + img.set(y, x, 255); + } } diff --git a/examples/take-face-pics.js b/examples/take-face-pics.js index 7f6efa2..418809b 100644 --- a/examples/take-face-pics.js +++ b/examples/take-face-pics.js @@ -1,31 +1,24 @@ -var cv = require('../lib/opencv') +var cv = require('../lib/opencv'); +var vid = new cv.VideoCapture(0); -var vid = new cv.VideoCapture(0) +vid.read(function(err, im){ + if (err) throw err; + im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){ + if (err) throw err; + if (!faces.length) return console.log("No Faces"); -var snap = function(){ - vid.read(function(err, im){ - im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){ - - if (!faces){ - console.log("No Faces") - return; - } - var face = faces[0] - , ims = im.size() - - var im2 = im.roi(face.x, face.y, face.width, face.height) - /* - im.adjustROI( - -face.y - , (face.y + face.height) - ims[0] - , -face.x - , (face.x + face.width) - ims[1]) - */ - im2.save('out.jpg') - }) - - - }); -} -snap() + var face = faces[0]; + var ims = im.size(); + var im2 = im.roi(face.x, face.y, face.width, face.height) + /* + im.adjustROI( + -face.y + , (face.y + face.height) - ims[0] + , -face.x + , (face.x + face.width) - ims[1]) + */ + im2.save('./tmp/take-face-pics.jpg') + console.log('Image saved to ./tmp/take-face-pics.jpg'); + }) +}); diff --git a/examples/warp-image.js b/examples/warp-image.js new file mode 100644 index 0000000..9f4dda2 --- /dev/null +++ b/examples/warp-image.js @@ -0,0 +1,16 @@ +var cv = require('../lib/opencv'); + +cv.readImage("./mona.png", function(err, im) { + if (err) throw err; + + width = im.width(); + height = im.height(); + if (width < 1 || height < 1) throw new Error('Image has no size'); + + var srcArray = [0, 0, width, 0, width, height, 0, height]; + var dstArray = [0, 0, width * 0.9, height * 0.1, width, height, width * 0.2, height * 0.8]; + var xfrmMat = im.getPerspectiveTransform(srcArray, dstArray); + im.warpPerspective(xfrmMat, width, height, [255, 255, 255]); + im.save("./warp-image.png"); + console.log('Image saved to ./tmp/warp-image.png'); +}); diff --git a/examples/warp_image.js b/examples/warp_image.js deleted file mode 100644 index 9b11178..0000000 --- a/examples/warp_image.js +++ /dev/null @@ -1,23 +0,0 @@ -var cv = require('../lib/opencv'); - -cv.readImage("./mona.png", function(err, im) { - - var srcArray = [ - 0,0, - im.width(),0, - im.width(),im.height(), - 0,im.height()]; - var dstArray = [ - 0,0, - im.width()*.9,im.height()*.1, - im.width(),im.height(), - im.width()*.2,im.height()*.8]; - - var xfrmMat = im.getPerspectiveTransform(srcArray,dstArray); - img_warp = im.copy(); - img_warp.warpPerspective(xfrmMat,im.width,im.height,[255,255,255]); - - img_warp.save("/tmp/mona_warp.png"); - -}); -