Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

stackoverflow ocr,cropping letters

import cv2
import numpy as np

#import image
image = cv2.imread('image.png')

#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
cv2.waitKey(0)

#binary
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
cv2.imshow('second', thresh)
cv2.waitKey(0)

#dilation
kernel = np.ones((1,1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)

#find contours
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y+h, x:x+w]

    # show ROI
    #cv2.imshow('segment no:'+str(i),roi)
    cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2)
    #cv2.waitKey(0)

    if w > 15 and h > 15:
        cv2.imwrite('roi{}.png'.format(i), roi)

cv2.imshow('marked areas',image)
cv2.waitKey(0)
Comment

PREVIOUS NEXT
Code Example
Python :: print [url_string for extension in extensionsToCheck if(extension in url_string)] 
Python :: queryset o que é 
Python :: python prime number 
Python :: sys executable juypter is incorrect visual code 
Python :: mad libs game prompt python 
Python :: access dynamicall to name attribute python 
Python :: compter des valeur consecutives en python 
Python :: command to update pip 
Shell :: how to check laptop serial number in ubuntu 
Shell :: how to check mongodb status in ubuntu 
Shell :: how to install obs on ubuntu 
Shell :: refusing to merge unrelated histories 
Shell :: ad sync powershell 
Shell :: how to restart nginx 
Shell :: git user.name user.email 
Shell :: install rest framework 
Shell :: ubuntu settings not opening 20.04 
Shell :: install zlib ubuntu 
Shell :: install pymysql 
Shell :: install wps ubuntu 20.04 multilanguage 
Shell :: start apache2 ubuntu 
Shell :: ubuntu list file by size 
Shell :: kill port linux 
Shell :: add git user and email 
Shell :: ubuntu install okular 
Shell :: auth install laravel 8 
Shell :: how to check if solidity is installed 
Shell :: notion ubuntu 
Shell :: Error loading MySQLdb module. 
Shell :: check logged in user git shell 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =