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.