1 #include<opencv2/opencv.hpp>
2 #include<iostream>
3 using namespace std;
4 using namespace cv;
5 int main()
6 {
7
8 const char* imagename = "C://Users//huashuo111//Desktop//test2.bmp";
9 //从文件中读入图像
10 Mat img = imread(imagename,IMREAD_GRAYSCALE);
11 //如果读入图像失败
12 if(img.empty())
13 {
14 fprintf(stderr, "Can not load image %s\n", imagename);
15 return-1;
16 }
17 // resize(img,img,Size(),0.5,0.5);
18 Mat mask(img.rows,img.cols,CV_8UC1,Scalar(0,0,0));
19 circle(mask,Point(mask.rows/2,mask.cols/2),150,CV_RGB(255,255,255),-1);
20
21 Mat r;
22 const uchar white=255;
23
24 bitwise_and(img,mask,r);
25 for(int i=0;i<r.rows;i++)
26 for(int j=0;j<r.cols;j++)
27 {
28 if(!mask.at<uchar>(i,j))
29 r.at<uchar>(i,j)=white;
30 }
31
32 imshow("img",img);
33 imshow("mask",mask);
34 imshow("result",r);
35
36 //此函数等待按键,按键盘任意键就返回
37 waitKey();
38 return 0;
39 }
原图:
掩码:
裁剪后: