博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《图像处理实例》 之 寻找图纸标注
阅读量:4287 次
发布时间:2019-05-27

本文共 3309 字,大约阅读时间需要 11 分钟。


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

方法:

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

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

  2.利用模板匹配    

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


1 #include 
2 #include
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
> contours;22 vector
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 else36 {37 true_image = gray(Rect(x, y, width, height));38 true_pix_count = countNonZero(true_image);39 true_pix_rate = static_cast
(true_pix_count) / static_cast
(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 == 27043 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
(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
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 }

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

转载地址:http://witgi.baihongyu.com/

你可能感兴趣的文章
Windows系统搭建GitServer--Bonobo Git Server
查看>>
Bootstrap3 datetimepicker控件之smalot的使用
查看>>
小程序Canvas隐藏问题处理
查看>>
基于Zookeeper的Curator分布式锁实现
查看>>
来谈谈 Java 反射机制,动态代理是基于什么原理?
查看>>
JVM 内存模型
查看>>
iOS之苹果自带的json解析NSJSONSerialization(序列化)
查看>>
iOS中坐标转换
查看>>
java 基础二
查看>>
java基础(三)方法/数组/堆栈/
查看>>
java基础(四)二维数组/
查看>>
java基础(五)面向对象/类/对象/形式参数/局部和成员变量
查看>>
java基础(六)关键字/private/this/static/构造方法/
查看>>
java基础(七)/面向对像
查看>>
java基础(八)Math/代码块/继承成员方法指南的关系/继承中成员变量之间的关系/方法的重写/继承中构造方法之间的关系/this和super的区别
查看>>
iOS之AFNetWorking基本用法(一)上传、下载
查看>>
java基础(九)关键字final/多态/抽象类/关键字abstract/接口
查看>>
java中的错误集合
查看>>
java基础(十)形式参数和返回值/链式编程/包/权限修饰符/内部类
查看>>
C语言char *p 和 cha'r p[10]的区别/sizeof和strlen的区别
查看>>