在OpenCV中将较小的图像覆盖在较大的图像中 [英] Overlay smaller image in a larger image in OpenCV

查看:273
本文介绍了在OpenCV中将较小的图像覆盖在较大的图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to replace a part of the image with my image in Opencv

我用过

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

但无法正常工作

这是当前显示图像和替换图像的白色多边形的代码.我想用任何尺寸的图片替换图片的白色多边形,并用指向的区域替换.

This is the code that is currently displaying the image and a white polygon for the replacement image. I would like to replace the white polygon for a pic with any dimension to be scaled and replaced with the region pointed.

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

While the code is in javacv I could convert it to java even if c code is posted

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);
}

任何指向此操作的指针都将非常有帮助.

Any pointers to this will be very helpful.

更新:

我在此代码中使用了warpmatrix,并且覆盖图像出现黑点

I used warpmatrix with this code and I get a black spot for the overlay image

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个顶点的多边形,叠加图像也被设置为矩形.我相信这是因为投资回报率.谁能告诉我如何按原样放置图像,以及为什么我会出现黑点而不是重叠图像.

I get a black spot for the overlay image and even though the original image is a polygon with 4 vertices the overlay image is set as a rectangle. I believe this is because of the ROI. Could anyone please tell me how to fit the image as is and also why I am getting a black spot instead of the overlay image.

推荐答案

我认为cvWarpPerspective(所以不要这样做

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

这篇关于在OpenCV中将较小的图像覆盖在较大的图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆