Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV C++, getting Region Of Interest (ROI) using cv::Mat

Tags:

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:

  1. Is there a way to convert cv::Mat to cvMat so that I can use cvGetSubRect method to get the Region of interest?
  2. Are there any methods in 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.

like image 768
vprasad Avatar asked Jul 04 '11 00:07

vprasad


People also ask

How do I choose a region of interest in OpenCV?

Python OpenCV – selectroi() Function With this method, we can select a range of interest in an image manually by selecting the area on the image. Parameter: window_name: name of the window where selection process will be shown. source image: image to select a ROI.

What is region of interest in OpenCV?

In this article, we show how to create a region of interest in an image in Python using the OpenCV module. First, let's explain the concept of a region of interest. A region of interest is a place on an image where we want to search for something.

What is ROI OpenCV?

Many common image operations are performed using Region of Interest in OpenCV. A ROI allows us to operate on a rectangular subset of the image. The typical series of steps to use ROI is: create a ROI on the image, perform the operation you want on this subregion of the image, reset back the ROI.


2 Answers

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.

like image 72
Michael Koval Avatar answered Nov 11 '22 13:11

Michael Koval


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.

like image 44
Angie Quijano Avatar answered Nov 11 '22 15:11

Angie Quijano