代码 :
#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,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);
}
上面的图像是成对出现的,因为您需要使用相应的“掩码”图像来让 OpenCV 仅将圆形区域复制到宇宙空间图像中。注意遮罩图像的扩展名为“.bmp”。这是因为 bmp 格式为位存储提供了更高的精度。但是OpenCV通过使用“imread(..., 0)”以相同的方式加载它们并没有区别,第二个参数“0”表示要加载的单通道或灰度图像。