img_src = cv2.imread("cs1.jpg")
# erode
kernel = np.ones((3, 3),np.uint8)
img_src = cv2.dilate(img_src ,kernel)
img_src = cv2.erode(img_src, kernel)
# find contour
img_contour, contours, hierarchy = cv2.findContours(img_bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = img_src.copy()
# contours is a contour list
mask = cv2.drawContours(mask, contours[0], 0, (255, 255, 255), -1) # draw one contour
# find max contour area
area = []
new_mask = np.zeros(mask.shape, dtype=np.uint8)
for i in range(len(contours)):
area.append(cv2.contourArea(contours[i]))
max_idx = np.argmax(area)
cv2.fillConvexPoly(new_mask, contours[max_idx], (255,255,255))
# 计算极值点
left_most = tuple(contour[contour[:, :, 0].argmin()][0])
right_most = tuple(contour[contour[:, :, 0].argmax()][0])
top_most = tuple(contour[contour[:, :, 1].argmin()][0])
bottom_most = tuple(contour[contour[:, :, 1].argmax()][0])
# plot point
cv2.circle(i
cv2 边缘识别、膨胀腐蚀、求最小包围矩阵
最新推荐文章于 2023-02-07 11:13:34 发布
本文介绍了如何使用OpenCV库中的cv2模块进行图像处理,包括边缘检测、膨胀腐蚀等形态学操作,以及如何找到图像的最小包围矩阵。通过Python代码示例,详细阐述了这些技术在实际应用中的实现步骤。
摘要由CSDN通过智能技术生成