如何利用等高线高效地进行纸张的四点变换

2024-10-01 07:16:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在下面的问题纸上使用4点变换。根据代码,它应该选择具有4个点的最长轮廓(这是环绕纸张的绿色近似矩形)并围绕该轮廓扭曲页面。但是当我执行4点变换时,它给出了奇怪的结果

当我使用过滤器对图像进行预处理时,它没有这个问题。有没有办法在OpenCV中应用这样的过滤器

这是为了找到轮廓

cnts=
cv2.findContours(edged.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
docCnt = None
ok=False
# ensure that at least one contour was found
if len(cnts) > 0:
# sort the contours according to their size in
# descending order
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

# loop over the sorted contours
    for c in cnts:
    # approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

    # if our approximated contour has four points,
    # then we can assume we have found the paper
        if len(approx) == 4:
            docCnt = approx
            ok=True
            break

这是应用4点变换

paper = four_point_transform(image, docCnt.reshape(4, 2))

These contours were obtained with the filtered image

These contours were obtained with original image

Filtered imageOriginal Image


Tags: theimagetrue过滤器lenifokcv2