最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python+OpenCV實(shí)現(xiàn)信用卡數(shù)字識別的方法詳解

 更新時(shí)間:2022年09月18日 15:31:12   作者:泡泡怡  
這篇文章主要介紹了如何利用python?opencv實(shí)現(xiàn)信用卡數(shù)字識別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、模板圖像處理

(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會自動(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í)候我們可以想到前面所學(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è)矩形,會在原圖上畫
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ù)字識別的方法詳解的文章就介紹到這了,更多相關(guān)Python OpenCV信用卡數(shù)字識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

镇原县| 山阴县| 陇川县| 通州区| 洞头县| 漳浦县| 章丘市| 友谊县| 阿拉善盟| 江阴市| 阜南县| 仪征市| 阜新市| 湖南省| 神农架林区| 夏邑县| 东平县| 通城县| 曲阜市| 靖安县| 河津市| 荔浦县| 老河口市| 敖汉旗| 溧阳市| 张掖市| 简阳市| 安阳县| 阿拉善左旗| 肇东市| 类乌齐县| 景泰县| 辽阳市| 武冈市| 光泽县| 县级市| 洛扎县| 溆浦县| 安塞县| 确山县| 涞水县|