Home > other >  Vs2015 OpenCV4.0 graphical text contour generation (note: the program is a 64 - bit exe, 32-bit mach
Vs2015 OpenCV4.0 graphical text contour generation (note: the program is a 64 - bit exe, 32-bit mach

Time:10-04

Vs2015 + graphical text OpenCV4.0 contour generation (note: the program is a 64 - bit exe, 32-bit machine does not support running!)



The core code parts:

#include
//4.0#include//4.0 header file to realize image grayscale binary corrosion header file resolve undefined identifiers CV_BGR2GRAY

# include & lt; Opencv2/core/core. Hpp>//4.0 the expansion of the header files to realize image
# include & lt; Opencv2 highgui/highgui. Hpp>//4.0 the expansion of the header files to realize image
# include & lt; Opencv2 imgproc/imgproc. Hpp>//4.0 the expansion of the header files to realize image



Void CMFCApplication1Dlg: : OnBnClickedButton1 basic image processing ()//opencv
{
Using the namespace CV;
using namespace std;
Mat matSrc=https://bbs.csdn.net/topics/imread (" D:/capture. JPG ");
Imshow (" displays the original image ", matSrc);
//waitKey (0);
//destroyAllWindows ();


//2 turns the original image into grayscale
Mat matGray;
CvtColor (matSrc matGray, CV_BGR2GRAY);
//2 show grayscale
NamedWindow (" according to gray image, "WINDOW_NORMAL);
Imshow (" according to gray image, "matGray);
Imwrite (" show grayscale. JPG ", matGray);

//3 the greyscale image binarization
Mat matBinary;
Threshold (matGray matBinary, 128, 255, THRESH_BINARY);
//3 shows that binary map
NamedWindow (" displays binary map ", WINDOW_NORMAL);
Imshow (" displays binary map ", matBinary);
Imwrite (" shows that binary figure. JPG ", matBinary);

//4 corrosion
Mat matResult;
Mat element=getStructuringElement (MORPH_RECT, Size (17, 17));//create 17 x17 nuclear
Erode (matBinary matResult, element);

//4 shows corrosion
NamedWindow (" show that corrosion, "WINDOW_NORMAL);
Imshow (" show that corrosion, "matResult);
Imwrite (" show corrosion figure. JPG ", matResult);


//5 expansion operation
Mat out;
Mat elements=getStructuringElement (MORPH_RECT, Size (15, 15));
Dilate (matResult, out, elements);

//show the rendering
NamedWindow (" display expansion ");
Imshow (" show inflation ", out);
Imwrite (" show inflation figure. JPG ", out);

}

Void CMFCApplication1Dlg: : OnBnClickedButton2 ()//opencv image text extraction and box selected processing
{
Using the namespace CV;
using namespace std;
Mat matSrc=https://bbs.csdn.net/topics/imread (" D:/capture. JPG ");
//imshow (" shows the original image, "matSrc);
//waitKey (0);
//destroyAllWindows ();


//2 turns the original image into grayscale
Mat matGray;
CvtColor (matSrc matGray, CV_BGR2GRAY);
//2 show grayscale
//namedWindow (" according to gray image, "WINDOW_NORMAL);
//imshow (" according to gray image, "matGray);


//00000000000000000000000000000000000000000000000000000000000000000000000000

Mat gray;
CvtColor (matSrc, gray, CV_BGR2GRAY);
//imshow (" matSrc ", gray);

//2. Pretreatment of morphological transformation, can get find rectangular profile


//1. Sobel operator, and gradient in the direction of x
Mat sobel.
Sobel (gray, Sobel, CV_8U, 1, 0, 3);
//2. The binarization
Mat binary;
Threshold (sobel, binary, 0, 255, THRESH_OTSU + THRESH_BINARY);
//3. The corrosion expansion and operation of nuclear setting
Mat element1=getStructuringElement (MORPH_RECT, Size (30, 9));
//control height can be controlled up and down the degree of expansion, such as the stronger ability to distinguish between 3 to 4, but also causes residual
Mat element2=getStructuringElement (MORPH_RECT, Size (24, 4));

//4. The expansion time, let the silhouettes
Mat dilate1;
Dilate (binary, dilate1, element2);
//5. Corrosion time, remove the detail, form line, etc., here is removed with vertical lines
Mat erode1;
Erode (dilate1 erode1, element1);
//6. Again, let outline clearly some
Mat dilate2;
Dilate (erode1 dilate2, element2);
//7. Store the intermediate image
Imwrite (" binary. JPG ", binary);
Imwrite (" dilate1. JPG ", dilate1);
Imwrite (" erode1. JPG ", erode1);
Imwrite (" dilate2. JPG ", dilate2);

//the code above is no problem, can realize the pretreatment of the graphic

Vector Contours.
VectorFindContours (dilate2, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//draw profile
Mat: : zeros (dilate2. The size (), CV_8UC3);

for (int i=0; I & lt; Hierarchy. The size (); I++)
{
Scalar color=Scalar (rand () % 255, 255 rand () %, the rand () % 255);
//drawContours (dilate2, contours, I, color, CV_FILLED, 8, hierarchy);
DrawContours (matSrc, contours, I, CV: : Scalar (0, 0, 255), 2, 8, hierarchy, 1, Point (0, 0));//0255, 0 255 0, 0,0,255 red blue green
}
Imshow (" profile ", matSrc);
Imwrite (" profile. JPG ", matSrc);
}
1.0 source code version download address:

https://download.csdn.net/download/blackangelboy/12157111
Program running effect the following





2.0 source code version download address:

https://download.csdn.net/download/blackangelboy/12158766
Program running effect the following


Source code from the Internet, please do not commercial!

Have development needs can contact me, custom development intelligent recognition algorithm.



Sweep code directly add me WeChat, discuss intelligent recognition technology!



Opencv library classes set methods:

Detailed reference please go to: https://blog.csdn.net/qq_39987952/article/details/90700277















































  • Related