编程语言:C++、Python我有一些2D轮廓信息,部分损坏,里面是空的。我想要的是近似这个轮廓与多边形(最大,八角形)。有哪些可能的解决办法?cv的findContours等函数由于cut off而无法正常工作。
输入轮廓
输出(我想要)
46scxncf1#
下面是Python/OpenCV中的两个结果。第一个获取围绕您的图形旋转的矩形。第二个得到关于你的图形的凸船体,然后将顶点数减少到8。输入:
import cv2 import numpy as np # read the input as grayscale img = cv2.imread('broken_polygon.jpg', cv2.IMREAD_GRAYSCALE) hh, ww = img.shape # threshold to binary img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # get non-zero points point points = np.column_stack(np.where(img.transpose() > 0)) # compute rotated rectangle from thresholded non-zero coords rect = cv2.minAreaRect(points) print(rect) # draw rotated rectangle on input img_rect = img.copy() img_rect = cv2.merge([img_rect,img_rect,img_rect]) box = np.intp(cv2.boxPoints(rect)) print('box:', box) cv2.polylines(img_rect, [box], True, (0,0,255), 2) # get convex hull hull = cv2.convexHull(points) # reduce vertices of convex hull peri = len(hull) poly = cv2.approxPolyDP(hull, 0.28 * peri, False) print('num reduced vertices:', len(poly)) # draw polygon on copy of input img_hbox = img.copy() img_hbox = cv2.merge([img_hbox,img_hbox,img_hbox]) cv2.polylines(img_hbox, [poly], False, (0,0,255), 2) # save results cv2.imwrite('broken_polygon_rot_rect.jpg', img_rect) cv2.imwrite('broken_polygon_hull_poly.jpg', img_hbox) # show results cv2.imshow('RotRect', img_rect) cv2.imshow('HullPoly', img_hbox) cv2.waitKey(0)
旋转矩形:
简化的凸形船体顶点:
1条答案
按热度按时间46scxncf1#
下面是Python/OpenCV中的两个结果。第一个获取围绕您的图形旋转的矩形。第二个得到关于你的图形的凸船体,然后将顶点数减少到8。
输入:
旋转矩形:
简化的凸形船体顶点: