A little pit in cv2.rectangle

When looking for a bug today, I found a cv2.rectangle that is not consistent with our common sense inertia:
generally we are very familiar with the usage of this function, which is to draw a rectangular box on the map, and to draw a boundinig box in the trace:
cv2. rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None

  • The previous idea: Give img, and the upper left corner coordinate pt1 and the lower right corner coordinate pt2, and then you can draw the matrix (so it is very honest to input according to the format requirements)
  • The current thinking, pt1 is not necessarily the coordinates of the upper left corner, and pt2 is not necessarily the coordinates of the lower right corner (see the example below)

Test code:

import cv2
import numpy as np
img = cv2.imread('/home/lz/Videos/OTB100/Bird1/img/0001.jpg')
gt = np.loadtxt('/home/lz/Videos/OTB100/Bird1/groundtruth_rect.txt', delimiter=',')[0]
x1, y1, x2, y2 = gt[0], gt[1], gt[0]+gt[2], gt[1]+gt[3]
img = cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 1)
cv2.imshow('img', img)
cv2.imwrite('bird.png', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is of course no surprise:
Insert picture description here
we can try all the remaining three cases, which are

img = cv2.rectangle(img, (int(x1), int(y2)), (int(x2), int(y1)), (0, 255, 0), 1)
img = cv2.rectangle(img, (int(x2), int(y1)), (int(x1), int(y2)), (0, 255, 0), 1)
img = cv2.rectangle(img, (int(x2), int(y2)), (int(x1), int(y1)), (0, 255, 0), 1)

It is found that they are all the same as those drawn by the first type, so sometimes the box form given by the data set may be wrong, but it still looks correct when you visualize it. This needs to be noted [Of course you can’t put similar x2, y1, y2) Enter cv2.rectangle]
And when the vertex of your upper left corner has a negative value, cv2.rectangle can still be drawn, of course, you can’t see it out of the field of view.

Just remind myself with this~~

Guess you like

Origin blog.csdn.net/laizi_laizi/article/details/108794334