【发布时间】:2019-11-26 21:38:33
【问题描述】:
我一直在为计算这样一个图形的宽度和高度测量值的问题而摸不着头脑。主要挑战是我不能使用 miBoundingrectangle 并且无法从内部找出一种方法,无论哪种方式我都会丢失一些用于高度和宽度测量的像素。
示例输入:
样本输出:
是否有任何防故障方法来获得准确的尺寸测量?
下面是我试图找到内部边界最大矩形的解决方案。
_,contour2,_=cv2.findContours(im,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
for c in contour2:
area=cv2.contourArea(c)
if area ==25224.0:
print(area)
n = np.squeeze(contour2[0])
x = sorted(n, key=lambda a:a[0])
left = x[0]
right = x[-1]
print("",left,right)
y= sorted(n, key=lambda a:a[1])
top = y[0]
bottom = y[-1]
cv2.drawContours(im,[c],-1,(128,128,128),2)
cv2.circle(im, (left[0],left[1]), 4, (128,128,128), 8)
cv2.circle(im, (right[0],right[1]), 4, (128,128,128), 8)
cv2.circle(im, (top[0],top[1]), 4, (128,128,128), 8)
cv2.circle(im, (bottom[0],bottom[1]), 4, (128,128,128), 8)
roi_w = int(np.sqrt((top[0]-right[0])*(top[0]-right[0])(top[1]-right[1])*(top[1]-right[1])))
roi_h = int(np.sqrt((top[0]-left[0])*(top[0]-left[0])+(top[1]-left[1])*(top[1]-left[1])))
pts1 = np.float32([top,right,left])
new_top = top
new_right = [top[0] + roi_w, top[1]]
new_left = [top[0], top[1] + roi_h]
pts2 = np.float32([new_top,new_right,new_left])
cv2.imshow("threshed", im)`
【问题讨论】:
-
你能添加你的输入图像吗?
-
对不起,进行了编辑:)
-
您能否详细说明您想要在中心宽度或高度中获得的最小宽度和行数?
-
在这种情况下是从质心(中心)开始的宽度和高度。
标签: python image opencv image-processing computer-vision