During image processing, we often need to operate on the specified region or target. This region is called the region of interest. In the initial stage of learning opencv, you must master the operation methods for areas of interest.
For example:
We get an IMG image with two targets, one blue block and one red block. We usually encounter the following three situations:
(1) We want to extract the blue target from IMG and save it as an image;
(2) We want to hide other targets in IMG that are not blue targets, show only blue targets, or only process images in the area of blue targets.
(3) We only want to process the red target area in IMG.
Let's assume that the blue target area is in the rectangle frame R. The program code is as follows:
Mat image, mask; rect r1 (100,100, 50,100); MAT img1, img2, img3, img4; For (;) {sequence> image; mask = mat :: zeros (image. size (), cv_8uc1); mask (R1 ). setto( 255); img1 = image (R1); image. copyto (img2, mask); image. copyto (img3); img3.setto (0, mask); imshow ("Image Sequence", image); imshow ("img1", img1); imshow ("img2", img2 ); imshow ("img3", img3); imshow ("Mask", mask); waitkey (0 );}
The result is as follows:
Note: The img2 in the second row should be img3 ,.
Regions of interest and use of mask in opencv