http://blog.csdn.net/anqing715/article/details/16883863
SOURCE picture
Characters like these are good operations, each character is independent and not linked together, so the contour detection is best.
So there are:
1. Turn the source image into a single-channel grayscale image
2. Perform a threshold operation on a grayscale image to get a two value image
(For some mobile phones, the background color is not pure words, you can use Cvsmooth () smooth a bit. )
Image after binary value
3. Contour detection (only for the outermost layer)
4. Traverse all detected contours, and use Cvboundingrect () to get the bounding rectangle of each contour
Find the outline
5. Pull them out, this step in the above traversal directly through the Setroi method to extract.
The final result
Here is the source code:
[CPP]View PlainCopy print?
- #include "stdafx.h"
- #include "Cv.h"
- #include "highgui.h"
- #include "cxcore.h"
- int main (int argc, char* argv[])
- {
- iplimage* imgsrc = cvloadimage ("d:\\4.jpg", Cv_load_image_color);
- iplimage* Img_gray = Cvcreateimage (Cvgetsize (IMGSRC), ipl_depth_8u, 1);
- Cvcvtcolor (IMGSRC, Img_gray, Cv_bgr2gray);
- Cvthreshold (Img_gray, img_gray,100, 255,CV_THRESH_BINARY_INV); //CV_THRESH_BINARY_INV makes the background black, the character is white, so the outermost layer of the character is found.
- Cvshowimage ("thresholdimg", Img_gray);
- cvseq* contours = NULL;
- cvmemstorage* storage = cvcreatememstorage (0);
- //top source picture defective can be removed by corrosion, swelling
- int count = cvfindcontours (Img_gray, storage, &contours,sizeof (Cvcontour), cv_retr_external);
- printf ("Number of outlines:%d", count);
- int idx = 0;
- char szname[56] = {0};
- int tempcount=0;
- For (cvseq* c = contours; c = NULL; c = c->h_next) {
- Cvrect RC =cvboundingrect (c,0);
- if ()
- // {
- Continue This can be filtered based on the size of the contour
- // }
- Cvdrawrect (IMGSRC, Cvpoint (rc.x, Rc.y), Cvpoint (rc.x + rc.width, Rc.y + rc.height), Cv_rgb (255, 0, 0));
- iplimage* Imgno = Cvcreateimage (Cvsize (Rc.width, Rc.height), ipl_depth_8u, 3);
- Cvsetimageroi (IMGSRC, RC);
- Cvcopyimage (IMGSRC, Imgno);
- Cvresetimageroi (IMGSRC);
- sprintf (SzName, "wnd_%d", idx++);
- Cvnamedwindow (SzName);
- Cvshowimage (SzName, Imgno); //If you want to cut out the image from left to right sort, or from top to bottom, you can compare rc.x,rc.y;
- Cvreleaseimage (&imgno);
- }
- Cvnamedwindow ("src");
- Cvshowimage ("src", imgsrc);
- Cvwaitkey (0);
- Cvreleasememstorage (&storage);
- Cvreleaseimage (&IMGSRC);
- Cvreleaseimage (&img_gray);
- Cvdestroyallwindows ();
- return 0;
- }
[to] the character segmentation extraction in the picture (based on OpenCV)