如何解决如何在opencv C++中使用相应的掩码复制图像?
代码:
#include <iostream>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
// Mat faceImage = imread("/Users/softech66/Downloads/images 2/unvese_space.png");
// Mat glassBGR = imread("/Users/softech66/Downloads/images 2/sun.png");
// /Users/softech66/Downloads/images 2/sun_mask.bmp
// resize(glassBGR,glassBGR,Size(300,100));
// check each pixel of glass and if its white(255,255,255) then change it with face image pixels
Mat img2 = imread("/Users/softech66/Downloads/images 2/sun.png",0);
Mat img4 = imread("/Users/softech66/Downloads/images 2/univese_space.png",0);
Mat img3 = imread("/Users/softech66/Downloads/images 2/sun_mask.bmp",0);
Mat img;
cvtColor(imread("/Users/softech66/Downloads/images 2/sun.png",IMREAD_COLOR),img,cv::COLOR_RGB2RGBA);
Mat mask = imread("/Users/softech66/Downloads/images 2/sun_mask.bmp",IMREAD_GRAYSCALE);
for(int r = 0; r < img.rows; r++){
for(int c = 0; c < img.cols; c++){
uchar alpha = 0;
if(r < mask.rows && c < mask.cols)
alpha = mask.at<uchar>(r,c);
img.at<Vec4b>(r,c)[3] = alpha;
}
}
imwrite("result.png",img);
Mat roi=img4(Rect(0,img.cols,img2.rows));
Mat mask1(roi.rows,roi.cols,roi.depth(),Scalar(1));
img.copyTo(roi,mask1);
// img4.copyTo(img);
imshow("img3",img4);
// imshow("img33",img4);
waitKey(0);
}
上面的图片是成对出现的,因为你需要使用对应的“mask”图片让OpenCV只将圆形区域复制到宇宙空间图片中。注意遮罩图像,扩展名为“.bmp”。这是因为 bmp 格式为位存储提供了更高的精度。但是OpenCV通过使用“imread(...,0)”以同样的方式加载它们并没有区别,第二个参数“0”表示要加载的单通道或灰度图像。
Moon image Earth image Sun image
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。