Home > Blockchain >  How do OpenCV's selectROI coordinates work?
How do OpenCV's selectROI coordinates work?

Time:02-12

I need to use the selectROI function for my project. However, I just can't seem to wrap my head around the format of the coordinates. According to this link, the coordinate array should be in this form: [Top_X, Top_Y, Bottom_X, Bottom_Y]. Assuming that the top left corner starts at (0,0) for (x,y), why are my coordinates so off when I draw 3 different bounding boxes using the selectROI function?

This is what I used to create my bounding boxes and output the coordinates:

import cv2

image = cv2.imread("path")

# Select ROI
r = cv2.selectROI("select the area", image, fromCenter=False)

print('Selected bounding boxes: {}'.format(r))

The following were outputted (see the image for reference of the boxes):

box 1: (42, 18, 212, 303) box 2: (463, 314, 108, 226) box 3: (1215, 801, 79, 161) image

Why do the x-values increase from 42 -> 212 -> 464 -> back to 108 -> 1125 -> back to 79... shouldn't the coordinates for the x's be increasing? And same goes for the y.

CodePudding user response:

The explanation on geeksforgeeks is wrong or at least easy to misunderstand: the last two values are not coordinates for the bottom-right corner, they are width and height of the rectangle. Notice that e.g. for the third rectangle, the height (161) is roughly twice the width (79), which matches the appearance of the rectangle.

  • Related