Search code examples
pythonnumpyopencvimage-recognitionpython-tesseract

How could I crop image in exact location where numbers are situated?


I have my code prototype:

import cv2
import numpy as np

img = cv2.imread('/home/follia/Pictures/scan.jpg')

h, w, k = img.shape
M = cv2.getRotationMatrix2D((w / 2, h / 2), 15.5, 1)
img = cv2.warpAffine(img, M, (w, h))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200, apertureSize=3)

lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 80)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imshow("origin", img)
cv2.waitKey(0)

Original image: enter image description here and it return this image: image I get

And I need this image to be cropped and show only numbers: image I need

Could you please help me out, how could I cut this location? And then, how could I recognize numbers and extract it from image to text?


Solution

  • Try this:

    Basic idea of this solution is, get the contours of the image after performing threshold() and detect the biggest contour among contours.

    INPUT:

    CODE:

    import cv2
    image = cv2.imread("test.jpg", 1)
    h, w, k = image.shape
    M = cv2.getRotationMatrix2D((w / 2, h / 2), 15.5, 1)
    image = cv2.warpAffine(image, M, (w, h), cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, borderValue=(255, 255, 255))
    
    img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    threshold = 80
    cv2.threshold(img,threshold,255,cv2.THRESH_BINARY,img)
    cv2.bitwise_not(img,img)
    cv2.imshow("Result", img)
    cv2.waitKey(0)
    im2, contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    
    if len(contours) != 0:
        #find the biggest area
        c = max(contours, key = cv2.contourArea)
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
        crop_img = image[y:y + h, x:x + w]
        cv2.imshow("Result", crop_img)
        cv2.waitKey(0)
    
    cv2.imshow("Result", image)
    cv2.waitKey(0)
    

    OUTPUT: