opencv掩模操作
 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;

 

posted on 2017-09-08 13:23  Stephenatalie  阅读(1351)  评论(0编辑  收藏  举报