Python+OpenCV實現(xiàn)信用卡數(shù)字識別的方法詳解
一、模板圖像處理
(1)灰度圖、二值圖轉(zhuǎn)化
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)結(jié)果如圖所示:


(2)進行輪廓提取接受參數(shù)為二值圖像,得到數(shù)字的信息,RETR_EXTERNAL 就是只是需要外輪廓,cv2.CHAIN_APPROX_SIMPLE只保留終點坐標(biāo)。
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)
結(jié)果:10
結(jié)果如圖所示:

(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我們調(diào)用函數(shù)#將輪廓排序,位置從小到大就是數(shù)字的信息。然后我們遍歷模板,使用cv2.boudingRect獲得輪廓的位置,提取位置對應(yīng)的圖片,與數(shù)字結(jié)合構(gòu)造成模板字典,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 結(jié)果如圖所示:


。。。。。。。。。。

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

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

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

(4)我們進行圖像的輪廓檢測只取外輪廓。在這個圖上有不同的區(qū)域,我們?nèi)绾螀^(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 坐標(biāo)在 一定的位置范圍
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)結(jié)果如圖:

(5)模板匹配,讀出圖像。
for i, locs in enumerate(banck_card_cnts):
x, y, w, h = locs[:] # 保留了在原始圖像的位置信息
dst_img = card_thresh[y:y + h, x:x + w] # 獲得當(dāng)前圖像的位置和區(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():
# 模板匹配,采用計算相關(guān)性系數(shù),值越大越相關(guān)
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)
# 在圖像上畫出結(jié)果來
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)結(jié)果如圖所示:




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

到此這篇關(guān)于Python+OpenCV實現(xiàn)信用卡數(shù)字識別的方法詳解的文章就介紹到這了,更多相關(guān)Python OpenCV信用卡數(shù)字識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python中數(shù)據(jù)處理的方法總結(jié)及實現(xiàn)
數(shù)據(jù)增強作為前處理的關(guān)鍵步驟,在整個計算機視覺中有著具足輕重的地位。本文為大家總結(jié)了Python中數(shù)據(jù)處理的方法及實現(xiàn),需要的可以參考一下2022-09-09
python Django連接MySQL數(shù)據(jù)庫做增刪改查
本文寫的是python Django連接MySQL數(shù)據(jù)庫的步驟,提供增刪改查的代碼2013-11-11
python抓取豆瓣圖片并自動保存示例學(xué)習(xí)
python抓取豆瓣圖片并自動保存示例學(xué)習(xí),示例使用了beautifulsoup庫分析HTML代碼,beautifulsoup是一個HTML/XML解析器,可以用來做網(wǎng)頁爬蟲2014-01-01
使用python實現(xiàn)無需驗證碼免登錄12306搶票功能
隨著科技的發(fā)展,越來越多的人選擇通過網(wǎng)絡(luò)購買火車票,而12306作為中國鐵路客戶服務(wù)中心的官方網(wǎng)站,成為了人們購票的首選平臺,然而,在春運、暑運等高峰期,由于車票緊張,搶票成為了一項具有挑戰(zhàn)性的任務(wù),下面,我們將詳細介紹如何使用python實現(xiàn)無需驗證碼免登錄12306搶票2025-01-01
Python3實現(xiàn)發(fā)送QQ郵件功能(文本)
這篇文章主要為大家詳細介紹了Python3實現(xiàn)發(fā)送QQ郵件功能,文本方面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

