opencv2 Canny + findContours在某些图像上不起作用

时间:2019-03-06 02:02:48

标签: python opencv image-processing

我正在尝试创建一个程序,该程序可以检测并去除图片中的边框,目的是检测图片中的文档并对其进行清理...

这是我的代码:

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)

图像teste.jpg为: enter image description here

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

enter image description here

...并根据要求提供有效的图片:

enter image description here

2 个答案:

答案 0 :(得分:1)

正如聊天中所讨论的,我建议您为此使用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

一些注意事项:由于 source 图像不太好,因此单应性就很好。通过获得更好的图像质量,可以使图像更加准确-通过使用体面的扫描仪扫描原稿,调整图像大小(为此我添加了一个功能)以及使用不同的色彩空间(在这里使用LUV)。 / p>

希望有帮助!

答案 1 :(得分:0)

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

  1. threshold1:磁滞过程的第一个阈值。
  2. threshold2:磁滞过程的第二个阈值。
  3. apertureSize:Sobel运算符的光圈大小。
  4. L2gradient:一个标志,指示是否应使用更准确的L2范数来计算图像梯度幅度(L2gradient=true,或者默认L1 norm =|dI/dx|+|dI/dy|是否足够({ {1}})。

我建议玩这些游戏以获得期望的结果。