如何从opencv中的图片中检测文档? [英] How to detect document from a picture in opencv?

查看:185
本文介绍了如何从opencv中的图片中检测文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计类似于camscanner的应用。为此,我必须拍摄一张图片然后找到该文件。我从这里描述的代码开始 -



我试图用最大面积打印轮廓,这导致了这个(字母'C'中的轮廓)



代码:

  img = cv2.imread('bounce.jpeg' )
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(gray,255) ,1,1,11,2)
_,等高线,等级= cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

def maximumRectangle(轮廓):
最大=无
max_area = 0
indexReturn = -1
指数范围内(l en(轮廓)):
i =等高线[指数]
面积= cv2.contourArea(i)
如果面积> 100:
peri = cv2.arcLength(i,True)
about = cv2.approxPolyDP(i,0.1 * peri,True)
if area> max_area:#and len(约)== 4:
最大=约
max_area = area
indexReturn = index
返回indexReturn

indexReturn = largestRectangle (轮廓)
cv2.imwrite('hola.png',cv2.drawContours(img,contours,indexReturn,(0,255,0)))

这有什么问题?有没有其他方法我可以在这张图片中捕获文件?

解决方案

试试这个:
输出图片

  import cv2 
import numpy as np

img = cv2.imread('bounce.jpg')
gray = cv2.cvtColor(img,cv2。 COLOR_BGR2GRAY)
invGamma = 1.0 / 0.3
table = np.array([((i / 255.0)** invGamma)* 255
for i in np.arange(0,256)] ).astype(uint8)

#使用查找表应用伽马校正
gray = cv2.LUT(灰色,表格)

ret,thresh1 = cv2.threshold(灰色,80,255,cv2.THRESH_BINARY)

#thresh = cv2.adaptiveThreshold(灰色,255,1,1,11,2)
_,等高线,等级= cv2 .findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

def maximumRectangle(轮廓):
最大=无
max_area = 0
indexReturn = -1
表示范围内的指数(len(轮廓)):
i = contours [index]
area = cv2.contourArea(i)
if area> 100:
peri = cv2.arcLength(i,True)
about = cv2.approxPolyDP(i,0.1 * peri,True)
if area> max_area:#and len(约)== 4:
最大=约
max_area = area
indexReturn = index
返回indexReturn

indexReturn = largestRectangle (轮廓)
hull = cv2.convexHull(等高线[indexReturn])
cv2.imwrite('hola.png',cv2.drawContours(img,[hull],0,(0,255,0), 3))
#cv2.imwrite('hola.png',thresh1)


I am trying to design an app similar to camscanner. For that, I have to take an image and then find the document in that. I started off with the code described here - http://opencvpython.blogspot.in/2012/06/sudoku-solver-part-2.html

I found the contours and the rectangular contour with max area should be the required document. For every contour, I am finding an approximate closed PolyDP. Of all the polyDP of size 4, the one with max area should be the required document. However, this method is not working.

The input image for the process is this

I tried to print the contour with max area and this resulted in this (Contour inside letter 'C')

Code:

img = cv2.imread('bounce.jpeg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5,5),0) 
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

def biggestRectangle(contours):
    biggest = None
    max_area = 0
    indexReturn = -1
    for index in range(len(contours)):
            i = contours[index]
            area = cv2.contourArea(i)
            if area > 100:
                    peri = cv2.arcLength(i,True)
                    approx = cv2.approxPolyDP(i,0.1*peri,True)
                    if area > max_area: #and len(approx)==4:
                            biggest = approx
                            max_area = area
                            indexReturn = index
    return indexReturn

indexReturn = biggestRectangle(contours)
cv2.imwrite('hola.png',cv2.drawContours(img, contours, indexReturn, (0,255,0)))

What is going wrong in this? Is there any other method by which I can capture the document in this picture?

解决方案

Try this : output image

import cv2
import numpy as np

img = cv2.imread('bounce.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
invGamma = 1.0 / 0.3
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")

# apply gamma correction using the lookup table
gray = cv2.LUT(gray, table)

ret,thresh1 = cv2.threshold(gray,80,255,cv2.THRESH_BINARY)

#thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
_, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

def biggestRectangle(contours):
    biggest = None
    max_area = 0
    indexReturn = -1
    for index in range(len(contours)):
            i = contours[index]
            area = cv2.contourArea(i)
            if area > 100:
                peri = cv2.arcLength(i,True)
                approx = cv2.approxPolyDP(i,0.1*peri,True)
                if area > max_area: #and len(approx)==4:
                        biggest = approx
                        max_area = area
                        indexReturn = index
    return indexReturn

indexReturn = biggestRectangle(contours)
hull = cv2.convexHull(contours[indexReturn])
cv2.imwrite('hola.png',cv2.drawContours(img, [hull], 0, (0,255,0),3))
#cv2.imwrite('hola.png',thresh1)

这篇关于如何从opencv中的图片中检测文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆