基于OpenCV的条形码区域检测(完)
工作较忙,该系列随笔就要草草收尾,这篇将贴出完整代码,经过大略整理并添加关键注释,希望能够帮助理解。
当时只是验证性质的实现,所以代码比较粗糙,请谅解!
若有疑问,欢迎邮件至”XDG.WORK@GMAIL.COM”沟通。
#include <iostream>
#include <vector>
#include <fstream>
#include <opencv2\opencv.hpp>
#include <ctime>
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;
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;
}
};
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);