基于OpenCV的条形码区域检测(完)

博主分享了基于OpenCV的条形码区域检测的完整代码,虽然较为粗糙,但添加了关键注释,方便理解。文章提到,代码仅供验证性质使用,测试图片可在GitHub的barcodes文件夹中找到。
摘要由CSDN通过智能技术生成

基于OpenCV的条形码区域检测(完)

 工作较忙,该系列随笔就要草草收尾,这篇将贴出完整代码,经过大略整理并添加关键注释,希望能够帮助理解。
 当时只是验证性质的实现,所以代码比较粗糙,请谅解!
  若有疑问,欢迎邮件至”XDG.WORK@GMAIL.COM”沟通。

测试图片在该文件夹下:barcodes@Github


#include <iostream>
#include <vector>
#include <fstream>

#include <opencv2\opencv.hpp>

#include <ctime>

//#define FILE_SAVE

using namespace cv;
using namespace std;

struct BlockInfo {
    //区块序号
    int index;
    //区块位置
    Point leftTop;
    //区块宽度
    int width;
    //区块高低
    int height;
    //区块角度信息直方图
    long angleHist[8];
    //得分(有效像素占总像素数的比例)
    float score;
    //有效点数量
    long count;
    //区块方向
    int direction;
    //所有点角度
    vector<float > angleSet;
    //
    bool exist;

    BlockInfo() {
        index = 0;
        leftTop.x = 0;
        leftTop.y = 0;
        width = 0;
        height = 0;
        memset(angleHist, 0, sizeof(long) * 8);
        score = 0.0f;
        count = 0;
        direction = -1;
        exist = false;
    }
};

vector<BlockInfo > g_blockSet;

//Mat gradient;
double gradient_minVal, gradient_maxVal;
long hist[8];

struct CmpByValue {
    bool operator()(const pair<int, long>& lhs, const pair<int, long>& rhs) {
        return lhs.second > rhs.second;
    }
};

//#define WHOLE_IMG

vector<Mat>  division(Mat &image, int width, int height) {
    int m, n;
    float fm, fn;
    fm = image.rows*1.0 / height*1.0;
    fn = image.cols*1.0 / width*1.0;

    m = (int)fm;
    n = (int)fn;

    if (fm - (float)m > 0) {
        m++;
    }
    if (fn - (float)n > 0) {
        n++;
    }

    int index = 0;

    vector<Mat> imgOut;
    for (int j = 0; j < m; j++) {
        if (j < m - 1) {
            int i;
            for (i = 0; i < n; i++) {
                if (i < n - 1) {
                    BlockInfo temBlock;
                    temBlock.index = index++;
                    Mat temImage(height, width, CV_8U, Scalar(0, 0, 0));
                    Mat imageROI = image(Rect(i*width, j*height, temImage.cols, temImage.rows));
                    addWeighted(temImage, 1.0, imageROI, 1.0, 0., temImage);

                    temBlock.width = width;
                    temBlock.height = height;
                    temBlock.leftTop = Point(i*width, j*height);

                    g_blockSet.push_back(temBlock);
                    imgOut.push_back(temImage);
                }
#ifdef WHOLE_IMG
                else {
                    Mat temImage(height, width, CV_8U, Scalar(0, 0, 0));
                    Mat imageROI = image(Rect(i*width, j*height, image.cols - i*width - 1, temImage.rows));
                    for (size_t y = 0; y < imageROI.rows; ++y) {
                        unsigned char* ps = imageROI.ptr<unsigned char>(y);
                        unsigned char* pp = temImage.ptr<unsigned char>(y);
                        for (size_t x = 0; x < imageROI.cols; ++x) {
                            pp[x] = ps[x];
                        }
                    }
                    imgOut.push_back(temImage);
                }
#endif
            }
        }
#ifdef WHOLE_IMG
        else {
            int i;
            for (i = 0; i < n; i++) {
                if (i < n - 1) {
                    Mat temImage(height, width, CV_8U, Scalar(0, 0, 0));
                    Mat imageROI = image(Rect(i*width, j*height, temImage.cols, image.rows - j*height - 1));
                    for (size_t y = 0; y < imageROI.rows; ++y) {
                        unsigned char* ps = imageROI.ptr<unsigned char>(y);
                        unsigned char* pp = temImage.ptr<unsigned char>(y);
                        for (size_t x = 0; x < imageROI.cols; ++x) {
                            pp[x] = ps[x];
                        }
                    }
                    imgOut.push_back(temImage);
                }
                else {
                    Mat temImage(height, width, CV_8U, Scalar(0, 0, 0));
                    Mat imageROI = image(Rect(i*width, j*height, image.cols - i*width - 1, image.rows - j*height - 1));
                    for (size_t y = 0; y < imageROI.rows; ++y) {
                        unsigned char* ps = imageROI.ptr<unsigned char>(y);
                        unsigned char* pp = temImage.ptr<unsigned char>(y);
                        for (size_t x = 0; x < imageROI.cols; ++x) {
                            pp[x] = ps[x];
                        }
                    }
                    imgOut.push_back(temImage);
                }
            }
        }
#endif
    }
    return imgOut;
}


///保存分割后的区块图像
void savedivisionfiles(vector<Mat> imgDiv) {
    int index = 0;
    for each(Mat m in imgDiv) {
        string prefilename = "Division - ";
        char str[10];
        itoa(index, str, 10);
        string indexstr(str);
     
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值