A digital character recognition system based on Artificial Neural network demo (i): Character denoising, segmentation

Source: Internet
Author: User

Recently tried to use neural network to do digital character recognition, probably did a bit. The whole is very simple, that is, the test image pre-processing, and then by resampling extraction features, and finally through the neural network training and recognition. Interested can click here to learn about, welcome to advise.

Here I would like to introduce how to remove a pair of pictures with more than one character to de-noising and then split a single character , about how to use the Ann in OpenCV see OpenCV ann Neural Network use example.

Original diagram:


Here's a look at the process steps,

1. Load images and grayscale

2. Two value and reverse color processing

3. Extract the outer contour

4. Remove noise based on contour size

5. Find the contour again and find its boundingrect according to the contour

6. Save the Boundingrect separately as an image.

Well, that's basically the end of it. Directly on the source:

//--------------------------------------"program description"-------------------------------------------//Program Description: Ann-based digital character recognition system//Program Description://Development Test operating system: Windows 7 64bit//developing the IDE version for testing: Visual Studio//Development test used OPENCV version: 2.4.8//------------------------------------------------------------------------------------------------#include <opencv2/opencv.hpp> //header files using namespaceCv//contains CV namespace//#include <fstream>//using namespace Std; voidMain () {//Load ImagesMat srcimage = Imread ("Scandigit.bmp"); Imshow (" Original figure" ", srcimage);//Show loaded pictures    //GrayscaleMat Grayimage; Cvtcolor (Srcimage, Grayimage, Cv_rgb2gray);grayscale of//rgb imageImshow (" grayscale image " ", grayimage);//Imwrite ("Grayimage.jpg", grayimage);//Save Picture    //Two valueMat Biimage; Threshold (Grayimage, Biimage, $,255, CV_THRESH_BINARY_INV);//Grayscale image for binary processing, the foreground becomes white after findcontours () look for the outer contour to be usefulImshow (" binary graph" ", biimage);//Imwrite ("Biimage.jpg", biimage);/*//View picture data Ofstream outimage ("Imagedata.txt", Ios::out | ios::binary); for (unsigned int nrow = 0, nrow < srcimage.rows; nrow++) {for (unsigned int ncol = 0; Ncol < srcimage . cols;                ncol++) {Uchar val = srcimage.at<unsigned char> (nrow,ncol); Outimage << (int (val) > 200 1:0);        File3<<int (val) << Endl;      } outimage << Endl; } outimage.close (); */    //Noise removalMat contourimage (Biimage.rows, Biimage.cols, CV_8UC1, Scalar (0,0,0)); Mat noiseremoveimage = Biimage.clone (); vector<vector<Point> >contours; vector<Vec4i>Hierarchy Findcontours (biimage, contours, hierarchy, cv_retr_external, cv_chain_approx_simple); for(inti =0; i< contours.size (); i++) {Scalar color (255,255,255); Drawcontours (contourimage, contours, I, color,1,8, Hierarchy,1);//Draw outlines//printf (area of contour [%d]:%.2f\n ", I, Contourarea (Contours[i]));        if(Contourarea (Contours[i]) < -)//Here Area 20 is the experience value{drawcontours (noiseremoveimage, contours, I, Scalar (0,0,0), -1,8, Hierarchy,0); }} imshow ("Contour Map", contourimage); Imshow ("Noise removal diagram", noiseremoveimage);/*//First color-treated Mat dstimage;    Bitwise_not (Srcimage, dstimage); Imshow ("Inverse color graph", dstimage); *// *//Overall tilt adjustment Mat Rotmat (2, 3, CV_32FC1);        Mat rotateimage (Noiseremoveimage.rows, Noiseremoveimage.cols, Noiseremoveimage.type (), Scalar (0, 0, 0));    Point Center = Point (NOISEREMOVEIMAGE.COLS/2, NOISEREMOVEIMAGE.ROWS/2);    Double angle = 2.1;//here Specifies the rotation angle//double angle = slopeimage (noiseimage);    Double scale = 1;    Rotmat = getrotationmatrix2d (center, angle, scale);    Warpaffine (Noiseremoveimage, Rotateimage, Rotmat, Rotateimage.size (), Inter_linear, Border_constant, Scalar (0, 0, 0)); Imshow ("Rotation chart", rotateimage); *//*//expansion of the image mat element = Getstructuringelement (Morph_rect, Size (3, 3));//define core mat Dilateimage = srcimage.c    Lone ();    Morphologyex (Srcimage, dilateimage, morph_dilate, Element); Imshow ("Expansion map", dilateimage); */    //character segmentation and normalization of dimensionsMat segmentationimage = Noiseremoveimage.clone (); vector<vector<Point> >segcontours; vector<Vec4i>Seghierarchy;    Findcontours (Noiseremoveimage, Segcontours, Seghierarchy, cv_retr_external, cv_chain_approx_simple);    Mat Characterimage; Mat Dstimage;Charname[ -]; for(inti =0; I < segcontours.size (); i++) {Rect rect = Boundingrect (Mat (segcontours[i]));//Rectangle (Segmentationimage, rect.tl (), rect.br (), Scalar (255, 0, 0)); Each box is displayed        //The dimensions of each character are normalized and saved as a pictureMat ROI = segmentationimage (rect); Resize (Roi,characterimage,size ( +, -),(0,0),(0,0), inter_nearest);//re-color processingBitwise_not (Characterimage, dstimage);sprintf(Name,"Testdata\\%d.jpg", i);     Imwrite (Name,dstimage); } imshow ("Split Chart", segmentationimage);//wait for any button to pressWaitkey (0);}

If you want to know the whole project, you are welcome to visit Https://github.com/NodYoung/ANNDigitRec.

A digital character recognition system based on Artificial Neural network demo (i): Character denoising, segmentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.