How to write code to identify the font and split it into separate pictures
This article mainly explains "how to write a code recognition frame to select a font and divide it into a separate picture". The explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to write a code recognition frame to select a font and divide it into a separate picture.
# juzicode.com/vx: orange code
Import os,sys,time,cv2
Import numpy as np
Dbg_is_show = False
Def show_img (win_name,img,wait_time=0,img_ratio=0.15,is_show=True):
If is_show is not True:
Return
Rows = img.shape [0]
Cols = img.shape [1]
Cv2.namedWindow (win_name, cv2.WINDOW_NORMAL) # cv2.WINDOW_AUTOSIZE)
Cv2.resizeWindow (win_name, (int (cols*img_ratio), int (rows*img_ratio)
Cv2.imshow (win_name,img)
Cv2.waitKey (wait_time)
If not os.path.exists ('out'):
Os.mkdir ('out')
Print ('juzicode.com/vx: orange code')
Print (cv2.__version__)
Img_src = cv2.imread ('src.jpg')
Print (img_src.shape)
Show_img ('img_src',img_src,is_show=dbg_is_show)
# get grayscale image
Img_b, img_g, img_r = cv2.split (img_src)
Show_img ('img_r',img_r,is_show=dbg_is_show)
Img_gray = cv2.bitwise_not (img_r)
Img_gray= cv2.medianBlur (img_gray,5)
# binarization
Thresh_bin,img_bin= cv2.threshold (img_gray,127,255,cv2.THRESH_BINARY)
Show_img ('img_bin',img_bin,is_show=dbg_is_show)
Kernel = cv2.getStructuringElement (cv2.MORPH_RECT, (3,3))
Img_eroded = cv2.erode (img_bin,kernel)
Show_img ('img_eroded',img_eroded)
Kernel = cv2.getStructuringElement (cv2.MORPH_RECT, (29,29))
Img_dilated = cv2.dilate (img_eroded,kernel)
Show_img ('img_dilated',img_dilated)
Res = cv2.findContours (img_dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Contours=res [1]
Print ('len (contours):', len (contours))
For i in range (0jinlen (contours)):
X, y, w, h = cv2.boundingRect (contours [I])
Print (iPaper len (contours [I]))
If len (contours [I])