Alessandro Ceccarelli
Alessandro Ceccarelli

Reputation: 1945

Crop an image using OpenCV Coordinates

I have the coordinates values of an image as:

tl = (result['topleft']['x'], result['topleft']['y'])
br = (result['bottomright']['x'], result['bottomright']['y'])

I would like to crop the original image (of sizes [720,720,3]) around that area of interest; what is the correct formula?

I have found this one:

crop_img = imgcv[y:y+h, x:x+w]

But I struggle to put the right values of the coordinate system in it;

Upvotes: 2

Views: 3818

Answers (1)

api55
api55

Reputation: 11420

crop_img = imgcv[y:y+h, x:x+w] is a correct formula to do it if you have a rectangle, i.e. the top left point and the width and height of the rectangle, but you can do it directly since you have the top left and bottom right points.

crop_img = imgcv[tl[1]:br[1], tl[0]:br[0]]

basically the formula tells from:to first in y coordinates and then in x coordinates. Since the top left point of the image is the origin, then its coordinates is the from and the bottom right coordinates is the to

If you have any doubt, leave a comment

Upvotes: 4

Related Questions