方形检测,边缘像差和1个缺角

时间:2012-09-04 08:09:38

标签: python image-processing opencv computer-vision

这是similar questions关于方检测的几个跟进,其中karlphillipmevatronabid-rahman-k提出了一些很酷的方法

我正在尝试设计一种强大的方形检测算法,以帮助将收据的图片与图像的其余部分隔离开来。我的代码是基于前面问题的凸包方法构建的,但是它在图像上窒息,其中一个点不在图像中,并且收据的边缘由于左侧的笔架而具有像差。

如何检测此收据上的角落?

这是图片:

image of receipt

这是我的代码:

import cv2
import numpy as np

img = cv2.imread('taco.jpg')
img = cv2.resize(img,(1944,2592))
img = cv2.medianBlur(img,31)
img = cv2.GaussianBlur(img,(0,0),3)

grayscale = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
thresh = cv2.Canny(grayscale, 10, 20)
thresh = cv2.dilate(thresh,None)

contours,hier = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    if cv2.contourArea(cnt)>250:  # remove small areas like noise etc
        hull = cv2.convexHull(cnt)    # find the convex hull of contour
        hull = cv2.approxPolyDP(hull,0.1*cv2.arcLength(hull,True),True)
        if len(hull)==4:
            cv2.drawContours(img,[hull],0,(0,255,0),2)

cv2.namedWindow('output',cv2.cv.CV_WINDOW_NORMAL)
cv2.imshow('output',img)
cv2.cv.ResizeWindow('output',960,640)
cv2.waitKey()
cv2.destroyAllWindows()

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

Mathematica中的解决方案:

导入图片:

i = Import@"http://i.imgur.com/RrYKJ.jpg";

检测比收据中的字母(参数)

中的刻度更大的边缘
i1 = EdgeDetect[i, 10]

Mathematica graphics

删除小于收据周长(参数)

的行的行
i2 = DeleteSmallComponents[i1, 1000]

Mathematica graphics

找出形态成分

(mc = MorphologicalComponents[Erosion[ColorNegate@i2, 1]]) // Colorize

Mathematica graphics

找到具有更多边界邻接的变形组件(从掩码中删除它)

com = Commonest[Join[mc[[1]], mc[[-1]], Transpose[mc][[1]], Transpose[mc][[-1]]]]

形成面具

mc1 = Unitize[mc /. com[[1]] -> 0];

Mathematica graphics

将掩码乘以原始图像

ImageMultiply[Image@mc1, i]

Mathematica graphics