opencv算术运算:bitwise_and()

 用bitwise_and裁剪一幅图的一部分:

#include
#include
using namespace std;
using namespace cv;
int main()
{
	
	 const char* imagename = "C://Users//huashuo111//Desktop//test2.bmp";
     //从文件中读入图像
     Mat img = imread(imagename,IMREAD_GRAYSCALE);
     //如果读入图像失败
     if(img.empty())
     {
         fprintf(stderr, "Can not load image %s\n", imagename);
         return-1;
     }
	// resize(img,img,Size(),0.5,0.5);
	 Mat mask(img.rows,img.cols,CV_8UC1,Scalar(0,0,0));
	 circle(mask,Point(mask.rows/2,mask.cols/2),150,CV_RGB(255,255,255),-1);
	
	 Mat r;
	 const uchar white=255;
	
	 bitwise_and(img,mask,r);
	 for(int i=0;i(i,j))
				 r.at(i,j)=white;
		 }

	imshow("img",img);
	imshow("mask",mask);
	imshow("result",r);

	
	

	
	
     //此函数等待按键,按键盘任意键就返回
     waitKey();
     return 0;
}
原图:

opencv算术运算:bitwise_and()_第1张图片


掩码:

opencv算术运算:bitwise_and()_第2张图片


裁剪后:

opencv算术运算:bitwise_and()_第3张图片

你可能感兴趣的:(opencv)