10個(gè)步驟Opencv輕松檢測(cè)出圖片中條形碼
本文為大家分享了Opencv輕松檢測(cè)出圖片中條形碼的步驟,供大家參考,具體內(nèi)容如下
1. 原圖像大小調(diào)整,提高運(yùn)算效率

2. 轉(zhuǎn)化為灰度圖

3. 高斯平滑濾波

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



5.均值濾波,消除高頻噪聲

6.二值化

7.閉運(yùn)算,填充條形碼間隙

8. 腐蝕,去除孤立的點(diǎn)

9. 膨脹,填充條形碼間空隙,根據(jù)核的大小,有可能需要2~3次膨脹操作

10.通過(guò)findContours找到條形碼區(qū)域的矩形邊界

實(shí)現(xiàn):
#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. 原圖像大小調(diào)整,提高運(yùn)算效率
resize(image,image,Size(500,300));
imshow("1.原圖像",image);
//2. 轉(zhuǎn)化為灰度圖
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.閉運(yùn)算,填充條形碼間隙
Mat element=getStructuringElement(0,Size(7,7));
morphologyEx(imageSobleOutThreshold,imageSobleOutThreshold,MORPH_CLOSE,element);
imshow("7.閉運(yùn)算",imageSobleOutThreshold);
//8. 腐蝕,去除孤立的點(diǎn)
erode(imageSobleOutThreshold,imageSobleOutThreshold,element);
imshow("8.腐蝕",imageSobleOutThreshold);
//9. 膨脹,填充條形碼間空隙,根據(jù)核的大小,有可能需要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.通過(guò)findContours找到條形碼區(qū)域的矩形邊界
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.找出二維碼矩形區(qū)域",image);
waitKey();
}
使用另一幅圖片的效果如下:

底部的二維碼左側(cè)邊界定位錯(cuò)位,檢測(cè)發(fā)現(xiàn)在二值化的時(shí)候左側(cè)第二個(gè)條碼部分被歸零了,導(dǎo)致在之后的腐蝕操作中被腐蝕掉了。調(diào)整閾值分界值180到160,重新運(yùn)行正確:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python使用Opencv實(shí)現(xiàn)圖像特征檢測(cè)與匹配的方法
- python opencv檢測(cè)目標(biāo)顏色的實(shí)例講解
- Python 使用Opencv實(shí)現(xiàn)目標(biāo)檢測(cè)與識(shí)別的示例代碼
- Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼
- Opencv Hough算法實(shí)現(xiàn)圖片中直線檢測(cè)
- 使用OpenCV檢測(cè)圖像中的矩形
- OpenCV實(shí)現(xiàn)圖像角點(diǎn)檢測(cè)
- opencv?canny邊緣檢測(cè)算法詳解
- 基于OpenCV的路面質(zhì)量檢測(cè)的實(shí)現(xiàn)
- opencv實(shí)現(xiàn)礦石圖片檢測(cè)礦石數(shù)量
相關(guān)文章
Ubuntu配置sublime text 3的c編譯環(huán)境的具體步驟
下面小編就為大家?guī)?lái)一篇Ubuntu配置sublime text 3的c編譯環(huán)境的具體步驟。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
C語(yǔ)言編程中對(duì)目錄進(jìn)行基本的打開關(guān)閉和讀取操作詳解
這篇文章主要介紹了C語(yǔ)言編程中對(duì)目錄進(jìn)行基本的打開關(guān)閉和讀取操作,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
詳解C++編程中表達(dá)式的語(yǔ)義與計(jì)算順序
這篇文章主要介紹了C++編程中表達(dá)式的語(yǔ)義與計(jì)算順序,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-01-01

