Opencv:10个步骤检测出图片中条形码
来源:互联网 发布:leaflet.js中国地图 编辑:程序博客网 时间:2024/04/29 13:13
1. 原图像大小调整,提高运算效率
2. 转化为灰度图
3. 高斯平滑滤波
4.求得水平和垂直方向灰度图像的梯度差,使用Sobel算子
5.均值滤波,消除高频噪声
6.二值化
7.闭运算,填充条形码间隙
8. 腐蚀,去除孤立的点
9. 膨胀,填充条形码间空隙,根据核的大小,有可能需要2~3次膨胀操作
10.通过findContours找到条形码区域的矩形边界
实现:
#include "core/core.hpp"#include "highgui/highgui.hpp"#include "imgproc/imgproc.hpp"using namespace cv;int main(int argc,char *argv[]){Mat image,imageGray,imageGuussian;Mat imageSobelX,imageSobelY,imageSobelOut;image=imread(argv[1]);//1. 原图像大小调整,提高运算效率resize(image,image,Size(500,300));imshow("1.原图像",image);//2. 转化为灰度图cvtColor(image,imageGray,CV_RGB2GRAY);imshow("2.灰度图",imageGray);//3. 高斯平滑滤波GaussianBlur(imageGray,imageGuussian,Size(3,3),0);imshow("3.高斯平衡滤波",imageGuussian);//4.求得水平和垂直方向灰度图像的梯度差,使用Sobel算子Mat imageX16S,imageY16S;Sobel(imageGuussian,imageX16S,CV_16S,1,0,3,1,0,4);Sobel(imageGuussian,imageY16S,CV_16S,0,1,3,1,0,4);convertScaleAbs(imageX16S,imageSobelX,1,0);convertScaleAbs(imageY16S,imageSobelY,1,0);imageSobelOut=imageSobelX-imageSobelY;imshow("4.X方向梯度",imageSobelX);imshow("4.Y方向梯度",imageSobelY);imshow("4.XY方向梯度差",imageSobelOut);//5.均值滤波,消除高频噪声blur(imageSobelOut,imageSobelOut,Size(3,3));imshow("5.均值滤波",imageSobelOut);//6.二值化Mat imageSobleOutThreshold;threshold(imageSobelOut,imageSobleOutThreshold,180,255,CV_THRESH_BINARY);imshow("6.二值化",imageSobleOutThreshold);//7.闭运算,填充条形码间隙Mat element=getStructuringElement(0,Size(7,7));morphologyEx(imageSobleOutThreshold,imageSobleOutThreshold,MORPH_CLOSE,element);imshow("7.闭运算",imageSobleOutThreshold);//8. 腐蚀,去除孤立的点erode(imageSobleOutThreshold,imageSobleOutThreshold,element);imshow("8.腐蚀",imageSobleOutThreshold);//9. 膨胀,填充条形码间空隙,根据核的大小,有可能需要2~3次膨胀操作dilate(imageSobleOutThreshold,imageSobleOutThreshold,element);dilate(imageSobleOutThreshold,imageSobleOutThreshold,element);dilate(imageSobleOutThreshold,imageSobleOutThreshold,element);imshow("9.膨胀",imageSobleOutThreshold);vector<vector<Point>> contours;vector<Vec4i> hiera;//10.通过findContours找到条形码区域的矩形边界findContours(imageSobleOutThreshold,contours,hiera,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);for(int i=0;i<contours.size();i++){Rect rect=boundingRect((Mat)contours[i]);rectangle(image,rect,Scalar(255),2);}imshow("10.找出二维码矩形区域",image);waitKey();}
使用另一幅图片的效果如下:
底部的二维码左侧边界定位错位,检测发现在二值化的时候左侧第二个条码部分被归零了,导致在之后的腐蚀操作中被腐蚀掉了。调整阈值分界值180到160,重新运行正确:
0 0
- Opencv:10个步骤检测出图片中条形码
- Opencv:10个步骤检测出图片中条形码
- OpenCV 检测图片上的条形码
- 用 Python 和 OpenCV 检测图片上的条形码
- 用 Python 和 OpenCV 检测图片上的条形码
- 用 Python 和 OpenCV 检测图片上的条形码
- 用 Python 和 OpenCV 检测图片上的条形码
- 用 Python 和 OpenCV 检测图片上的条形码
- 使用Python和OpenCV检测图片上的条形码
- 检测图片上的条形码 Python 和 OpenCV
- 基于OpenCV的条形码区域检测(完)
- 用 Python 和 OpenCV 检测图片上的条形码Detecting Barcodes in Images with Python and OpenCV
- opencv中标准Hough变换检测出的直线参数
- 利用opencv检测出矩形
- 如何用10只实验鼠检测出1000个药瓶中哪个有毒药?
- 如何用10只实验鼠检测出1000个药瓶中哪个有毒药?
- 条形码检测
- 基于OpenCV的条形码区域检测(一)
- POJ 3126 BFS-Prime Path
- Java线程(三):线程协作-生产者/消费者问题
- Android Studio 标记书签
- java单例设计模式例子
- nginx中location介绍
- Opencv:10个步骤检测出图片中条形码
- Mastering Python-Packt Publishing 2016(读书笔记)第1版(讲解3.5的coroutine、asyncio、metaclass等)
- Android之Adapter系列文章介绍
- 打diff命令和知识
- OS10.11安装Cocoapods并集成ReactiveCocoa
- 前端学习Sublime篇(四)
- 简单的Net::FTP模块的简单应用
- 每个程序员都应当知道的编译器优化知识
- android的UI性能优化