Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenCV to realize the function of Standard digit recognition

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use OpenCV to achieve standard digit recognition. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Import sysimport numpy as npimport cv2 im = cv2.imread ('t.png') im3 = im.copy () gray = cv2.cvtColor (im,cv2.COLOR_BGR2GRAY) # convert to grayscale image before you can use image thresholding thresh = cv2.adaptiveThreshold (gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY 11) # Adaptive thresholding # Now finding Contours # image,contours,hierarchy = cv2.findContours (thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # Edge search The number box was found, but there was a miscalculation that samples = np.empty ((0900)) # takes all the pixels of each recognized number as a feature Responses = [] # labelkeys = [i for i in range (48 58)] # 48-58 is ASCII code count = 0for cnt in contours: if cv2.contourArea (cnt) > 80: # use the edge area to filter the smaller edge box [x if h] = cv2.boundingRect (cnt) edge h > 25 edge h

< 30: #使用高过滤小框和大框 count+=1 cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2) roi = thresh[y:y+h,x:x+w] roismall = cv2.resize(roi,(30,30)) cv2.imshow('norm',im) key = cv2.waitKey(0) if key == 27: # (escape to quit) sys.exit() elif key in keys: responses.append(int(chr(key))) sample = roismall.reshape((1,900)) samples = np.append(samples,sample,0) if count == 100: #过滤一下过多边缘框,后期可能会尝试极大抑制 breakresponses = np.array(responses,np.float32)responses = responses.reshape((responses.size,1))print ("training complete") np.savetxt('generalsamples.data',samples)np.savetxt('generalresponses.data',responses)#cv2.waitKey()cv2.destroyAllWindows() 训练数据为:

The test data are:

Use the ML package that comes with openCV, the KNearest algorithm

Import sysimport cv2import numpy as np # training part # samples = np.loadtxt ('generalsamples.data',np.float32) responses = np.loadtxt (' generalresponses.data',np.float32) responses = responses.reshape ((responses.size,1)) model = cv2.ml.KNearest_create () model.train (samples,cv2.ml.ROW_SAMPLE Responses) def getNum (path): im = cv2.imread (path) out = np.zeros (im.shape,np.uint8) gray = cv2.cvtColor (im) Cv2.COLOR_BGR2GRAY) # preprocess for i in range (gray.__len__ ()): for j in range (gray [0]. _ _ len__ ()): if gray [I] [j] = 0: gray [I] [j] = = 255else: gray [I] [j] = 0 thresh = cv2.adaptiveThreshold (gray,255,1) 1) image,contours,hierarchy = cv2.findContours (thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) count = 0 numbers = [] for cnt in contours: if cv2.contourArea (cnt) > 80: [cv2.boundingRect (cnt) if h > 25: cv2.rectangle (im, (xmemy), (xmemwrecy), (0Perry 255Let0) 2) roi = thresh [yroismall Yemehjinghe Zero Xuew] roismall = cv2.resize (roi, (30 Magazine 30)) roismall = roismall.reshape ((1900)) roismall = np.float32 (roismall) retval, results, neigh_resp, dists = model.findNearest (roismall) K = 1) string = str (int ((results [0] [0]) numbers.append (int ((results [0] [0]) cv2.putText (out,string, (xmemy Yimh), 0 count + = 1 if count = 10: break return numbers numbers = getNum ('1.png')

Thank you for reading! On "how to use OpenCV to achieve standard digital recognition function" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report