案例1:文本上直线标记

摘要:本文主要介绍了一种标记文本上直线的方法。

1、问题描述

将文本上图像上的直线标记出来,文本例子如下:

 

2、方案描述

进行上面图像的直线检测,首先会想到霍夫直线检测,但是这样会将大部分的文字中包含的一些细小的直线也检测出来,并不是我们想要的结果,因此,必须要对图像进行预处理,具体步骤以及意义如下:

  • 二值化处理(为形态学操作打基础)
  • 形态学操作(将文字去除)
  • 进行直线检测
  • 将直线标记出来

3、代码示例

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include<opencv2/opencv.hpp>
 4 
 5 using namespace std;
 6 using namespace cv;
 7 
 8 const char *input = "输入图像";
 9 const char *output1 = "二值图像";
10 const char *output2 = "直线图像";
11 const char *output3 = "结果图像";
12 
13 Mat src, src_Threshould, dst, lsrc;
14 
15 int main(int arge, char**argv) {
16     src = imread("C:/Users/Lzy/Desktop/img/word.jpg");
17     lsrc = imread("C:/Users/Lzy/Desktop/img/word.jpg");
18     if (src.empty()) {
19         cout << "图像导入失败" << endl;
20         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、操作结果

 原图

 

 二值图

 

 形态学操作图

 

 检测标记图

posted @ 2020-01-03 20:38  AmingGlaxy  阅读(247)  评论(0编辑  收藏  举报