1. 程式人生 > >opencv掩模操作

opencv掩模操作

logs rec class div copyto set span return col

 1     //設置變量,讀圖
 2     Mat image, mask;
 3     Mat img1, img2, img3, img4;
 4     image = imread("data/img/1.jpg");
 5 
 6     //設置掩模
 7     mask = Mat::zeros(image.size(), CV_8UC1);
 8     Rect r1(100, 100, 250, 300);
 9     mask(r1).setTo(255);
10 
11     //取img123及mask
12     img1 = image(r1);//取image中r1區域,“摳出來”
13 image.copyTo(img2, mask);//把image復制進預定掩模,進行與 14 image.copyTo(img3); 15 img3.setTo(0, mask);//把掩img3在模中1中的區域置0,“摳出去” 16 17 //顯示 18 imshow("Image sequence", image); 19 imshow("img1", img1); 20 imshow("img2", img2); 21 imshow("img3", img3); 22 imshow("mask", mask); 23 24
waitKey(); 25 return 0;

opencv掩模操作