Сканер изображений отлично работает для одного изображения, но не для другого - PullRequest
0 голосов
/ 08 апреля 2020

Я пишу код для сканирования изображения (например, приложение CamScanner). Сначала я изменяю размер изображения, затем снимаю шум, применяю canny для извлечения краев, из краев я нахожу края документа, затем преобразую перспективу, используя эти края. Наконец я порождаю документ:

import cv2
import numpy as np

#function so that the np array of 4 points is in top-left, top-right, bottom right, bottom-left order
def order_points(pts):
    pts = pts.reshape((4,2))
    rect = np.zeros((4,2),dtype = np.float32)

    add = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(add)]
    rect[2] = pts[np.argmax(add)]

    diff = np.diff(pts,axis = 1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    return rect

img = cv2.imread('/Users/yuvanseth/Documents/VS/NLP_OCR/document_advanced.png', 0)   #read in the image
width = 1000
height = 1414
img = cv2.resize(img,(width,height)) #resizing according to A4 sheet aspect ratio

img_gauss = cv2.GaussianBlur(img,(5,5),0)  #blurring using a 5x5 matrix to reduce noise

kernel = np.ones((5,5),np.uint8)
img_gauss_open = cv2.morphologyEx(img_gauss, cv2.MORPH_OPEN, kernel) #opening performs erosion followed by dilation, to further reduce noise

edged = cv2.Canny(img_gauss_open,100,210)  #applying canny algorithm to return edges of an image

contours, _ = cv2.findContours(edged,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)  #return the contours as a list, with simple apprximation model
contours = sorted(contours,key = cv2.contourArea,reverse=True) #sort in descending order of area of contour

#the loop finds the boundary of the document 
for contour in contours:
    peri = cv2.arcLength(contour,True)
    approx = cv2.approxPolyDP(contour,0.04 * peri,True) #this function reduces contour to simple points

    # the first contour with 4 vertices is the document's boundary as it has the largest area of all rectangles
    if len(approx) == 4 :
        break
approx = order_points(approx) 

points = np.float32([[0,0],[width,0],[width,height],[0,height]])  #map to width x height target window

matrix = cv2.getPerspectiveTransform(approx,points)  #return the required matrix to map document to the required points
persp = cv2.warpPerspective(img,matrix,(width,height)) #get the top-view of the ducument using matrix

cv2.imshow("persp",persp)

scanned = cv2.adaptiveThreshold(persp, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 155, 10) #turning document binary
cv2.imshow("Scanned",scanned)

cv2.waitKey(0)
cv2.destroyAllWindows()

Мой код работает для базового c изображения, но не для изображения под углом (показывает пустую белую страницу)

Я думаю, что Это связано со сложными пороговыми значениями, которые определяют, какой тип контуров вы получаете от своего изображения. Но я не могу go изменить значения для каждого изображения, код должен быть общим для всех.

Любая помощь?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...