社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

newlearnershiv

newlearnershiv 最近创建的主题
newlearnershiv 最近回复了
6 年前
回复了 newlearnershiv 创建的主题 » python中的字符分割

可以使用“查找轮廓”并用方框将其绑定。

image = cv2.imread("filename") 
image = cv2.fastNlMeansDenoisingColored(image,None,10,10,7,21)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

res,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) #threshold 
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)) 

 dilated = cv2.dilate(thresh,kernel,iterations = 5) 

 val,contours, hierarchy = 
            cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) 

 coord = []
 for contour in contours:  
      [x,y,w,h] = cv2.boundingRect(contour)   
      if h>300 and w>300:   
          continue   
      if h<40 or w<40:   
          continue  
      coord.append((x,y,w,h)) 

 coord.sort(key=lambda tup:tup[0]) # if the image has only one sentence sort in one axis

 count = 0
 for cor in coord:
        [x,y,w,h] = cor
        t = image[y:y+h,x:x+w,:]
        cv2.imwrite(str(count)+".png",t)
  print("number of char in image:", count)