根据像素值快速获取下标
# get index
def get_index(im, value):
# Load image
im = cv2.imread('image.png')
blue = [255, 0, 0]
# Get X and Y coordinates of all blue pixels
X, Y = np.where(np.all(im == value, axis=2))
zipped = np.column_stack((X, Y))
return zipped
根据坐标画图像的四边框
这里用的是x,y的最大值和最小值进行绘制
value是想需要找的图像的像素值
# get index
def arrow_index(im, value):
# Load image
# im = cv2.imread('image.png')
# blue = [255, 0, 0]
# Get X and Y coordinates of all blue pixels
X, Y = np.where(np.all(im == value, axis=2))
max_x = max(X)
min_x = min(X)
min_y = min(Y)
max_y = max(Y)
print(X, Y)
color = (166, 166, 166)
newBox = [[min_y, min_x], [max_y, min_x], [max_y, max_x], [min_y, max_x]]
point = np.array(newBox).astype(int)
cv2.line(im, tuple(point[0]), tuple(point[1]), color, thickness=1)
cv2.line(im, tuple(point[1]), tuple(point[2]), color, thickness=1)
cv2.line(im, tuple(point[2]), tuple(point[3]), color, thickness=1)
cv2.line(im, tuple(point[3]), tuple(point[0]), color, thickness=1)
cv2.imwrite('./result.jpg', img=im)
print()