opencv barcode recognition (already implemented)

While doing other things, I used opencv to write a simple and rude bar code detection and recognition, and
more logical recognition processing is needed.

#include <iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>

using namespace cv;
using namespace std;

Mat Image_process(Mat &src1)
{
    
    
    resize(src1,src1,Size(640,480));
    Mat dst;
    cvtColor(src1,dst,COLOR_BGR2GRAY);
    threshold(dst,dst,0,255,THRESH_BINARY_INV);
    Mat element=getStructuringElement(MORPH_ELLIPSE,Size(17,17));
    dilate(dst,dst,element);
    return dst;
}
int main()
{
    
    
    Mat src=imread("/home/sms/tu/download.png");

    Mat get_out=Image_process(src);
    imshow("原图",src);
    imshow("dst",get_out);
    vector<vector<Point>> contours;
    findContours(get_out,contours,RETR_TREE,CHAIN_APPROX_SIMPLE);
    vector<Rect> bounding(contours.size());
    vector<Rect> Rect_temp;
    for (size_t i=0;i<contours.size();i++)
    {
    
    
        bounding[i]=boundingRect(Mat(contours[i]));
        cout<<"第i"<<i<<"个轮廓的面积是"<<bounding[i].area()<<endl;
        if(bounding[i].area()>5000)
        {
    
    
            Rect_temp.push_back(bounding[i]);
        }
    }
    cout<<"大于500的轮廓面积是"<<Rect_temp.at(0).area()<<endl;
    for (size_t i=0;i<Rect_temp.size();++i)
    {
    
    
           rectangle(src,Rect_temp[i].tl(),Rect_temp[i].br(),Scalar(0,0,255),10,8);
    }
    imshow("drawing",src);
    waitKey(0);
}
![检测后的图](https://img-blog.csdnimg.cn/2020112117582331.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01zeXVzaGVuZw==,size_16,color_FFFFFF,t_70#pic_center)

Guess you like

Origin blog.csdn.net/Msyusheng/article/details/109903838