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

本文共 3361 字,大约阅读时间需要 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 }

 

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

转载于:https://www.cnblogs.com/wjy-lulu/p/7535891.html

你可能感兴趣的文章
开发规范(一) 如何记录日志 By 阿里
查看>>
基于长连接简单聊天
查看>>
C/C++——C语言数组名与指针
查看>>
jquery Easy ui 设置下拉combobox 可用与不可用
查看>>
Eval()和DataBinder Eval(Container DataItem,)的区别及用法
查看>>
探寻路径
查看>>
讨论:技术和创意那个重要?。。。哈哈,我认为技术是创意实现的方法。
查看>>
硬件方案终于谈下来了,松了一口气,呼
查看>>
linux安装
查看>>
吴恩达机器学习笔记7-梯度下降III(Gradient descent intuition) --梯度下降的线性回归...
查看>>
iPhone-获取网络数据或者路径的文件名
查看>>
jquery简单实现点击弹出层效果实例
查看>>
TOSSIM进行无线传感网络仿真的大致流程
查看>>
微信内打开链接显示已停止访问该网页
查看>>
基于servlet和jsp的简单注册登录页面(包括:增删查改及分页显示)
查看>>
数据结构基础之一
查看>>
10.29随笔
查看>>
ScintillaNET v2.5 简单应用实例讲解
查看>>
I.MX6 Android busybox 从哪里生成的
查看>>
循环节长度 蓝桥杯
查看>>