这里的mask操作指的是,对一幅图像image,有一个binary的mask图像,image_masked = image & mask
可以采用 Mat的copyTo成员函数,利用mask参数实现。
#include <opencv2/core/core.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 3)
{
cout << argv[0] << " <image> <thresh_val>" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
// convert to gray
Mat img_gray;
cvtColor(image, img_gray, CV_BGR2GRAY);
// threshold to get mask
int threshold_value = atoi(argv[2]);
int max_BINARY_value = 256;