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.
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.
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).
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With