图像修复-生成掩模图像(RGB三通道)

#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

int main()
{
    Mat image,  result;
    image = imread("2.png", 1);
    Mat mask(image.size(), image.type(), Scalar(0,0,0));
    int w = image.cols;  //返回图片的宽度
    int h = image.rows;  //返回图片的高度
    for (int i = 0; i < h - 1; i++)
    {
        for (int j = 0; j < w - 1; j++)
        {
            if (image.at<cv::Vec3b>(i, j)[0] == 255 && image.at<cv::Vec3b>(i, j)[1] == 255 && image.at<cv::Vec3b>(i, j)[2] == 255)
            {
                mask.at<cv::Vec3b>(i, j)[0] = 255;
                mask.at<cv::Vec3b>(i, j)[1] = 255;
                mask.at<cv::Vec3b>(i, j)[2] = 255;
            }
        }
    }
    namedWindow("1", WINDOW_AUTOSIZE);
    imshow("1", mask);
    imwrite("1.png", mask);


    waitKey(0);
    return 0;
}
posted @ 2021-02-21 23:27  司砚章  阅读(130)  评论(0编辑  收藏  举报