Crop half of an image in OpenCV

shjnlee picture shjnlee · Jul 29, 2016 · Viewed 10k times · Source

How can I crop an image and only keep the bottom half of it?

I tried:

Mat cropped frame = frame(Rect(frame.cols/2, 0, frame.cols, frame.rows/2));

but it gives me an error.

I also tried:

double min, max;
Point min_loc, max_loc;
minMaxLoc(frame, &min, &max, &min_loc, &max_loc);
int x = min_loc.x + (max_loc.x - min_loc.x) / 2;
Mat croppedframe = = frame(Rect(x, min_loc.y, frame.size().width, frame.size().height / 2));

but it doesn't work as well.

Answer

rajeevr22 picture rajeevr22 · Jul 29, 2016

Here's a the python version for any beginners out there.

def crop_bottom_half(image):
    cropped_img = image[image.shape[0]/2:image.shape[0]]
    return cropped_img