opencv tip, Rect bounding

Sometimes, it causes memory error or unexpected result when we set lager rect size than image.

It is very annoying to always check.

But this tip is very simple and easy.

Plz see the code and result.

cv::Rect bounds(0,0,100,100);
cv::Rect roi(10,10,40,40);
Rect boundedRect = (roi & bounds);
cout << "x = " << boundedRect.x << endl;
cout << "y = " << boundedRect.y << endl;
cout << "width = " << boundedRect.width << endl;
cout << "height = " << boundedRect.height << endl;
cv::Rect roi2(-10,10,40,40);
Rect boundedRect2 = (roi2 & bounds);
cout << boundedRect << endl;

cv::Rect roi3(-10,-10,400,40);
cout << (roi3 & bounds) << endl;


Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)