Attention! This forum will be made read-only by Dec-20. Please migrate to Most of existing active users should've received invitation by e-mail.
Ask Your Question

How to get the ROI of an image from the center

asked 2015-01-22 23:40:15 -0500

Ztar03 gravatar image

Hello good evening I started researching about getting the ROI of an image and from what I understand is using function RECT I've tried but I get an error and honestly do not quite understand its parameters mean, if someone can help me roi get the picture attached below.

image description

My code is the next

Mat image;
image = imread(argv[1]);
int alto = 100; 
Rect roi(0,(image.rows)-alto,image.cols,alto);
Mat imgROI = image(roi);

and the error is the next

Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array') in binary_op, file /home/jenny/Downloads/opencv-2.4.9/modules/core/src/arithm.cpp, line 1021 terminate called after throwing an instance of 'cv::Exception'

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-01-23 08:36:53 -0500

If you want to cut out that roi by its parameters than you can do this by the following code snippet.

// Read image
Mat image = imread("/path/to/image");
// Define the roi
// Parameters are top left corner x, y followed by width and height of your roi
// Sample will cut out 100x100 pixels from the top left corner
Rect region_of_interest(0,0,100,100);
// Create a pointer to the roi in the original image
// Copy the data to a standalone matrix --> use a deep clone not only pointer reference copy
Mat temp (image, roi);
Mat ROI = temp.clone();
// Show the result
imshow("region of interest", ROI);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-01-22 23:40:15 -0500

Seen: 2,165 times

Last updated: Jan 23 '15