2017-06-19 46 views
1

这听起来很简单,但它非常棘手。我使用NodeJS OpenCV从图片中识别出脸部的X和Y.Nodejs 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'); 
    }); 
}); 

我不想一个椭圆添加到图像,我想补充的图像ontop的人的脸。我该怎么做呢?

使用OpenCV,我试着创建一个缓冲区矩阵并合并来自2张图片的数据。这真的很复杂。我现在正在研究Node Canvas,但我会认为OpenCV会为此提供一个简单的解决方案。任何人都有一些示例代码可以做到吗?

感谢

回答

0

可以实现,使用功能CopyTo从()当前在节点的OpenCV提供,仅仅作为参数指示目标图像与要应用ROI(感兴趣区域)第二个图像。

NAN_METHOD(Matrix::CopyTo) { 
Nan::HandleScope scope; 

Matrix * self = Nan::ObjectWrap::Unwrap<Matrix>(info.This()); 
int width = self->mat.size().width; 
int height = self->mat.size().height; 

// param 0 - destination image: 
Matrix *dest = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject()); 
// param 1 - x coord of the destination 
int x = info[1]->IntegerValue(); 
// param 2 - y coord of the destination 
int y = info[2]->IntegerValue(); 

cv::Mat dstROI = cv::Mat(dest->mat, cv::Rect(x, y, width, height)); 
self->mat.copyTo(dstROI); 

return;} 

我强烈建议你看看源代码,因为这个库没有记录。

它应该工作

overlapImage.copyTo(sourceImage, x, y, width, height);