Process the cropped image, draw on the original one

I have an image that I am cropping and finding contours in it. All works fine. However, my contours are drawn with a shift in the original image. Obviously, this is happening because the contours were detected in the cropped image, rather than the original one. Hence the shift.

# cut off the irrelevant stuff, work on the ROI
cropped_image = image[0:800, 500:1200]

# get the contour of the object
ret, thresh = cv2.threshold(cropped_image, 150, 255, cv2.THRESH_BINARY)
gray = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
contours, hierarchy2 = cv2.findContours(gray, cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

# draw on the original image, not the cropped one
cv2.drawContours(image, contours, -1, (0, 255, 0), 2, cv2.LINE_AA)

My question is, how can I account for that “shift” while drawing on the original image? I think I need a one-liner that adds the lost coordinates to my cropped image.

either draw into cropped_image or… look at the contours object. numpy is perfectly capable of adding vectors.

contours = [c + (500,0) for c in contours] or something like that

1 Like
contours = [c + (500,0) for c in contours]

Did the magic, thank you : )