Cv2.ApproxPolydp()返回什么? [英] What does cv2.approxPolydp() return?
本文介绍了Cv2.ApproxPolydp()返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cv2.approxPolyDP()
后检测到的轮廓调整图像的大小。然后,我必须根据cv2.approxPolyDP()
检测到的轮廓返回值使用cv2.resize()
自定义裁剪,并根据cv2.approxPolyDP()
检测到的轮廓返回值进行裁剪。
我想知道哪个索引是高度,哪个索引是宽度,或者起始x,y坐标和结束x,y坐标。
def biggestContour(contours):
biggest = np.array([])
max_area = 0
for i in contours:
area = cv2.contourArea(i)
if area > 5000:
peri = cv2.arcLength(i, True)
approx = cv2.approxPolyDP(i, 0.02 * peri, True)
if area > max_area and len(approx) == 4:
biggest = approx
max_area = area
return biggest,max_area
考虑这段代码,我如何找到哪个索引是高度,哪个索引是宽度,或者起始x,y坐标和结束x,y坐标?
推荐答案
cv2.approxPolyDP
返回重新采样的等高线,因此这仍将返回一组(x, y)
点。如果你想裁剪出这个结果,返回的轮廓应该是一个N x 1 x 2
NumPy数组,所以去掉单值维度,然后在x和y坐标上执行标准的最小/最大运算,得到左上角和右下角,最后进行裁剪。假设要裁剪的图像名为img
,根据cv2.findContours
计算的轮廓列表名为contours
,请尝试:
# Find biggest contour
cnt, area = biggestContour(contours)
# Remove singleton dimensions
points = np.squeeze(cnt)
# Extract rows and columns
y = points[:, 0]
x = points[:, 1]
# Now crop
(topy, topx) = (np.min(y), np.min(x))
(bottomy, bottomx) = (np.max(y), np.max(x))
out = img[topy:bottomy+1, topx:bottomx+1]
out
现在将包含裁剪的图像。
这篇关于Cv2.ApproxPolydp()返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文