Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop a CvMat in OpenCV?

I have an image converted in a CvMat Matrix say CVMat source. Once I get a region of interest from source I want the rest of the algorithm to be applied to that region of interest only. For that I think I will have to somehow crop the source matrix which I am unable to do so. Is there a method or a function that could crop a CvMat Matrix and return another cropped CvMat matrix? thanks.

like image 340
Waqar Avatar asked Nov 25 '11 09:11

Waqar


People also ask

How do you crop a cv2 rectangle?

To crop an image to a certain area with OpenCV, use NumPy slicing img[y:y+height, x:x+width] with the (x, y) starting point on the upper left and (x+width, y+height) ending point on the lower right. Those two points unambiguously define the rectangle to be cropped.

How do you crop an image in Python?

crop() method is used to crop a rectangular portion of any image. Parameters: box – a 4-tuple defining the left, upper, right, and lower pixel coordinate. Return type: Image (Returns a rectangular region as (left, upper, right, lower)-tuple).

How do you crop an image with coordinates in Python?

crop() function that crops a rectangular part of the image. top and left : These parameters represent the top left coordinates i.e (x,y) = (left, top). bottom and right : These parameters represent the bottom right coordinates i.e. (x,y) = (right, bottom).


1 Answers

OpenCV has region of interest functions which you may find useful. If you are using the cv::Mat then you could use something like the following.

// You mention that you start with a CVMat* imagesource CVMat * imagesource;  // Transform it into the C++ cv::Mat format cv::Mat image(imagesource);   // Setup a rectangle to define your region of interest cv::Rect myROI(10, 10, 100, 100);  // Crop the full image to that image contained by the rectangle myROI // Note that this doesn't copy the data cv::Mat croppedImage = image(myROI); 

Documentation for extracting sub image

like image 77
Chris Avatar answered Sep 20 '22 19:09

Chris