Abstract: This paper describes a method for marking a line on the text.
1. Description of the problem
The straight line marked on the image of text, text examples are as follows:
2, the protocol described
Straight-line detection of the above image, first think Hough line detection, but so will most of the text contains some fine also detected straight line is not the result we want, therefore, must be pre-image process, and the meaning of the following specific steps:
- Binarization process (foundation for the morphological operations)
- Morphological operations (to remove text)
- Linearly detecting
- The straight line marked
3, sample code
. 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <the iostream> . 3 #include <opencv2 / opencv.hpp> . 4 . 5 the using namespace STD; . 6 the using namespace CV; . 7 . 8 const char * INPUT = " input image " ; . 9 const char * = OUTPUT1 " binary image " ; 10 const char * = Output2 " linear image " ; . 11 const char * = OUTPUT3 "Result image " ; 12 is 13 is Mat the src, src_Threshould, DST, LSRC; 14 15 int main ( int Arge, char ** the argv) { 16 the src = imread ( " C: /Users/Lzy/Desktop/img/word.jpg " ); . 17 LSRC = imread ( " C: /Users/Lzy/Desktop/img/word.jpg " ); 18 is IF (src.empty ()) { . 19 COUT << " image import failed " << endl; 20 is return - . 1 ; 21 } 22 23 cvtColor(src, src, COLOR_BGR2GRAY); 24 namedWindow(input, WINDOW_AUTOSIZE); 25 imshow(input, src); 26 27 adaptiveThreshold(~src,src_Threshould,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,7,-2); 28 //medianBlur(src_Threshould,src_Threshould,3); 29 namedWindow(output1, WINDOW_AUTOSIZE); 30 imshow(output1,src_Threshould); 31 32 Mat vline = getStructuringElement(MORPH_RECT,Size(1,src.rows/20),Point(-1,-1)); 33 erode(src_Threshould, src_Threshould,vline); 34 dilate(src_Threshould,dst, vline); 35 namedWindow(output2, WINDOW_AUTOSIZE); 36 imshow(output2,dst); 37 38 vector<Vec4f>lines; 39 HoughLinesP(dst, lines, 1, CV_PI / 180.0, 10, 0, 10); 40 for (int i = 0; i < lines.size(); i++) { 41 Vec4f linep = lines[i]; 42 line(lsrc, Point(linep[0], linep[1]), Point(linep[2], linep[3]), Scalar(0,0,255), 3, LINE_AA); 43 } 44 imshow(output3, lsrc); 45 46 waitKey(0); 47 return 0; 48 }
4, the operating result
Artwork |
Binary image |
Morphological operations FIG. |
FIG detectable label |