Python+OpenCV實現(xiàn)信用卡數(shù)字識別的方法詳解
一、模板圖像處理
(1)灰度圖、二值圖轉化
template = cv2.imread('C:/Users/bwy/Desktop/number.png')
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
cv_show('template_gray', template_gray)
# 形成二值圖像,因為要做輪廓檢測
ret, template_thresh = cv2.threshold(template_gray, 127, 255, cv2.THRESH_BINARY_INV)
cv_show('template_thresh', template_thresh)結果如圖所示:


(2)進行輪廓提取接受參數(shù)為二值圖像,得到數(shù)字的信息,RETR_EXTERNAL 就是只是需要外輪廓,cv2.CHAIN_APPROX_SIMPLE只保留終點坐標。
template_contours, hierarchy = cv2.findContours(template_thresh,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(template,template_contours,-1,(0,0,255),2)
cv_show('template',template)-1:代表所的輪廓,我們這里畫出來10個輪廓。(可以用代碼驗證一下)
print(np.array(refCnts,-1,(0,0,255),3)
結果:10
結果如圖所示:

(3)我們需要將輪廓進行大小排序(我們拿到的數(shù)據(jù)模板不一定向我們前面所展示的從0-9按順序的,所以我們需要進行排序、resize。
def contours_sort(contours, method=0):
if method == 0:
contours = sorted(contours, key=lambda x: cv2.boundingRect(x)[0])
else:
contours = sorted(contours, key=lambda x: cv2.boundingRect(x)[0], reverse=True)
return contours我們調用函數(shù)#將輪廓排序,位置從小到大就是數(shù)字的信息。然后我們遍歷模板,使用cv2.boudingRect獲得輪廓的位置,提取位置對應的圖片,與數(shù)字結合構造成模板字典,dsize = (55, 88),統(tǒng)一大小。
dict_template = {}
for i, contour in enumerate(template_contours):
# 畫出其外接矩陣,獲得其位置信息
x, y, w, h = cv2.boundingRect(contour)
template_img = template_thresh[y:y + h, x:x + w]
# 使用cv2.resize變化模板的大小
template_img = cv2.resize(template_img, dsize)
cv_show('template_img{}'.format(i), template_img)
dict_template[i] = template_img 結果如圖所示:


。。。。。。。。。。

二、信用卡圖片預處理
(1)進行灰度值
card_gray = cv2.cvtColor(card, cv2.COLOR_BGR2GRAY)
cv_show('card_gray',card_gray)(2)形成二值圖像,因為要做輪廓檢測,解釋參數(shù):THRESH_OTSU會自動尋找合適的閾值,適合雙峰,需要閾值參數(shù)設置為零 二值化
card_thresh =cv2.threshold(card_gray,0,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)[1]
cv_show('card_thresh',card_thresh)結果如圖所示:

(3) 我們觀察一下圖片,我們識別圖片上的數(shù)字但也會存在黃框和紅框中的干擾,這時候我們可以想到前面所學到的形態(tài)學操作禮帽,閉運算...

先進行禮帽操作,突出更明亮的區(qū)域:
kernel=np.ones((9,3),np.uint8)
card_tophat=cv2.morphologyEx(card_gray,cv2.MORPH_TOPHAT,kernel)
cv_show('card_tophat',card_tophat)結果如圖:

(4)我們進行圖像的輪廓檢測只取外輪廓。在這個圖上有不同的區(qū)域,我們如何區(qū)分呢,我們可以用h的大小進行估計,這個數(shù)據(jù)根據(jù)項目而定

bankcard_contours, hierarchy = cv2.findContours(card_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
banck_card_cnts = []
draw_img = card.copy()
for i, contour in enumerate(bankcard_contours):
x, y, w, h = cv2.boundingRect(contour)
# 數(shù)字的x 坐標在 一定的位置范圍
if 0.5 * card_h < y < 0.6 * card_h:
banck_card_cnts.append((x, y, w, h))
draw_img = cv2.rectangle(draw_img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255),
thickness=2) # 畫出這個矩形,會在原圖上畫
cv_show_image('rectangle_contours_img', draw_img)結果如圖:

(5)模板匹配,讀出圖像。
for i, locs in enumerate(banck_card_cnts):
x, y, w, h = locs[:] # 保留了在原始圖像的位置信息
dst_img = card_thresh[y:y + h, x:x + w] # 獲得當前圖像的位置和區(qū)域
dst_img = cv2.resize(dst_img, dsize)
cv_show('rectangle_contours_img', dst_img)
tm_vals = {}
for number, temp_img in dict_template.items():
# 模板匹配,采用計算相關性系數(shù),值越大越相關
res = cv2.matchTemplate(dst_img, temp_img, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
tm_vals[number] = max_val
number_tm = max(tm_vals, key=tm_vals.get)
# 在圖像上畫出結果來
draw_img = cv2.rectangle(draw_img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255),
thickness=2)
cv2.putText(draw_img, str(number_tm), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.65,
color=(0, 0, 255), thickness=2)
cv_show_image('final_result', draw_img)結果如圖所示:




只是展示一部分(倒序輸出)

到此這篇關于Python+OpenCV實現(xiàn)信用卡數(shù)字識別的方法詳解的文章就介紹到這了,更多相關Python OpenCV信用卡數(shù)字識別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python機器學習算法庫scikit-learn學習之決策樹實現(xiàn)方法詳解
這篇文章主要介紹了Python機器學習算法庫scikit-learn學習之決策樹實現(xiàn)方法,結合實例形式分析了決策樹算法的原理及使用sklearn庫實現(xiàn)決策樹的相關操作技巧,需要的朋友可以參考下2019-07-07
Python對Tornado請求與響應的數(shù)據(jù)處理
這篇文章主要介紹了Python對Tornado請求與響應的數(shù)據(jù)處理,需要的朋友可以參考下2020-02-02
Python讀取Hive數(shù)據(jù)庫實現(xiàn)代碼詳解
這篇文章主要介紹了Python讀取Hive數(shù)據(jù)庫實現(xiàn)代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

