Good way to prevent image Rect area from out of bounds

A good way to prevent the image Rect area from going out of bounds:

OpenCV's cv::Rect provides many practical methods, please refer to: http://blog.csdn.net/da_yuan8421/article/details/60959419:

When processing an image, it is often necessary to cut out a certain area in the image for processing. If the cut-out area crosses the boundary, it is easy to cause the image to collapse.

//求两个矩形的交集和并集
rect = rect1 & rect2;
rect = rect1 | rect2;
//对矩形进行对比,返回布尔变量
rect1 == rect2;
rect1 != rect2;

Using the intersection of two Rects, we can easily avoid the situation where the image cropping area is out of bounds, as follows:

Rect rect;
rect.x = -10;
rect.y = -10;
rect.height = 100000;
rect.width = 20000;
rect &= Rect(0, 0, src.cols, src.rows);//求交集
cv::Mat crop_img = src(rect);

In the above example, the size of the original image src=200*200, which needs to be cropped to rect=[-10,-10,10000,20000]. In order to avoid cropping Rect out of bounds, special protection is required. The easiest way is to add this sentence Words: rect &= Rect(0, 0, src.cols, src.rows), the Rect of this intersection will definitely not cross the bounds.
Reprinted: https://blog.csdn.net/j879159541/article/details/97117935

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324125628&siteId=291194637