|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "../precomp.hpp"
|
|
|
#include "abs_decoder.hpp"
|
|
|
|
|
|
namespace cv {
|
|
|
namespace barcode {
|
|
|
|
|
|
void cropROI(const Mat &src, Mat &dst, const std::vector<Point2f> &rects)
|
|
|
{
|
|
|
std::vector<Point2f> vertices = rects;
|
|
|
int height = cvRound(norm(vertices[0] - vertices[1]));
|
|
|
int width = cvRound(norm(vertices[1] - vertices[2]));
|
|
|
if (height > width)
|
|
|
{
|
|
|
std::swap(height, width);
|
|
|
Point2f v0 = vertices[0];
|
|
|
vertices.erase(vertices.begin());
|
|
|
vertices.push_back(v0);
|
|
|
}
|
|
|
std::vector<Point2f> dst_vertices{
|
|
|
Point2f(0, (float) (height - 1)), Point2f(0, 0), Point2f((float) (width - 1), 0),
|
|
|
Point2f((float) (width - 1), (float) (height - 1))};
|
|
|
dst.create(Size(width, height), CV_8UC1);
|
|
|
Mat M = getPerspectiveTransform(vertices, dst_vertices);
|
|
|
warpPerspective(src, dst, M, dst.size(), cv::INTER_LINEAR, BORDER_CONSTANT, Scalar(255));
|
|
|
}
|
|
|
|
|
|
void fillCounter(const std::vector<uchar> &row, uint start, Counter &counter)
|
|
|
{
|
|
|
size_t counter_length = counter.pattern.size();
|
|
|
std::fill(counter.pattern.begin(), counter.pattern.end(), 0);
|
|
|
counter.sum = 0;
|
|
|
size_t end = row.size();
|
|
|
uchar color = row[start];
|
|
|
uint counterPosition = 0;
|
|
|
while (start < end)
|
|
|
{
|
|
|
if (row[start] == color)
|
|
|
{
|
|
|
counter.pattern[counterPosition]++;
|
|
|
counter.sum++;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
counterPosition++;
|
|
|
if (counterPosition == counter_length)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
counter.pattern[counterPosition] = 1;
|
|
|
counter.sum++;
|
|
|
color = 255 - color;
|
|
|
}
|
|
|
}
|
|
|
++start;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static inline uint
|
|
|
patternMatchVariance(const Counter &counter, const std::vector<int> &pattern, uint maxIndividualVariance)
|
|
|
{
|
|
|
size_t numCounters = counter.pattern.size();
|
|
|
int total = static_cast<int>(counter.sum);
|
|
|
int patternLength = std::accumulate(pattern.cbegin(), pattern.cend(), 0);
|
|
|
if (total < patternLength)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return WHITE;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength;
|
|
|
maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT;
|
|
|
uint totalVariance = 0;
|
|
|
for (uint x = 0; x < numCounters; x++)
|
|
|
{
|
|
|
int cnt = counter.pattern[x] << INTEGER_MATH_SHIFT;
|
|
|
int scaledPattern = pattern[x] * unitBarWidth;
|
|
|
uint variance = std::abs(cnt - scaledPattern);
|
|
|
if (variance > maxIndividualVariance)
|
|
|
{
|
|
|
return WHITE;
|
|
|
}
|
|
|
totalVariance += variance;
|
|
|
}
|
|
|
return totalVariance / total;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint patternMatch(const Counter &counters, const std::vector<int> &pattern, uint maxIndividual)
|
|
|
{
|
|
|
CV_Assert(counters.pattern.size() == pattern.size());
|
|
|
return patternMatchVariance(counters, pattern, maxIndividual);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|