How to extract only characters from image?

后端 未结 1 1744
萌比男神i
萌比男神i 2021-01-13 01:38

I have this type of image from that I only want to extract the characters.

After binarization, I am getting this image

img = cv2.imread(\'t         


        
1条回答
  •  一向
    一向 (楼主)
    2021-01-13 02:35

    Not so difficult...

    import cv2
    
    img = cv2.imread('img.jpg')
    
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imshow('gray', gray)
    
    ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
    cv2.imshow('thresh', thresh)
    
    im2, ctrs, hier = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
    
    for i, ctr in enumerate(sorted_ctrs):
        x, y, w, h = cv2.boundingRect(ctr)
    
        roi = img[y:y + h, x:x + w]
    
        area = w*h
    
        if 250 < area < 900:
            rect = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.imshow('rect', rect)
    
    cv2.waitKey(0)
    

    Result

    You can tweak the code like you want (here it can save ROI using original image; for eventually OCR recognition you have to save them in binary format - better methods than sorting by area are available)

    Source: Extract ROI from image with Python and OpenCV and some of my knowledge.

    Just kidding, take a look at my questions/answers.

    0 讨论(0)
提交回复
热议问题