mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
Fix all examples & clean up
This commit is contained in:
parent
da8add8b51
commit
8515b05245
@ -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');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
18
examples/car-detection.js
Normal file
18
examples/car-detection.js
Normal file
@ -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');
|
||||
});
|
||||
});
|
||||
@ -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<cars.length; i++){
|
||||
var x = cars[i];
|
||||
im.rectangle([x.x, x.y], [x.width, x.height]);
|
||||
}
|
||||
|
||||
im.save('./cars.jpg');
|
||||
});
|
||||
});
|
||||
14
examples/color-filter.js
Normal file
14
examples/color-filter.js
Normal file
@ -0,0 +1,14 @@
|
||||
var cv = require('../lib/opencv');
|
||||
|
||||
// (B)lue, (G)reen, (R)ed
|
||||
var lower_threshold = [46, 57, 83];
|
||||
var upper_threshold = [80, 96, 115];
|
||||
|
||||
cv.readImage('./files/coin1.jpg', function(err, im) {
|
||||
if (err) throw err;
|
||||
if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size');
|
||||
|
||||
im.inRange(lower_threshold, upper_threshold);
|
||||
im.save('./tmp/coin_detected.jpg');
|
||||
console.log('Image saved to ./tmp/coin_detected.jpg');
|
||||
});
|
||||
@ -1,12 +0,0 @@
|
||||
var cv = require('../lib/opencv');
|
||||
|
||||
// (B)lue, (G)reen, (R)ed
|
||||
var lower_threshold = [46, 57, 83];
|
||||
var upper_threshold = [80, 96, 115];
|
||||
|
||||
cv.readImage('./coin1.jpg', function(err, im) {
|
||||
|
||||
im.inRange(lower_threshold, upper_threshold);
|
||||
im.save('./coin_detected.jpg');
|
||||
|
||||
});
|
||||
@ -5,37 +5,39 @@ var highThresh = 100;
|
||||
var nIters = 2;
|
||||
var maxArea = 2500;
|
||||
|
||||
var GREEN = [0, 255, 0]; //B, G, R
|
||||
var WHITE = [255, 255, 255]; //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 RED = [0, 0, 255]; // B, G, R
|
||||
|
||||
cv.readImage('./stuff.png', function(err, im) {
|
||||
cv.readImage('./files/stuff.png', function(err, im) {
|
||||
if (err) throw err;
|
||||
if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size');
|
||||
|
||||
var big = new cv.Matrix(im.height(), im.width());
|
||||
var all = new cv.Matrix(im.height(), im.width());
|
||||
var big = new cv.Matrix(im.height(), im.width());
|
||||
var all = new cv.Matrix(im.height(), im.width());
|
||||
|
||||
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++) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
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');
|
||||
});
|
||||
|
||||
24
examples/convert-image.js
Executable file
24
examples/convert-image.js
Executable file
@ -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');
|
||||
});
|
||||
@ -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");
|
||||
});
|
||||
|
||||
@ -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');
|
||||
});
|
||||
|
||||
22
examples/face-detection-rectangle.js
Executable file
22
examples/face-detection-rectangle.js
Executable file
@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
18
examples/face-detection.js
Executable file
18
examples/face-detection.js
Executable file
@ -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');
|
||||
});
|
||||
});
|
||||
@ -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")
|
||||
im.detectObject('../data/haarcascade_frontalface_alt.xml', {}, function(err, faces) {
|
||||
if (err) return resp.end(err.stack);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
resp.writeHead(200, {'Content-Type': 'image/jpeg'});
|
||||
resp.end(im.toBuffer());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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<faces.length; i++){
|
||||
var x = faces[i]
|
||||
im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
|
||||
}
|
||||
|
||||
//console.log(faces);
|
||||
resp.writeHead(200, {'Content-Type': 'image/jpeg'});
|
||||
resp.end(im.toBuffer());
|
||||
});
|
||||
});
|
||||
})
|
||||
} else {
|
||||
request({uri:url || 'http://google.com'}).pipe(resp)
|
||||
}
|
||||
}).listen(1901)
|
||||
|
||||
}).listen(3000, function(){ console.log('Listening on http://localhost:3000'); })
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
var cv = require('../lib/opencv')
|
||||
, assert = require('assert')
|
||||
, fs =require('fs')
|
||||
|
||||
|
||||
//console.log(cv.version)
|
||||
cv.readImage("./mona.png", function(err, im){
|
||||
|
||||
im.detectObject("./haarcascade_frontalface_alt.xml", {}, function(err, faces){
|
||||
|
||||
for (var i=0;i<faces.length; i++){
|
||||
var x = faces[i];
|
||||
im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
|
||||
}
|
||||
|
||||
im.save('./out.png');
|
||||
});
|
||||
});
|
||||
@ -2,19 +2,21 @@ var path = require('path'),
|
||||
cv = require('../lib/opencv');
|
||||
|
||||
// When opening a file, the full path must be passed to opencv
|
||||
var vid = new cv.VideoCapture(path.join(__dirname, "motion.mov"));
|
||||
var vid = new cv.VideoCapture(path.join(__dirname, 'files', 'motion.mov'));
|
||||
|
||||
vid.read(function(err, mat){
|
||||
var track = new cv.TrackedObject(mat, [420, 110, 490, 170], {channel: "value"});
|
||||
if (err) throw err;
|
||||
|
||||
var track = new cv.TrackedObject(mat, [420, 110, 490, 170], {channel: 'value'});
|
||||
var x = 0;
|
||||
var iter = function(){
|
||||
vid.read(function(err, m2){
|
||||
x++;
|
||||
var rec = track.track(m2)
|
||||
console.log(">>", 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();
|
||||
})
|
||||
|
||||
|
||||
@ -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');
|
||||
});
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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');
|
||||
})
|
||||
});
|
||||
|
||||
16
examples/warp-image.js
Normal file
16
examples/warp-image.js
Normal file
@ -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');
|
||||
});
|
||||
@ -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");
|
||||
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user