Python+OpenCV實(shí)現(xiàn)信用卡數(shù)字識(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) # 形成二值圖像,因?yàn)橐鲚喞獧z測 ret, template_thresh = cv2.threshold(template_gray, 127, 255, cv2.THRESH_BINARY_INV) cv_show('template_thresh', template_thresh)
結(jié)果如圖所示:
(2)進(jìn)行輪廓提取接受參數(shù)為二值圖像,得到數(shù)字的信息,RETR_EXTERNAL 就是只是需要外輪廓,cv2.CHAIN_APPROX_SIMPLE只保留終點(diǎn)坐標(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個(gè)輪廓。(可以用代碼驗(yàn)證一下)
print(np.array(refCnts,-1,(0,0,255),3)
結(jié)果:10
結(jié)果如圖所示:
(3)我們需要將輪廓進(jìn)行大小排序(我們拿到的數(shù)據(jù)模板不一定向我們前面所展示的從0-9按順序的,所以我們需要進(jìn)行排序、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)進(jìn)行灰度值
card_gray = cv2.cvtColor(card, cv2.COLOR_BGR2GRAY) cv_show('card_gray',card_gray)
(2)形成二值圖像,因?yàn)橐鲚喞獧z測,解釋參數(shù):THRESH_OTSU會(huì)自動(dòng)尋找合適的閾值,適合雙峰,需要閾值參數(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í)別圖片上的數(shù)字但也會(huì)存在黃框和紅框中的干擾,這時(shí)候我們可以想到前面所學(xué)到的形態(tài)學(xué)操作禮帽,閉運(yùn)算...
先進(jìn)行禮帽操作,突出更明亮的區(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)我們進(jìn)行圖像的輪廓檢測只取外輪廓。在這個(gè)圖上有不同的區(qū)域,我們?nèi)绾螀^(qū)分呢,我們可以用h的大小進(jìn)行估計(jì),這個(gè)數(shù)據(jù)根據(jù)項(xiàng)目而定
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) # 畫出這個(gè)矩形,會(huì)在原圖上畫 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(): # 模板匹配,采用計(jì)算相關(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實(shí)現(xiàn)信用卡數(shù)字識(shí)別的方法詳解的文章就介紹到這了,更多相關(guān)Python OpenCV信用卡數(shù)字識(shí)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python機(jī)器學(xué)習(xí)算法庫scikit-learn學(xué)習(xí)之決策樹實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)算法庫scikit-learn學(xué)習(xí)之決策樹實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了決策樹算法的原理及使用sklearn庫實(shí)現(xiàn)決策樹的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07Python對Tornado請求與響應(yīng)的數(shù)據(jù)處理
這篇文章主要介紹了Python對Tornado請求與響應(yīng)的數(shù)據(jù)處理,需要的朋友可以參考下2020-02-02Python讀取Hive數(shù)據(jù)庫實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Python讀取Hive數(shù)據(jù)庫實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03Python操作MySQL簡單實(shí)現(xiàn)方法
這篇文章主要介紹了Python操作MySQL簡單實(shí)現(xiàn)方法,通過一個(gè)簡單的實(shí)例講述了Python針對mysql數(shù)據(jù)庫的增刪改查技巧,需要的朋友可以參考下2015-01-01關(guān)于Python時(shí)間日期常見的一些操作方法
Python的datetime模塊是處理日期和時(shí)間的強(qiáng)大工具,datetime類可以獲取當(dāng)前時(shí)間、指定日期、計(jì)算時(shí)間差、訪問時(shí)間屬性及格式化時(shí)間,這些功能使得在Python中進(jìn)行時(shí)間日期處理變得簡單高效,需要的朋友可以參考下2024-09-09