open-cv的轮廓检测矩形拟合 下图中如何实现最大矩形的检测呢
5条回答 默认 最新
脚踏南山 2022-07-19 17:20关注获得5.00元问题酬金 import cv2 as cv import numpy as np img_path = "/home/LTL/Desktop/20220719171125.png" img_np = cv.imread(img_path) ret, thresh1 = cv.threshold(img_np, 80, 255, cv.THRESH_BINARY) np_zeros = np.ones(thresh1.shape[0:2]) * 255 index_1 = np.where(thresh1[:, :, 0] == 0) index_2 = np.where(thresh1[:, :, 1] == 0) index_3 = np.where(thresh1[:, :, 1] == 0) np_zeros[index_1[0], index_1[1]] = 0 np_zeros[index_2[0], index_2[1]] = 0 np_zeros[index_3[0], index_3[1]] = 0 contour_list, _ = cv.findContours(np_zeros.astype(np.uint8), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contour_area_list = list(map(lambda x: cv.contourArea(x), contour_list)) contour_list = list(filter(lambda x: 100 < cv.contourArea(x) < img_np.shape[0] * img_np.shape[1] // 2, contour_list)) result_xywh = [] result_poly_point = [] contour_list.sort(key=lambda x: cv.contourArea(x), reverse=True) for contour_i, contour in enumerate(contour_list[0:1]): contour_area = cv.contourArea(contour) x, y, w, h = cv.boundingRect(contour) poly_point_list = contour[:, 0, :].ravel().tolist() poly_point = np.int0(poly_point_list).reshape((-1, 1, 2)) result_poly_point.append(poly_point) cv.rectangle(img_np, (x, y), (x + w, y + h), (255, 255, 0), 2) cv.imshow('', img_np) cv.waitKey() cv.destroyAllWindows()
评论 打赏 举报解决 2无用
