有没有办法根据盒子周围的 4 个小方块来获取盒子内的内容(我已经注意到红色虚线矩形)?
我的代码:
# Read and resize image
img_input = cv2.imread(args.pop('image'), )
img_input_height, img_input_width, _ = img_input.shape
img_input_ap = img_input_width / img_input_height
img_input_width = int(const_image_max_height * img_input_ap)
img_input = cv2.resize(img_input, (img_input_width, img_input_height,), )
# Process image color
img_final = img_input.copy()
img_gray = cv2.cvtColor(img_input, cv2.COLOR_BGR2GRAY, )
img_blur = cv2.GaussianBlur(img_gray, (5, 5,), 5)
img_canny = cv2.Canny(img_blur, 10, 70, )
# Find around contours
img_canny_contours, _ = cv2.findContours(
img_canny,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE,
)
# Find square contours
img_preview = img_final.copy()
for contour in img_canny_contours:
rect = cv2.boundingRect(contour)
rect_width = rect[2]
rect_height = rect[3]
if 0.8 <= (rect_width / rect_height) <= 1.2:
# Now I founded the squares...
# Now I need to find the rectangle between 4 squares founded here...
cv2.imshow('img_preview', img_preview)
cv2.waitKey()
cv2.destroyAllWindows()