如何将两个图像中像素都为255的在一个新的掩膜中显示c++
时间: 2024-03-03 17:53:06 浏览: 26
你可以使用OpenCV库来实现将两个图像中像素都为255的在一个新的掩膜中显示的功能。下面是一个c++代码示例:
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
// Load two grayscale images
Mat img1 = imread("image1.png", IMREAD_GRAYSCALE);
Mat img2 = imread("image2.png", IMREAD_GRAYSCALE);
// Create a new blank image of the same size
Mat mask(img2.rows, img2.cols, CV_8UC1, Scalar(0));
// Loop through each pixel and set the value to 255 if both images have a value of 255
for (int y = 0; y < img1.rows; y++) {
for (int x = 0; x < img1.cols; x++) {
if (img1.at<uchar>(y, x) == 255 && img2.at<uchar>(y, x) == 255) {
mask.at<uchar>(y, x) = 255;
}
}
}
// Save the new mask image
imwrite("mask.png", mask);
return 0;
}
```
在这个示例中,我们使用`Mat`类来加载和创建图像,并使用`for`循环遍历每个像素。`at<uchar>(y, x)`函数用于获取图像中指定位置的像素值,并使用`Scalar`类创建一个表示像素值的8位无符号整数(`CV_8UC1`)。最后,我们使用`imwrite`函数将新掩膜图像保存到磁盘上。