python實(shí)現(xiàn)圖片中文字分割效果

本文實(shí)例為大家分享了python實(shí)現(xiàn)圖片中文字分割的具體代碼,具體內(nèi)容如下
1、原始圖片(包含數(shù)字):
結(jié)果圖:
2、原始圖片(包含文字):
結(jié)果圖:
3、代碼如下:
















import cv2import numpy as nppath = 'test.jpg'root = 'output\\'# 圖像resizedsize = 36img = cv2.imread(path)data = np.array(img)height = data.shape[0]width = data.shape[1]# 設(shè)置最小的文字像素高度min_val = 10start_i = -1end_i = -1# 存放每行的起止坐標(biāo)rowinfo = []# 行分割for i in range(height):# 行中有字相關(guān)信息if (not data[i].all()):end_i = iif(start_i < 0):start_i = ipass# 行中無字相關(guān)信息elif (data[i].all() and start_i >= 0):if(end_i - start_i >= min_val):rowinfo.append((start_i, end_i))passstart_i, end_i = -1, -1print(rowinfo)# 列分割start_j = -1end_j = -1# 最小文字像素寬度min_val_word = 5# 分割后保存編號number = 0for start, end in rowinfo:for j in range(width):# 列中有字相關(guān)信息if(not data[start: end, j].all()):end_j = jif(start_j < 0):start_j = jpass# 列中無字信息elif(data[start: end, j].all() and start_j >= 0):if(end_j - start_j >= min_val_word):img = data[start:end, start_j: end_j]im2save = cv2.resize(img, (dsize, dsize)) #歸一化處理cv2.imwrite(root + '%d.jpg' % number, im2save)number += 1passstart_j, end_j = -1, -1
「?? 感謝大家」
如果你覺得這篇內(nèi)容對你挺有有幫助的話:
點(diǎn)贊支持下吧,讓更多的人也能看到這篇內(nèi)容(收藏不點(diǎn)贊,都是耍流氓 -_-) 歡迎在留言區(qū)與我分享你的想法,也歡迎你在留言區(qū)記錄你的思考過程。 覺得不錯(cuò)的話,也可以閱讀近期梳理的文章(感謝鼓勵(lì)與支持??????): 教你用python實(shí)現(xiàn)抖音上的頭像特效合成 python實(shí)現(xiàn)單張圖像拼接與批量圖片拼接 銀行卡號碼校驗(yàn)算法(Luhn算法,又叫模10算法) python實(shí)現(xiàn)橫向拼接圖片 小程序云開發(fā)資源的管理 教你用python進(jìn)行數(shù)字化妝,可愛至極 加速Python列表和字典,讓你代碼更加高效 python如何通過pyqt5實(shí)現(xiàn)進(jìn)度條 通俗易懂之最小二乘法(附matlab和python例子實(shí)現(xiàn)) 匯總超全的Matplotlib可視化最有價(jià)值的 50 個(gè)圖表(附完整 Python 源代碼)(一) 教你用Python制作實(shí)現(xiàn)自定義字符大小的簡易小說閱讀器
老鐵,三連支持一下,好嗎?↓↓↓


點(diǎn)分享

點(diǎn)點(diǎn)贊

點(diǎn)在
評論
圖片
表情





