Ask Your Question

# ROI, always a rectangle?

Hello

So far I havent had so much luck in people answering my questions ;; let's hope this one gets a reply :(

Quick and simple question. In openCV, ROIs have to be always rectangles??

I would like to use a circle as ROI. Is this not possible?

Thanks a lot in advance to all of you :)

edit retag close merge delete

## 3 answers

Sort by ยป oldest newest most voted

You must look this : link text

more

my comment from a closed question ....

ROI is a cv::Mat with it's own header while matrix data is shared with a parent cv::Mat than is rectangular by definition. May be could be usefull Optional operation mask used by many functions

more

The ROI of a mat is not needed to be always a rectangle, but cv::Mat is always a matrix, or a rectangle image, so you cannot have a round image, but you may have a circle on a black background that contains the information that you want. For having this, you have to use a maks:

cv::Mat image = cv::imread("img.jpg");
cv::Mat mask = cv::Mat::zeros(image.size(), CV_8UC1);
cv::Point circleCenter(mask.cols / 2, mask.rows / 2);
int radius = std::min(mask.cols, mask.rows);
cv::circle(mask, circleCenter, radius, CV_RGB(255, 255, 255));
cv::Mat imagePart = cv::Mat::zeros(image.size(), image.type());
image.copyTo(imagePart, mask);


So, the ROI may be any shape, by using a mask. But if you are refering at the ROI as a submatrix, then you must have a rectangle for defining it (you can use the rectangle that englobes the circle: cv::Rect roi(circleCenter.x - radius, circleCenter.y - radius, 2*radius, 2*radius); but do not forget to do an AND operation, for being sure that it does not get out of the image boundaries: roi = roi & cv::Rect(0, 0, image.cols, image.rows)).

more

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2015-01-25 19:16:03 -0500

Seen: 16,231 times

Last updated: Oct 08 '15