{"id":62172,"date":"2026-01-19T14:42:07","date_gmt":"2026-01-19T06:42:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/62172.html"},"modified":"2026-01-19T14:42:07","modified_gmt":"2026-01-19T06:42:07","slug":"%e8%ae%a1%e7%ae%97%e6%9c%ba%e8%a7%86%e8%a7%89opencv%e4%b9%8b%e5%9b%be%e7%89%87%e6%97%8b%e8%bd%ac%e6%a8%a1%e7%89%88%e5%8c%b9%e9%85%8d%e9%93%b6%e8%a1%8c%e5%8d%a1%e5%8f%b7%e7%9a%84%e8%af%86","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/62172.html","title":{"rendered":"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b"},"content":{"rendered":"<p>\u4e00\u3001\u56fe\u7247\u65cb\u8f6c<\/p>\n<p>&#039;&#039;&#039;\u56fe\u7247\u65cb\u8f6c&#039;&#039;&#039;<br \/>\nimport cv2<br \/>\nimport numpy as np<\/p>\n<p>#\u7b2c\u4e00\u79cd\u65b9\u6cd5\u7528numpy\u5b9e\u73b0<br \/>\nimg&#061; cv2.imread(&#039;&#039;)<br \/>\nrotated_img1&#061;np.rot90(img,k&#061;-1)#\u65cb\u8f6c90\u5ea6&#xff0c;-1\u662f\u987a\u65f6\u9488\u65cb\u8f6c&#xff0c;1\u4e3a\u9006\u65f6\u9488\u65cb\u8f6c<br \/>\nrotated_img2&#061;np.rot90(img,k&#061;1)<br \/>\ncv2.imshow(&#039;yuantu&#039;,img)<br \/>\ncv2.imshow(&#039;image1shun&#039;,rotated_img1)<br \/>\ncv2.imshow(&#039;image2ni&#039;,rotated_img2)<br \/>\ncv2.waitKey(0)<br \/>\ncv2.destroyAllWindows()<br \/>\n#\u7b2c\u4e8c\u79cd\u7528opencv<br \/>\nrotated_img&#061;cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)#\u987a\u65f6\u948890\u5ea6<br \/>\nrotated_img2&#061;cv2.rotate(img,cv2.ROTATE_90_COUNTERCLOCKWISE)#\u9006\u65f6\u948890\u5ea6<br \/>\nrotated_img3&#061;cv2.rotate(img,cv2.ROTATE_180)#\u65cb\u8f6c180\u5ea6<br \/>\ncv2.imshow(&#039;shun90&#039;,rotated_img)<br \/>\ncv2.imshow(&#039;ni90&#039;,rotated_img2)<br \/>\ncv2.imshow(&#039;180&#039;,rotated_img3)<br \/>\ncv2.waitKey(0) <\/p>\n<p>\u4e8c\u3001\u6a21\u7248\u5339\u914d<\/p>\n<p>&#039;&#039;&#039;\u6a21\u7248\u4e0e\u591a\u4e2a\u5bf9\u8c61\u5339\u914d&#039;&#039;&#039;<br \/>\nimport cv2<br \/>\nimport numpy as np<br \/>\nimg_rgb&#061;cv2.imread(&#039;&#039;)<br \/>\nimg_gray&#061;cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)<br \/>\ntemplate&#061;cv2.imread(&#039;&#039;)<br \/>\nh,w&#061;template.shape[:2]<br \/>\n#\u4f7f\u7528\u6a21\u7248\u5339\u914d\u65b9\u6cd5cv2.matchTemplate,\u8fdb\u884c\u6a21\u677f\u5339\u914d<br \/>\nres&#061;cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)<br \/>\n#\u8bbe\u5b9a\u5339\u914d\u9608\u503c<br \/>\nthreshold&#061;0.9<br \/>\n#\u83b7\u53d6\u5339\u914d\u7ed3\u679c\u4e2d\u6240\u6709\u7b26\u5408\u9608\u503c\u7684\u70b9\u5750\u6807<br \/>\nloc&#061;np.where(res &gt;&#061; threshold)<br \/>\n#print(loc)<br \/>\n#\u904d\u5386\u6240\u6709\u5339\u914d\u70b9<br \/>\nfor pt in zip(*loc[::-1]):<br \/>\n    #\u5728\u539f\u56fe\u4e0a\u7ed8\u5236\u5339\u914d\u533a\u57df\u7684\u77e9\u5f62\u6846<br \/>\n    cv2.rectangle(img_rgb,(pt[0]&#043;w,pt[1]&#043;h),(0,0,255),1)<\/p>\n<p>cv2.imshow(&#039;img&#039;,img_rgb)<br \/>\ncv2.waitKey(0) <\/p>\n<p>\u4e09\u3001\u6846\u9009\u56fe\u7247\u76ee\u6807<\/p>\n<p>\u5229\u7528\u4e0a\u9762\u56fe\u7247\u65cb\u8f6c\u548c\u6a21\u677f\u5339\u914d<\/p>\n<p>template&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"128\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064203-696dd23b781d4.png\" width=\"100\" \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u9700\u8981\u8fdb\u884c\u6846\u9009\u7684\u56fe\u7247&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"131\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064203-696dd23b84c1a.png\" width=\"140\" \/><\/p>\n<p>import cv2<br \/>\nimport numpy as np<\/p>\n<p>def pipei(img_rgb,template):<br \/>\n    img_gray&#061;cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)<br \/>\n    h,w&#061;template.shape[:2]<br \/>\n    res&#061;cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)<br \/>\n    threshold&#061;0.9<br \/>\n    loc&#061;np.where(res &gt;&#061; threshold)<br \/>\n    for pt in zip(*loc[::-1]):<br \/>\n        cv2.rectangle(img_rgb,pt,(pt[0]&#043;w,pt[1]&#043;h),(0,0,255),1)<br \/>\n    return img_rgb<\/p>\n<p>img_rgb&#061;cv2.imread(&#039;yuantu1.jpg&#039;)<br \/>\ntemplate&#061;cv2.imread(&#039;jiantou.jpg&#039;,0)<br \/>\ntemplate2&#061;np.rot90(template,k&#061;-1)<br \/>\ntemplate3&#061;np.rot90(template,k&#061;1)<\/p>\n<p>img_rgb&#061;pipei(img_rgb,template)<br \/>\nimg_rgb&#061;pipei(img_rgb,template2)<br \/>\nimg_rgb&#061;pipei(img_rgb,template3)<br \/>\ncv2.imshow(&#039;img_rgb&#039;,img_rgb)<br \/>\ncv2.waitKey(0) <\/p>\n<p>\u7ed3\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"914\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064203-696dd23b8b5ec.png\" width=\"1121\" \/><\/p>\n<p>\u56db\u3001\u94f6\u884c\u5361\u53f7\u8bc6\u522b<\/p>\n<p>1.\u5bfc\u5165\u6a21\u5757<\/p>\n<p>import numpy as np<br \/>\nimport argparse<br \/>\nimport cv2<br \/>\nimport myutils <\/p>\n<p>2.\u8bbe\u7f6e\u53c2\u6570<\/p>\n<p>\u5728\u4e0a\u4e00\u7bc7\u89c6\u89c9\u535a\u5ba2\u4e2d\u63d0\u5230\u8fc7\u7684\u4f20\u5165\u53c2\u6570\u7684\u65b9\u6cd5\u8fd9\u91cc\u6211\u4eec\u8bbe\u7f6e\u4e24\u4e2a\u53c2\u6570&#xff0c;\u4e00\u4e2a\u662f\u4f20\u5165\u7684\u7167\u7247&#xff0c;\u4e00\u4e2a\u662f\u8bc6\u522b\u6a21\u7248<\/p>\n<p>\u00a0 \u4f20\u5165\u7684\u7167\u7247&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"167\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064204-696dd23c56977.png\" width=\"153\" \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u8bc6\u522b\u6a21\u677f&#xff1a;\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"167\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064204-696dd23c616a5.png\" width=\"186\" \/><\/p>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u8fd9\u91cc\u6211\u4eec\u8bbe\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7528\u6765\u5c55\u793a\u56fe\u7247\u7684<\/p>\n<p>#\u8bbe\u7f6e\u53c2\u6570<br \/>\nap&#061;argparse.ArgumentParser()<br \/>\nap.add_argument(&#039;-i&#039;,&#039;&#8211;image&#039;,required&#061;True,help&#061;&#039;path to input image&#039;)<br \/>\nap.add_argument(&#039;-t&#039;,&#039;&#8211;template&#039;,required&#061;True,help&#061;&#039;path to template OCA-A image&#039;)<br \/>\nargs&#061;vars(ap.parse_args())<br \/>\ndef cv_show(name,img):<br \/>\n    cv2.imshow(name,img)<br \/>\n    cv2.waitKey(0) <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"364\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064204-696dd23c67a08.png\" width=\"931\" \/><\/p>\n<p>3.\u8f6e\u5ed3\u83b7\u53d6<\/p>\n<p>\u5bf9\u6a21\u677f\u4e2d\u6bcf\u6b4c\u6570\u5b57\u8f6e\u5ed3\u8fdb\u884c\u83b7\u53d6&#xff0c;\u68c0\u6d4b\u5230\u8f6e\u5ed3&#xff0c;ref\u8f93\u5165\u56fe\u50cf&#xff08;\u5fc5\u987b\u662f\u4e8c\u503c\u56fe&#xff09;&#xff0c;<\/p>\n<p>cv2.RETR_EXTERNAL&#xff08;\u8f6e\u5ed3\u68c0\u7d22\u6a21\u5f0f&#xff1a;\u53ea\u68c0\u6d4b\u6700\u5916\u5c42\u8f6e\u5ed3&#xff09;&#xff0c;<\/p>\n<p>cv2.CHAIN_APPROX_SIMPLE &#xff08;\u8f6e\u5ed3\u8fd1\u4f3c\u65b9\u6cd5&#xff1a;\u538b\u7f29\u6c34\u5e73\/\u5782\u76f4\/\u5bf9\u89d2\u7ebf\u65b9\u5411\u70b9&#xff09;\u3002<\/p>\n<p>&#039;&#039;&#039;&#8212;&#8212;&#8212;&#8212;\u6bcd\u7248\u56fe\u50cf\u4e2d\u6570\u5b57\u7684\u5b9a\u4f4d\u5904\u7406&#8212;&#8212;&#8212;&#8212;-&#039;&#039;&#039;<br \/>\nimg&#061;cv2.imread(args[&#039;template&#039;])#\u8bfb\u53d6\u6a21\u677f\u56fe<br \/>\ncv_show(&#039;img&#039;,img)<br \/>\nref&#061;cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#\u8f6c\u4e3a\u7070\u5ea6\u56fe<br \/>\ncv_show(&#039;ref&#039;,ref)<br \/>\nref&#061;cv2.threshold(ref,10,255,cv2.THRESH_BINARY_INV)[1]#\u4e8c\u503c\u5316\u5e76\u53cd\u8272&#xff0c;\u8fbe\u5230\u9ed1\u5e95\u767d\u5b57\u6548\u679c<br \/>\ncv_show(&#039;ref&#039;,ref)<\/p>\n<p>#\u8f6e\u5ed3\u68c0\u6d4b\u4e0e\u6392\u5e8f<br \/>\n_,refCnts,hierarchy&#061;cv2.findContours(ref,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)#\u68c0\u6d4b\u8f6e\u5ed3<br \/>\ncv2.drawContours(img,refCnts,-1,(0,0,255),3)#\u7ed8\u5236\u8f6e\u5ed3<br \/>\ncv_show(&#039;refCnts&#039;,img)<br \/>\nrefCnts&#061;myutils.sort_contours(refCnts,method&#061;&#039;left-to-right&#039;)[0]#\u4ece\u5de6\u5230\u53f3\u6392\u5217<\/p>\n<p>digits&#061;{}#\u5b57\u5178\u5b58\u50a8\u6570\u5b57\u6a21\u677f<br \/>\nfor (i,c) in enumerate(refCnts):<br \/>\n    (x,y,w,h)&#061;cv2.boundingRect(c)#\u83b7\u53d6\u8f6e\u5ed3\u5916\u63a5\u77e9\u5f62<br \/>\n    roi&#061;ref[y:y&#043;h,x:x&#043;w]#\u88c1\u526a\u51fa\u5355\u4e2a\u6570\u5b57<br \/>\n    roi&#061;cv2.resize(roi,(57,88))#\u7edf\u4e00\u5c3a\u5bf8\u4e3a57&#215;88\u50cf\u7d20<br \/>\n    cv_show(&#039;ro &#039;,roi)<br \/>\n    digits[i]&#061;roi#\u6309\u7167\u987a\u5e8f\u5b58\u5165\u5b57\u5178&#xff0c;0\u2014\u2014&gt;&#039;0&#039;,1\u2014\u2014&gt;&#039;1&#039;\u2026\u2026<br \/>\nprint(digits)#\u6253\u5370\u5b57\u5178:{0:\u6570\u5b570\u7684\u77e9\u9635&#xff0c;1::\u6570\u5b571\u7684\u77e9\u9635&#xff0c;\u2026\u2026&#xff0c;9:\u6570\u5b579\u7684\u77e9\u9635<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"472\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064204-696dd23c84341.png\" width=\"1195\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"231\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064204-696dd23cc94b3.png\" width=\"1196\" \/><\/p>\n<p>\u5c060-9\u50cf\u7d20\u77e9\u9635\u5bf9\u5e94\u5230\u5177\u4f53\u7684\u6570\u5b57&#xff0c;\u6784\u9020\u4e3a\u5b57\u5178\u5b58\u653e\u5728digists\u5f53\u4e2d<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"699\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064204-696dd23cf417c.png\" width=\"1282\" \/><\/p>\n<p>4.\u94f6\u884c\u5361\u56fe\u50cf\u5904\u7406<\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u9876\u5e3d&#xff0c;\u5ffd\u7565\u6389\u5361\u4e0a\u4e00\u4e9b\u5c0f\u7ec6\u8282&#xff0c;\u51f8\u663e\u6570\u5b57<\/p>\n<p>&#039;&#039;&#039;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;\u4fe1\u7528\u5361\u7684\u56fe\u50cf\u5904\u7406&#8212;&#8212;&#8212;&#8211;&#039;&#039;&#039;<br \/>\n#\u8bfb\u53d6\u8f93\u5165\u7684\u56fe\u50cf&#xff0c;\u9884\u5904\u7406<br \/>\nimage&#061;cv2.imread(r&#039;card1.png&#039;)<br \/>\ncv_show(&#039;image&#039;,image)<br \/>\nimage&#061;myutils.resize(image,width&#061;300)#\u8bbe\u7f6e\u56fe\u50cf\u5927\u5c0f<br \/>\ngray&#061;cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)<br \/>\ncv_show(&#039;gray&#039;,gray)<\/p>\n<p>#\u9876\u5e3d\u5904\u7406&#xff0c;\u7a81\u51fa\u56fe\u50cf\u4e2d\u7684\u4eae\u7ec6\u8282&#xff0c;\u6e05\u695a\u80cc\u666f\u56fe&#xff0c;\u56e0\u4e3a\u80cc\u666f\u56fe\u989c\u8272\u53d8\u5316\u5c0f&#xff0c;\u4e0d\u88ab\u8150\u8680\u6389<br \/>\nrectKernel&#061;cv2.getStructuringElement(cv2.MORPH_RECT,(9,3))<br \/>\nsqKernel&#061;cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))<br \/>\ntophat&#061;cv2.morphologyEx(gray,cv2.MORPH_TOPHAT,rectKernel)<br \/>\n# open&#061;cv2.morphologyEx(gray,cv2.MORPH_OPEN,rectKernel)<br \/>\n# cv_show(&#039;open&#039;,open)<br \/>\ncv_show(&#039;tophat&#039;,tophat) <\/p>\n<p>\u8bfb\u53d6\u8f93\u5165\u7684\u56fe\u50cf&#xff0c;\u4e5f\u5c31\u662fcard1.png&#xff0c;\u7070\u5ea6\u56fe\u5904\u7406&#xff0c;\u5904\u7406\u6389\u5361\u80cc\u666f\u7684\u6742\u8d28<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"911\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064205-696dd23d51619.png\" width=\"876\" \/><\/p>\n<p>5.\u627e\u6570\u5b57\u8fb9\u6846<\/p>\n<p>\u8fd9\u5757\u662f\u4e3b\u8981\u6838\u5fc3&#xff0c;\u5982\u679c\u6211\u4eec\u5904\u7406\u56fe\u7247\u4e0d\u5f53\u5c31\u4f1a\u5f71\u54cd\u6211\u4eec\u7684\u8f93\u51fa\u7ed3\u679c<\/p>\n<p>&#039;&#039;&#039;&#8212;&#8212;&#8212;&#8212;-\u627e\u5230\u6570\u5b57\u8fb9\u6846&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#039;&#039;&#039;<br \/>\n#\u901a\u8fc7\u95ed\u64cd\u4f5c&#xff08;\u5148\u81a8\u80c0\u540e\u8150\u8680&#xff09;\u5c06\u6570\u5b57\u8fde\u5728\u4e00\u8d77<br \/>\nclosex&#061;cv2.morphologyEx(tophat,cv2.MORPH_CLOSE,rectKernel)<br \/>\ncv_show(&#039;close1&#039;,closex)<br \/>\n#Tjresh_otsu\u4f1a\u81ea\u52a8\u5bfb\u627e\u5408\u9002\u7684\u9608\u503c&#xff0c;\u9002\u5408\u53cc\u5cf0&#xff0c;\u9700\u8981\u628a\u9608\u503c\u53c2\u6570\u8bbe\u7f6e\u4e3a0<br \/>\nthresh&#061;cv2.threshold(closex,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]<br \/>\ncv_show(&#039;thresh&#039;,thresh)<br \/>\n#\u95ed\u64cd\u4f5c<br \/>\nthresh&#061;cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,sqKernel)<br \/>\ncv_show(&#039;close2&#039;,thresh)<br \/>\n#\u8ba1\u7b97\u8f6e\u5ed3<br \/>\n_,cnts,h&#061;cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)<br \/>\ncnts_img&#061;image.copy()<br \/>\ncv2.drawContours(cnts_img,cnts,-1,(0,0,255),3)<br \/>\ncv_show(&#039;cnts_img&#039;,cnts_img)<br \/>\n#\u904d\u5386\u8f6e\u5ed3&#xff0c;\u627e\u5230\u6570\u5b57\u90e8\u5206\u50cf\u7d20\u533a\u57df<br \/>\nlocs&#061;[]<br \/>\nfor c in cnts:<br \/>\n    (x,y,w,h)&#061;cv2.boundingRect(c)<br \/>\n    ar&#061;w \/ float(h)<br \/>\n    #\u9009\u62e9\u5408\u9002\u7684\u533a\u57df&#xff0c;\u6839\u636e\u5b9e\u9645\u4efb\u52a1<br \/>\n    if 2.5 &lt; ar &lt;4.0:<br \/>\n        if (40&lt;w&lt;55) and (10&lt;h&lt;20):<br \/>\n            locs.append((x,y,w,h))<br \/>\n#\u5c06\u7b26\u5408\u7684\u8f6e\u5ed3\u4ece\u5de6\u5230\u53f3\u6392\u5217<br \/>\nlocs&#061;sorted(locs,key&#061;lambda x: x[0])<br \/>\nprint(locs) <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"648\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064205-696dd23da0cfb.png\" width=\"889\" \/><\/p>\n<p>\u8fd9\u91cc\u7b5b\u9009\u51fa\u6211\u4eec\u9700\u8981\u7684\u8f6e\u5ed3&#xff0c;\u957f\u5bbd\u7b49\u662f\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4f30\u8ba1\u7684\u53ef\u4ee5\u591a\u6b21\u4fee\u6539\u7b5b\u9009\u5230\u5bf9\u5e94\u7684\u5361\u53f7\u8f6e\u5ed3&#xff0c;\u8fd9\u91cc\u6211\u4eec\u5f97\u5230\u56db\u4e2a\u8fb9\u6846\u662f\u56e0\u4e3a&#xff0c;\u94f6\u884c\u5361\u53f7\u662f\u56db\u5757\u6570\u5b57\u5206\u5f00\u7684<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"37\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064205-696dd23ddb144.png\" width=\"787\" \/><\/p>\n<p>6.\u904d\u5386\u8f6e\u5ed3\u4e2d\u5404\u4e2a\u6570\u5b57<\/p>\n<p>\u4e09\u4e2a\u5faa\u73af&#xff1a;<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u5faa\u73af\u662f\u904d\u5386\u627e\u5230\u7684\u6bcf\u4e2a\u6570\u5b57\u7ec4<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u5faa\u73af&#xff0c;\u904d\u5386\u7ec4\u5185\u7684\u6bcf\u4e2a\u6570\u5b57<\/li>\n<li>\u7b2c\u4e09\u4e2a\u5faa\u73af\u662f\u4e0e0-9\u6a21\u677f\u9010\u4e00\u5339\u914d<\/li>\n<\/ul>\n<p>output&#061;[]<br \/>\n# \u904d\u5386\u6bcf\u4e00\u4e2a\u8f6e\u5ed3\u4e2d\u7684\u6570\u5b57<br \/>\nfor (gx,gy,gw,gh) in locs:<br \/>\n    groupoutput&#061;[]<br \/>\n    group&#061;gray[gy-5:gy&#043;gh&#043;5,gx-5:gx&#043;gw&#043;5]#\u6269\u5c55\u8fb9\u754c&#xff0c;\u907f\u514d\u8fb9\u7f18\u5207\u5272<br \/>\n    cv_show(&#039;group&#039;,group)<br \/>\n    #\u9884\u5904\u7406<br \/>\n    group&#061;cv2.threshold(group,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]#\u81ea\u52a8\u4e8c\u503c\u5316<br \/>\n    cv_show(&#039;group&#039;,group)<br \/>\n    #\u8ba1\u7b97\u6bcf\u4e00\u7ec4\u7684\u8f6e\u5ed3<br \/>\n    group_,digitscnts,hierarchy&#061;cv2.findContours(group.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)#\u5728\u4e8c\u503c\u5316\u7684\u6bcf\u4e2agroup\u4e2d\u627e\u6bcf\u4e2a\u6570\u5b57\u7684\u8f6e\u5ed3<br \/>\n    digitscnts&#061;myutils.sort_contours(digitscnts,method&#061;&#039;left-to-right&#039;)[0]#\u6392\u5e8f<br \/>\n    #\u8ba1\u7b97\u6bcf\u4e00\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u503c<br \/>\n    for c in digitscnts:<br \/>\n        #\u627e\u5230\u5f53\u524d\u6570\u503c\u7684\u8f6e\u5ed3&#xff0c;resize\u6210\u5408\u9002\u7684\u5927\u5c0f<br \/>\n        (x,y,w,h)&#061;cv2.boundingRect(c)<br \/>\n        roi&#061;group[y:y&#043;h,x:x&#043;w]#\u88c1\u526a\u51fa\u5355\u4e2a\u6570\u5b57<br \/>\n        roi&#061;cv2.resize(roi,(57,88))#\u7edf\u4e00\u5230\u6a21\u677f\u5c3a\u5bf8<br \/>\n        cv_show(&#039;roi&#039;,roi)<br \/>\n        &#039;&#039;&#039;&#8212;&#8211;\u4f7f\u7528\u6a21\u7248\u5339\u914d&#xff0c;\u8ba1\u7b97\u5339\u914d\u7684\u5f97\u5206&#039;&#039;&#039;<br \/>\n        scores&#061;[]<br \/>\n        #\u5728\u6a21\u7248\u4e2d\u8ba1\u7b97\u6bcf\u4e00\u4e2a\u5f97\u5206<br \/>\n        for (digit,digitroi) in digits.items():<br \/>\n            #\u6a21\u7248\u5339\u914d<br \/>\n            result&#061;cv2.matchTemplate(roi,digitroi,cv2.TM_CCORR)<br \/>\n            (_,score,_,_)&#061;cv2.minMaxLoc(result)<br \/>\n            scores.append(score)<br \/>\n        #\u5f97\u5230\u6700\u5408\u9002\u7684\u6570\u5b57<br \/>\n        groupoutput.append(str(np.argmax(scores)))<br \/>\n    #\u753b\u56fe<br \/>\n    cv2.rectangle(image,(gx-5,gy-5),(gx&#043;gw&#043;5,gy&#043;gh&#043;5),(0,0,255),1)<br \/>\n    # cv2.putText()\u662fopencv\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570&#xff0c;\u7528\u4e8e\u5728\u56fe\u50cf\u4e0a\u6dfb\u52a0\u6587\u672c<br \/>\n    cv2.putText(image,&#039;&#039;.join(groupoutput),(gx,gy-15),cv2.FONT_HERSHEY_SIMPLEX,0.65,(0,0,255),2)<br \/>\n    output.extend(groupoutput)#\u5f97\u5230\u7ed3\u679c&#xff0c;\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u672b\u5c3e <\/p>\n<p>7.\u6253\u5370\u7ed3\u679c<\/p>\n<p>#\u6253\u5370\u7ed3\u679c<br \/>\nprint(&#039;credit card type:{}&#039;.format(FIRST_NUMBER[output[0]]))<br \/>\nprint(&#039;credit card #:{}&#039;.format(&#039;&#039;.join(output)))<br \/>\ncv2.imshow(&#039;image&#039;,image)<br \/>\ncv2.waitKey(0) <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"497\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064205-696dd23dea347.png\" width=\"695\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u56fe\u7247\u65cb\u8f6c<br \/>\n\u56fe\u7247\u65cb\u8f6c<br \/>\nimport cv2<br \/>\nimport numpy as np#\u7b2c\u4e00\u79cd\u65b9\u6cd5\u7528numpy\u5b9e\u73b0<br \/>\nimg cv2.imread()<br \/>\nrotated_img1np.rot90(img,k-1)#\u65cb\u8f6c90\u5ea6&#xff0c;-1\u662f\u987a\u65f6\u9488\u65cb\u8f6c&#xff0c;1\u4e3a\u9006\u65f6\u9488\u65cb\u8f6c<br \/>\nrotated_img2np.rot90(img,k1)<br \/>\ncv2.imshow(yuantu,img)<br \/>\ncv2.imshow(image1shun,rotated_img1)<br \/>\ncv2.imshow(<\/p>\n","protected":false},"author":2,"featured_media":62159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1460,50,371,523],"topic":[],"class_list":["post-62172","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-opencv","tag-50","tag-371","tag-523"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/62172.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u56fe\u7247\u65cb\u8f6c \u56fe\u7247\u65cb\u8f6c import cv2 import numpy as np#\u7b2c\u4e00\u79cd\u65b9\u6cd5\u7528numpy\u5b9e\u73b0 img cv2.imread() rotated_img1np.rot90(img,k-1)#\u65cb\u8f6c90\u5ea6&#xff0c;-1\u662f\u987a\u65f6\u9488\u65cb\u8f6c&#xff0c;1\u4e3a\u9006\u65f6\u9488\u65cb\u8f6c rotated_img2np.rot90(img,k1) cv2.imshow(yuantu,img) cv2.imshow(image1shun,rotated_img1) cv2.imshow(\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/62172.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-19T06:42:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064203-696dd23b781d4.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62172.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/62172.html\",\"name\":\"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-19T06:42:07+00:00\",\"dateModified\":\"2026-01-19T06:42:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/62172.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/62172.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62172.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/62172.html","og_locale":"zh_CN","og_type":"article","og_title":"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u56fe\u7247\u65cb\u8f6c \u56fe\u7247\u65cb\u8f6c import cv2 import numpy as np#\u7b2c\u4e00\u79cd\u65b9\u6cd5\u7528numpy\u5b9e\u73b0 img cv2.imread() rotated_img1np.rot90(img,k-1)#\u65cb\u8f6c90\u5ea6&#xff0c;-1\u662f\u987a\u65f6\u9488\u65cb\u8f6c&#xff0c;1\u4e3a\u9006\u65f6\u9488\u65cb\u8f6c rotated_img2np.rot90(img,k1) cv2.imshow(yuantu,img) cv2.imshow(image1shun,rotated_img1) cv2.imshow(","og_url":"https:\/\/www.wsisp.com\/helps\/62172.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-19T06:42:07+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119064203-696dd23b781d4.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/62172.html","url":"https:\/\/www.wsisp.com\/helps\/62172.html","name":"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-19T06:42:07+00:00","dateModified":"2026-01-19T06:42:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/62172.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/62172.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/62172.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u8ba1\u7b97\u673a\u89c6\u89c9opencv\u4e4b\u56fe\u7247\u65cb\u8f6c&amp;\u6a21\u7248\u5339\u914d&amp;\u94f6\u884c\u5361\u53f7\u7684\u8bc6\u522b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=62172"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62172\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/62159"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=62172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=62172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=62172"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=62172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}