使用其他图像处理技术更容易找到角点。由于您的图像充满了细节,我们可以使用阈值、腐蚀和膨胀。往下看:
color = cv2.imread('box.png')
plt.imshow(color);
plt.xticks([]);
plt.yticks([]);
gray = cv2.imread('box.png',0)
gray[gray > 10 ] = 255
plt.imshow(gray,cmap='gray');
plt.xticks([]);
plt.yticks([]);
kernel = np.ones((5,5),np.uint8)
gray = cv2.dilate(gray,kernel,iterations = 10)
gray = cv2.erode(gray,kernel,iterations=15)
gray = cv2.dilate(gray,kernel,iterations = 3)
plt.imshow(gray,cmap='gray');
plt.xticks([]);
plt.yticks([]);
y,x = np.where(gray == 255)
y_index = np.argmin(y)
x_coord = x[y_index]
gray[0:,0:x_coord] = 0
plt.imshow(gray,cmap='gray');
plt.xticks([]);
plt.yticks([]);
# Top-left
top_left = (x_coord,y[y_index])
cv2.circle(color,top_left,5,[0,255,0],-1);
# Bottom-left
y,x = np.where(gray == 255)
y_index = np.argmax(y)
x_coord = x[y_index]
bottom_left = (x_coord,y[y_index])
cv2.circle(color,bottom_left,5,[0,255,0],-1);
# Top-right
y,x = np.where(gray == 255)
x_index = np.argmax(x)
y_coord = y[x_index]
top_right = (x[x_index],y_coord)
cv2.circle(color,top_right,5,[0,255,0],-1);
# Bottom-right
for y in range(top_right[1],gray.shape[0],1):
roi = gray[y:y+1,top_right[0]-11:top_right[0]]
if np.all((roi == 0)):
break
bottom_right = (top_right[0],y)
cv2.circle(color,bottom_right,5,[0,255,0],-1);
plt.imshow(color);
plt.xticks([]);
plt.yticks([]);
您可以使用所有值来获得不同的结果。如果你改变视角,你可能需要稍微改变一下代码。