opencv2 Canny+findContours无法处理某些图像

2024-04-26 10:29:43 发布

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

我正在尝试创建一个程序,将检测和删除图片中的边框,目标是检测图片中的文档并清除它。。。你知道吗

这是我的密码:

import sys
import cv2
import numpy as np
import rect

image = cv2.imread('./test.jpg')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 9)
ret, gray = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)

edges = cv2.Canny(gray, 10, 250)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)

#x,y,w,h = cv2.boundingRect(contours[0])
#cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),0)

# get approximate contour
for c in contours:
    p = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * p, True)

    if len(approx) == 4:
        target = approx
        break


cv2.drawContours(image, [target], -1, (0, 255, 0), 2)
cv2.imwrite('./final.jpg', image)

形象测试.jpg是: enter image description here

但现在…它唯一能找到的是:

enter image description here

……根据要求,这是一张有效的图片:

enter image description here


Tags: 文档imageimport程序truetarget目标图片
2条回答

正如在聊天中所讨论的,我建议您使用Feature Description and Matching来实现这一点。根据我的经验,它比等高线快,你应该能够绕过光线、视角等变化的问题

以下是我尝试的:

import cv2
import numpy as np


def locater(image, source, num=0):
    def resize(im, new_width):
        r = float(new_width) / im.shape[1]
        dim = (new_width, int(im.shape[0] * r))
        return cv2.resize(im, dim, interpolation=cv2.INTER_AREA)
    #width = 300
    #source = resize(source, new_width=width)
    #image = resize(image, new_width=width)

    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
    image, u, v = cv2.split(hsv)

    hsv = cv2.cvtColor(source, cv2.COLOR_BGR2LUV)
    source, u, v = cv2.split(hsv)

    MIN_MATCH_COUNT = 10
    orb = cv2.ORB_create()
    kp1, des1 = orb.detectAndCompute(image, None)
    kp2, des2 = orb.detectAndCompute(source, None)

    flann = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_FLANNBASED)

    des1 = np.asarray(des1, dtype=np.float32)
    des2 = np.asarray(des2, dtype=np.float32)

    matches = flann.knnMatch(des1, des2, k=2)

    # store all the good matches as per Lowe's ratio test
    good = []
    for m, n in matches:
        if m.distance < 0.7 * n.distance:
            good.append(m)

    if len(good) >= MIN_MATCH_COUNT:
        src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
        dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

        M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
        matchesMask = mask.ravel().tolist()

        h,w = image.shape
        pts = np.float32([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]).reshape(-1, 1, 2)
        dst = cv2.perspectiveTransform(pts, M)
        source_bgr = cv2.cvtColor(source, cv2.COLOR_GRAY2BGR)
        img2 = cv2.polylines(source_bgr, [np.int32(dst)], True, (0,0,255), 3, 
                             cv2.LINE_AA)
        cv2.imwrite("out"+str(num)+".jpg", img2)
    else:
        print("Not enough matches." + str(len(good)))
        matchesMask = None

    draw_params = dict(matchColor=(0, 255, 0), # draw matches in green color
                       singlePointColor=None,
                       matchesMask=matchesMask, # draw only inliers
                       flags=2)
    img3 = cv2.drawMatches(image, kp1, source, kp2, good, None, **draw_params)
    cv2.imwrite("ORB"+str(num)+".jpg", img3)

image = cv2.imread('contour.jpg')
source = cv2.imread('contour_source.jpg')
locater(source, image, num=1)

源图像:

Source

结果:

Result 1

Result 2

一些注释:由于图像不太好,单应性很好。你可以通过获得更好的图像质量来提高它的准确性——用一台像样的扫描仪扫描原稿,调整图像大小(我为此添加了一个函数)以及使用不同的颜色空间(我在这里使用LUV)。你知道吗

希望有帮助!你知道吗

如果您查看docs.opencv.org上的文档,您会发现它有许多可以提供的参数,如:

  1. threshold1:滞后过程的第一个阈值。你知道吗
  2. threshold2:滞后过程的第二个阈值。你知道吗
  3. apertureSize:Sobel操作符的孔径大小。你知道吗
  4. L2gradient:一个标志,指示是否应该使用更精确的L2范数来计算图像梯度幅度(L2gradient=true),或者默认的L1norm =|dI/dx|+|dI/dy|是否足够(L2gradient=false)。你知道吗

我建议用这些来得到想要的结果。你知道吗

相关问题 更多 >