我想估算一个给定遮罩对象的4坐标四边形(不仅仅是矩形),如图+所示,而不会丢失遮罩对象的任何像素
我尝试使用CV2,但最终无法找到解决方案
正在尝试cv2.boundinRect:
#mask = grayed image with only a specific object being masked
#image = the original rgb image
x,y,x_width,y_height = cv2.boundingRect(mask)
image=np.array(im[0])
cv2.rectangle(image,(x,y),(x+x_width,y+y_height),(0,255,0),2)
plt.imshow(image)
正在尝试cv2.findContours+cv2.approxPolyDP:
#mask = grayed image with only a specific object being masked
#image = the original rgb image
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
selected_contour = max(contours, key=lambda x: cv2.contourArea(x))
approx = cv2.approxPolyDP(selected_contour, 0.0035 * cv2.arcLength(selected_contour, True), True)
cv2.drawContours(image, [approx], 0, (0, 0, 255), 5)
plt.imshow(image)
我不确定是否有更好的或内置的版本;但我有一个基于随机数的简单想法:
我只在顶部做了这个,但你也可以在其他方面做同样的事情。其思想是首先找到对象的边界框;然后把物体分成相等的部分,这样我们就能找到最高的山峰
在每个范围内,您可以随机找到点;但为了获得最佳效果,最好检查形状的所有顶点,以正确找到最高峰值
在找到最高峰值后,我们必须计算一个关于这两个点的直线方程,以便我们可以绘制一条关于直线方程的全局直线
相关问题 更多 >
编程相关推荐