[to] the character segmentation extraction in the picture (based on OpenCV)

Source: Internet
Author: User

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?
    1. #include "stdafx.h"
    2. #include "Cv.h"
    3. #include "highgui.h"
    4. #include "cxcore.h"
    5. int main (int argc, char* argv[])
    6. {
    7. iplimage* imgsrc = cvloadimage ("d:\\4.jpg", Cv_load_image_color);
    8. iplimage* Img_gray = Cvcreateimage (Cvgetsize (IMGSRC), ipl_depth_8u, 1);
    9. Cvcvtcolor (IMGSRC, Img_gray, Cv_bgr2gray);
    10. 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.
    11. Cvshowimage ("thresholdimg", Img_gray);
    12. cvseq* contours = NULL;
    13. cvmemstorage* storage = cvcreatememstorage (0);
    14. //top source picture defective can be removed by corrosion, swelling
    15. int count = cvfindcontours (Img_gray, storage, &contours,sizeof (Cvcontour), cv_retr_external);
    16. printf ("Number of outlines:%d", count);
    17. int idx = 0;
    18. char szname[56] = {0};
    19. int tempcount=0;
    20. For (cvseq* c = contours; c = NULL; c = c->h_next) {
    21. Cvrect RC =cvboundingrect (c,0);
    22. if ()
    23. //       {
    24. Continue This can be filtered based on the size of the contour
    25. //       }
    26. Cvdrawrect (IMGSRC, Cvpoint (rc.x, Rc.y), Cvpoint (rc.x + rc.width, Rc.y + rc.height), Cv_rgb (255, 0, 0));
    27. iplimage* Imgno = Cvcreateimage (Cvsize (Rc.width, Rc.height), ipl_depth_8u, 3);
    28. Cvsetimageroi (IMGSRC, RC);
    29. Cvcopyimage (IMGSRC, Imgno);
    30. Cvresetimageroi (IMGSRC);
    31. sprintf (SzName, "wnd_%d", idx++);
    32. Cvnamedwindow (SzName);
    33. 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;
    34. Cvreleaseimage (&imgno);
    35. }
    36. Cvnamedwindow ("src");
    37. Cvshowimage ("src", imgsrc);
    38. Cvwaitkey (0);
    39. Cvreleasememstorage (&storage);
    40. Cvreleaseimage (&IMGSRC);
    41. Cvreleaseimage (&img_gray);
    42. Cvdestroyallwindows ();
    43. return 0;
    44. }

[to] the character segmentation extraction in the picture (based on OpenCV)

Related Article

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.