在OpenCV中下移图像的一部分(剪切和粘贴) [英] Shift part of an image down in opencv (cut and paste)

查看:2539
本文介绍了在OpenCV中下移图像的一部分(剪切和粘贴)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部分图像,我想将其下移25像素,如图所示。





我有掩码想下移。总图像大小不应更改。所以,操作是剪切和粘贴。

平台是C ++

解决方案

创建两个子图像:您要移动的模式周围的第一个 sub1 ,第二个 sub2 ,大小小于 sub1 。将 sub1 复制到 sub2 中,然后用黑色或白色填充 sub1 颜色。显然你需要使用裁剪的尺寸大于两个子图像的掩模。



这是伪代码,用来说明这个想法:

  cv:Mat YourImage; 
cv :: Mat YourMask;
std :: vector< cv :: Point> YourPatternContour;

cv :: Rect sub1ROI = cv :: boundingRect(YourPatternContour);
cv :: Mat sub1 = YourImage(sub1ROI);
cv :: Mat mask = YourMask(sub1ROI);

cv :: Rect sub2ROI = sub1ROI + cv :: Point(25,25);
cv :: Mat sub2 = YourImage(sub2ROI);

sub1.copyTo(sub2,mask);
sub1.set(0,mask);


I have a part of image and I want to shift it down say by 25 pixels like shown in the picture.

I have mask of the region I want to shift down. The total image size should not change. So, the operation is cut and paste. The region where I cut can be filled with 0 or 255.

Platform is C++

解决方案

Create two sub-images: the first sub1 around the pattern you want to move and the second sub2, same size than sub1, around the destination. Copy sub1 into sub2 then fill sub1 with black or white color. You need obviously to use the mask cropped at the same size than the two sub-images.

This is pseudo-code, to illustrate the idea:

cv::Mat YourImage;
cv::Mat YourMask;
std::vector<cv::Point> YourPatternContour;

cv::Rect sub1ROI = cv::boundingRect(YourPatternContour);
cv::Mat sub1 = YourImage(sub1ROI);
cv::Mat mask = YourMask(sub1ROI);

cv::Rect sub2ROI = sub1ROI + cv::Point(25,25);
cv::Mat sub2 = YourImage(sub2ROI);

sub1.copyTo(sub2, mask);
sub1.set(0, mask);

这篇关于在OpenCV中下移图像的一部分(剪切和粘贴)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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