How to cut image to smaller pieces? [closed]

asked 2016-01-11 10:03:49 -0600

jaqqu7 gravatar image

I need to cut image in half and each of them save as different one. For example I have image in grayscale and I try to do it like this:

halfImg[0] = Mat(greyimg, Rect(0, 0, 163, 93)).clone();
halfImg[1] = Mat(greyimg, Rect(164, 0, 327, 93)).clone();

but the second one generates this error:

image description

Is there another way to do it in Open CV?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-11-10 02:17:22.292195

Comments

1

Your Rect expects x, y, width and height, so if you give 164(x)+327(width)=491 on x, does the coordinate exists? What is the size of your greyImage?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-11 10:22:23 -0600 )edit

So, Is it possible that I just keep using Rect() wrong way? My image has size of 327x93.

jaqqu7 gravatar imagejaqqu7 ( 2016-01-11 10:26:47 -0600 )edit

take a look at http://answers.opencv.org/question/82641 you can adapt @thdrksdfthmn 's answer to divide your image vertically

sturkmen gravatar imagesturkmen ( 2016-01-11 10:28:52 -0600 )edit
1

Thanks! Now I understand what I did wrong. I did not know that the coordinates add up in this function. Your first comment helps me alot.

halfImage[0] = Mat(greyimg, Rect(0, 0, 163, 93)).clone();
halfImage[1] = Mat(greyimg, Rect(164, 0, 163, 93)).clone();

This is correct way.

jaqqu7 gravatar imagejaqqu7 ( 2016-01-11 10:33:14 -0600 )edit

it is still uncorrect. tell me why?

sturkmen gravatar imagesturkmen ( 2016-01-11 10:40:56 -0600 )edit

No, this is correct now.

jaqqu7 gravatar imagejaqqu7 ( 2016-01-11 10:47:15 -0600 )edit
2

you have two images both witdh is 163 :) check this one

cv::Rect leftROI( 0, 0, greyimg.cols / 2, greyimg.rows );
cv::Rect rightROI( leftROI.width, 0, greyimg.cols - leftROI.width , greyimg.rows );
halfImage[0] = greyimg( leftROI ).clone();
halfImage[1] = greyimg( rightROI ).clone();

or

halfImage[0] = Mat(greyimg, Rect(0, 0, 163, 93)).clone();
halfImage[1] = Mat(greyimg, Rect(163, 0, 164, 93)).clone();
sturkmen gravatar imagesturkmen ( 2016-01-11 10:58:46 -0600 )edit