影醉阏轩窗

衣带渐宽终不悔,为伊消得人憔悴。
扩大
缩小

《图像处理实例》 之 寻找图纸标注

 

 


要求:寻找图纸零件所有标注的字符,包括位置信息+图像信息

方法:

  1.利用形态学+轮廓信息去查询

    这里精度不是很高,计算难度也比较复杂,好处是思想简单。

  2.利用模板匹配    

    这里是保证模板和实际相差不大,不然匹配精度就很差了。

 


 

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 #include "math.h"
 4 using namespace cv;
 5 using namespace std;
 6 
 7 typedef struct MyStruct
 8 {
 9     Rect my_rec;
10     Mat my_img;
11 }MyStruct;
12 
13 int main(int argc, char*argv[])
14 {
15     Mat inputImage = imread("1.png");
16     Mat showImage = inputImage.clone();
17     cvtColor(inputImage, inputImage, CV_BGR2GRAY);
18     Mat morph, gray = inputImage.clone(), showGray;
19     showGray.create(inputImage.size(), CV_8UC1);
20     showGray.setTo(0);
21     vector<vector<Point>> contours;
22     vector<Vec4i> hierarchy;
23     findContours(inputImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));
24     Rect rec_adapt;
25     for (size_t i = 0; i < contours.size(); i++)
26     {
27         int x = minAreaRect(contours[i]).boundingRect().x;
28         int y = minAreaRect(contours[i]).boundingRect().y;
29         int width = minAreaRect(contours[i]).boundingRect().width;
30         int height = minAreaRect(contours[i]).boundingRect().height;
31         Mat true_image;
32         int true_pix_count;
33         double true_pix_rate;
34         if (x < 0 || y < 0) true_pix_rate = 1;
35         else
36         {
37             true_image = gray(Rect(x, y, width, height));
38             true_pix_count = countNonZero(true_image);
39             true_pix_rate = static_cast<double>(true_pix_count) / static_cast<double>(minAreaRect(contours[i]).boundingRect().area());
40         }             
41         double angle = minAreaRect(contours[i]).angle;
42         bool flag_angle = (angle == 9 ||  angle == 180 || angle == 0 ) ? true : false;//|| angle == 270
43         if (minAreaRect(contours[i]).size.height >= 10 && minAreaRect(contours[i]).size.height <= 20 && minAreaRect(contours[i]).size.width >= 4 && minAreaRect(contours[i]).size.width <= 30  && flag_angle && true_pix_rate <= 0.8)//
44         {
45             drawContours(showGray, contours, static_cast<int>(i), Scalar(255, 255, 255), 1);
46         }
47     }
48     Mat img1;
49     Mat kernel_x = getStructuringElement(MORPH_RECT, Size(20,1));
50     Mat kernel_y = getStructuringElement(MORPH_RECT, Size(1, 28));
51     Mat kernel_x_l = getStructuringElement(MORPH_RECT, Size(20, 1));
52     morphologyEx(showGray, showGray, MORPH_DILATE, kernel_x);
53     morphologyEx(showGray, showGray, MORPH_DILATE, kernel_x);
54     morphologyEx(showGray, img1, MORPH_OPEN, kernel_y);
55     showGray = showGray - img1;
56     morphologyEx(showGray, showGray, MORPH_CLOSE, kernel_x_l);
57     findContours(showGray, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));
58     vector<MyStruct> my_class;
59     for (size_t i = 0; i < contours.size(); i++)
60     {    
61         if (boundingRect(contours[i]).width > 60)
62         {
63             int x = minAreaRect(contours[i]).boundingRect().x;
64             int y = minAreaRect(contours[i]).boundingRect().y;
65             int width = minAreaRect(contours[i]).boundingRect().width;
66             int height = minAreaRect(contours[i]).boundingRect().height;
67             MyStruct Struct_temp;
68             Struct_temp.my_rec = boundingRect(contours[i]);
69             Struct_temp.my_img = showImage(Rect(x, y, width, height)).clone();
70             my_class.push_back(Struct_temp);
71             rectangle(showImage, boundingRect(contours[i]), Scalar(0, 0, 255));
72         }        
73     }
74     
75 
76     waitKey(0);
77     return 0;
78 
79 }

 

效果图不上了,不想再去运行了

posted on 2017-09-17 16:00  影醉阏轩窗  阅读(492)  评论(0编辑  收藏  举报

导航

/* 线条鼠标集合 */ /* 鼠标点击求赞文字特效 */ //带头像评论