Reputation: 1461
I'm very new to OpenCV (started using it two days ago), I'm trying to cut a hand image from a depth image got from Kinect, I need the hand image for gesture recognition. I have the image as a cv::Mat
type. My questions are:
cv::Mat
to cvMat
so that I can use cvGetSubRect
method to get the Region of interest?cv::Mat
that I can use for getting the part of the image?I wanted to use IplImage
but I read somewhere that cv::Mat
is the preferred way now.
Upvotes: 22
Views: 96280
Reputation: 8367
You can use the overloaded function call operator on the cv::Mat
:
cv::Mat img = ...;
cv::Mat subImg = img(cv::Range(0, 100), cv::Range(0, 100));
Check the OpenCV documentation for more information and for the overloaded function that takes a cv::Rect
. Note that using this form of slicing creates a new matrix header, but does not copy the data.
Upvotes: 48
Reputation: 4453
Maybe an other approach could be:
//Create the rectangle
cv::Rect roi(10, 20, 100, 50);
//Create the cv::Mat with the ROI you need, where "image" is the cv::Mat you want to extract the ROI from
cv::Mat image_roi = image(roi)
I hope this can help.
Upvotes: 25