繁体   English   中英

cv2.approxPolydp() 返回什么?

[英]What does cv2.approxPolydp() return?

我正在尝试根据应用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 数组,因此删除 singleton 维度,然后对 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现在将包含裁剪后的图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM