在OpenCV中覆盖较大图像中的较小图像

时间:2012-04-03 16:31:50

标签: opencv javacv

我想用Opencv中的图像替换图像的一部分

我用过

cvGetPerspectiveMatrix() with a warpmatrix and using cvAnd() and cvOr()

但无法让它发挥作用

这是当前显示图像的代码和替换图像的白色多边形。我想替换任何尺寸的pic的白色多边形以进行缩放,并用指向的区域替换。

虽然代码在javacv中,但即使发布了c代码,我也可以将其转换为java

grabber.start();
while(isDisp() && (image=grabber.grab())!=null){
  if (dst_corners !=  null) {// corners of the image to be replaced
  CvPoint points = new CvPoint((byte) 0,dst_corners,0,dst_corners.length);
  cvFillConvexPoly(image,points, 4, CvScalar.WHITE, 1, 0);//white polygon covering the replacement image 
  }
correspondFrame.showImage(image);
}

任何对此的指示都会非常有用。

更新

我在这段代码中使用了warpmatrix,我得到了叠加图像的黑点

cvSetImageROI(image, cvRect(x1,y1, overlay.width(), overlay.height()));
CvPoint2D32f p = new CvPoint2D32f(4);
CvPoint2D32f q = new CvPoint2D32f(4);

q.position(0).x(0);
q.position(0).y(0);
q.position(1).x((float) overlay.width());
q.position(1).y(0);
q.position(2).x((float) overlay.width());
q.position(2).y((float) overlay.height());
q.position(3).x(0);
q.position(3).y((float) overlay.height());

p.position(0).x((int)Math.round(dst_corners[0]);
p.position(0).y((int)Math.round(dst_corners[1]));
p.position(1).x((int)Math.round(dst_corners[2]));
p.position(1).y((int)Math.round(dst_corners[3]));
p.position(3).x((int)Math.round(dst_corners[4]));
p.position(3).y((int)Math.round(dst_corners[5]));
p.position(2).x((int)Math.round(dst_corners[6]));
p.position(2).y((int)Math.round(dst_corners[7]));

cvGetPerspectiveTransform(q, p, warp_matrix);

cvWarpPerspective(overlay, image, warp_matrix);

我得到叠加图像的黑点,即使原始图像是具有4个顶点的多边形,叠加图像也被设置为矩形。我相信这是因为投资回报率。任何人都可以告诉我如何适应图像,以及为什么我得到一个黑点而不是叠加图像。

2 个答案:

答案 0 :(得分:0)

一种方法是将pic缩放到白色多边形大小,然后将其复制到抓取的图像设置其感兴趣的区域(这里是解释ROI的link)。 您的代码应如下所示:

resize(pic, resizedImage, resizedImage.size(), 0, 0, interpolation); //resizedImage should have the points size
cvSetImageROI(image, cvRect(the points coordinates));
cvCopy(resizedImage,image);
cvResetImageROI(image);

我希望有所帮助。

祝你好运, 丹尼尔

答案 1 :(得分:0)

我认为cvWarpPerspectivelink)正是您所寻找的。

所以不要做

CvPoint points = new CvPoint((byte) 0,dst_corners,0,dst_corners.length);
cvFillConvexPoly(image,points, 4, CvScalar.WHITE, 1, 0);//white polygon covering the replacement image

尝试

cvWarpPerspective(yourimage, image, M, image.size(), INTER_CUBIC, BORDER_TRANSPARENT);

M是您从cvGetPerspectiveMatrix

获得的矩阵
相关问题