服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 10个步骤Opencv轻松检测出图片中条形码

10个步骤Opencv轻松检测出图片中条形码

2021-06-19 11:38-牧野- C/C++

这篇文章主要为大家详细介绍了Opencv轻松检测出图片中条形码的10个步骤,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享了Opencv轻松检测出图片中条形码的步骤,供大家参考,具体内容如下

1. 原图像大小调整,提高运算效率

10个步骤Opencv轻松检测出图片中条形码

2. 转化为灰度图

10个步骤Opencv轻松检测出图片中条形码

3. 高斯平滑滤波

10个步骤Opencv轻松检测出图片中条形码

4.求得水平和垂直方向灰度图像的梯度差,使用Sobel算子

10个步骤Opencv轻松检测出图片中条形码10个步骤Opencv轻松检测出图片中条形码10个步骤Opencv轻松检测出图片中条形码

5.均值滤波,消除高频噪声

10个步骤Opencv轻松检测出图片中条形码

6.二值化

10个步骤Opencv轻松检测出图片中条形码

7.闭运算,填充条形码间隙

10个步骤Opencv轻松检测出图片中条形码

8. 腐蚀,去除孤立的点

10个步骤Opencv轻松检测出图片中条形码

9. 膨胀,填充条形码间空隙,根据核的大小,有可能需要2~3次膨胀操作

10个步骤Opencv轻松检测出图片中条形码

10.通过findContours找到条形码区域的矩形边界

10个步骤Opencv轻松检测出图片中条形码

实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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();
}

使用另一幅图片的效果如下:

10个步骤Opencv轻松检测出图片中条形码

底部的二维码左侧边界定位错位,检测发现在二值化的时候左侧第二个条码部分被归零了,导致在之后的腐蚀操作中被腐蚀掉了。调整阈值分界值180到160,重新运行正确:

10个步骤Opencv轻松检测出图片中条形码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/dcrmg/article/details/52095508

延伸 · 阅读

精彩推荐