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 realize simple digit recognition by opencv

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

Share

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

This article mainly introduces "how to achieve simple digital recognition in opencv". In daily operation, I believe that many people have doubts about how to achieve simple digital recognition in opencv. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve simple digital recognition in opencv". Next, please follow the editor to study!

Catalogue

Preface

The problem to be solved

The way to solve the problem

Preface

Because my knowledge is too small to use python deep learning to identify the numbers here, so I completely use opencv to identify numbers, and then share and record some of what I have seen and thought in the learning process.

The problem to be solved

This is a number to be identified. The first thing I want to do here is to extract a ROI from the image. The extraction result is only the number, excluding some other insignificant elements.

This is a ROI picture, and all we have to do is identify the numbers in the photo.

The way to solve the problem

1, first divide the numbers in this picture into five small pictures, each picture contains a number, why divide it? Because we can't let the computer know what the number is, we can only let the computer identify the feature according to the feature, and then each feature corresponds to a value, and first post the program that divides the picture, and then there will be a train of thought at the bottom of the program.

# include # include using namespace std; using namespace cv;#include Mat src_threshold;Mat src_dil;int sunImage (Mat & image); int main () {clock_t start, finish; start=clock (); Mat src; src=imread ("D:\\ vspic\\ picture\\ number6.jpg") Resize (src,src,Size (src.cols/7,src.rows/7)); imshow ("src", src); Mat src_gray; cvtColor (src,src_gray,COLOR_BGR2GRAY); / / imshow ("gsrc_ray", src_gray); Mat src_blur; blur (src_gray,src_blur,Size (9)) / / GaussianBlur (src_gray,src_blur,Size (115.11), 1jue 1); Mat src_threshold; threshold (src_blur,src_threshold,150,255,THRESH_OTSU); / / imshow ("src_threshold", src_threshold); Mat src_canny; Canny (src_threshold,src_canny,125,255,3); / / imshow ("src_canny", src_canny); vectorcontours_src Vectorhierarchy_src (contours_src.size ()); findContours (src_canny,contours_src,hierarchy_src,RETR_EXTERNAL,CHAIN_APPROX_NONE); Rect rect_s; Rect choose_rect; for (size_t iTunes 0) I1.5&&height > 50) {/ * rectangle (src,rect_s.tl (), rect_s.br (), Scalar (255255255), 1 choose_rect=Rect (rect_s.x+20,rect_s.y+30,rect_s.x-30,rect_s.y-108);}} Mat roi Roi=src (choose_rect); / / imshow ("src_", roi); Mat img = roi; Mat gray_img; / / generate grayscale image cvtColor (img, gray_img, CV_BGR2GRAY); / / Gaussian blur Mat img_gau; GaussianBlur (gray_img, img_gau, Size (3,3), 0,0) / / threshold segmentation Mat img_seg; threshold (img_gau, img_seg, 0,255, THRESH_BINARY + THRESH_OTSU); Mat element; element=getStructuringElement (MORPH_RECT,Size (8Power8)); erode (img_seg,src_dil,element); / / imshow ("src_dil", src_dil); / / Edge detection to extract contour Mat img_canny Canny (src_dil, img_canny, 200,100); / / imshow ("canny", img_canny); vector contours; vector hierarchy (contours.size ()); findContours (img_canny, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point ()); / / find contours int size = (int) (contours.size ()); / / number of contours / / cout

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