1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge branch '4.x' into '5.x'

This commit is contained in:
Maksim Shabunin
2024-06-11 19:38:59 +03:00
573 changed files with 72922 additions and 7355 deletions
@@ -99,21 +99,6 @@ void Board::Impl::generateImage(Size outSize, OutputArray img, int marginSize, i
float sizeX = maxX - minX;
float sizeY = maxY - minY;
// proportion transformations
float xReduction = sizeX / float(out.cols);
float yReduction = sizeY / float(out.rows);
// determine the zone where the markers are placed
if(xReduction > yReduction) {
int nRows = int(sizeY / xReduction);
int rowsMargins = (out.rows - nRows) / 2;
out.adjustROI(-rowsMargins, -rowsMargins, 0, 0);
} else {
int nCols = int(sizeX / yReduction);
int colsMargins = (out.cols - nCols) / 2;
out.adjustROI(0, 0, -colsMargins, -colsMargins);
}
// now paint each marker
Mat marker;
Point2f outCorners[3];
+66 -3
View File
@@ -142,11 +142,15 @@ struct BarcodeImpl : public GraphicalCodeDetector::Impl
public:
shared_ptr<SuperScale> sr;
bool use_nn_sr = false;
double detectorThrDownSample = 512.f;
vector<float> detectorWindowSizes = {0.01f, 0.03f, 0.06f, 0.08f};
double detectorThrGradMagnitude = 64.f;
public:
//=================
// own methods
BarcodeImpl() = default;
BarcodeImpl() {}
vector<Mat> initDecode(const Mat &src, const vector<vector<Point2f>> &points) const;
bool decodeWithType(InputArray img,
InputArray points,
@@ -268,8 +272,8 @@ bool BarcodeImpl::detect(InputArray img, OutputArray points) const
}
Detect bardet;
bardet.init(inarr);
bardet.localization();
bardet.init(inarr, detectorThrDownSample);
bardet.localization(detectorWindowSizes, detectorThrGradMagnitude);
if (!bardet.computeTransformationPoints())
{ return false; }
vector<vector<Point2f>> pnts2f = bardet.getTransformationPoints();
@@ -370,5 +374,64 @@ bool BarcodeDetector::detectAndDecodeWithType(InputArray img, vector<string> &de
return p_->detectAndDecodeWithType(img, decoded_info, decoded_type, points_);
}
double BarcodeDetector::getDownsamplingThreshold() const
{
Ptr<BarcodeImpl> p_ = dynamic_pointer_cast<BarcodeImpl>(p);
CV_Assert(p_);
return p_->detectorThrDownSample;
}
BarcodeDetector& BarcodeDetector::setDownsamplingThreshold(double thresh)
{
Ptr<BarcodeImpl> p_ = dynamic_pointer_cast<BarcodeImpl>(p);
CV_Assert(p_);
CV_Assert(thresh >= 64);
p_->detectorThrDownSample = thresh;
return *this;
}
void BarcodeDetector::getDetectorScales(CV_OUT std::vector<float>& sizes) const
{
Ptr<BarcodeImpl> p_ = dynamic_pointer_cast<BarcodeImpl>(p);
CV_Assert(p_);
sizes = p_->detectorWindowSizes;
}
BarcodeDetector& BarcodeDetector::setDetectorScales(const std::vector<float>& sizes)
{
Ptr<BarcodeImpl> p_ = dynamic_pointer_cast<BarcodeImpl>(p);
CV_Assert(p_);
CV_Assert(sizes.size() > 0 && sizes.size() <= 16);
for (const float &size : sizes) {
CV_Assert(size > 0 && size < 1);
}
p_->detectorWindowSizes = sizes;
return *this;
}
double BarcodeDetector::getGradientThreshold() const
{
Ptr<BarcodeImpl> p_ = dynamic_pointer_cast<BarcodeImpl>(p);
CV_Assert(p_);
return p_->detectorThrGradMagnitude;
}
BarcodeDetector& BarcodeDetector::setGradientThreshold(double thresh)
{
Ptr<BarcodeImpl> p_ = dynamic_pointer_cast<BarcodeImpl>(p);
CV_Assert(p_);
CV_Assert(thresh >= 0 && thresh < 1e4);
p_->detectorThrGradMagnitude = thresh;
return *this;
}
}// namespace barcode
} // namespace cv
@@ -136,13 +136,13 @@ static void NMSBoxes(const std::vector<RotatedRect>& bboxes, const std::vector<f
//==============================================================================
void Detect::init(const Mat &src)
void Detect::init(const Mat &src, double detectorThreshDownSamplingLimit)
{
const double min_side = std::min(src.size().width, src.size().height);
if (min_side > 512.0)
if (min_side > detectorThreshDownSamplingLimit)
{
purpose = SHRINKING;
coeff_expansion = min_side / 512.0;
coeff_expansion = min_side / detectorThreshDownSamplingLimit;
width = cvRound(src.size().width / coeff_expansion);
height = cvRound(src.size().height / coeff_expansion);
Size new_size(width, height);
@@ -171,19 +171,19 @@ void Detect::init(const Mat &src)
}
void Detect::localization()
void Detect::localization(const std::vector<float>& detectorWindowSizes, double detectorThreshGradientMagnitude)
{
localization_bbox.clear();
bbox_scores.clear();
// get integral image
preprocess();
preprocess(detectorThreshGradientMagnitude);
// empirical setting
static constexpr float SCALE_LIST[] = {0.01f, 0.03f, 0.06f, 0.08f};
//static constexpr float SCALE_LIST[] = {0.01f, 0.03f, 0.06f, 0.08f};
const auto min_side = static_cast<float>(std::min(width, height));
int window_size;
for (const float scale:SCALE_LIST)
for (const float scale: detectorWindowSizes)
{
window_size = cvRound(min_side * scale);
if(window_size == 0) {
@@ -205,7 +205,20 @@ bool Detect::computeTransformationPoints()
transformation_points.reserve(bbox_indices.size());
RotatedRect rect;
Point2f temp[4];
const float THRESHOLD_SCORE = float(width * height) / 300.f;
/**
* #24902 resolution invariant barcode detector
*
* refactor of THRESHOLD_SCORE = float(width * height) / 300.f
* wrt to rescaled input size - 300 value needs factorization
* only one factor pair matches a common aspect ratio of 4:3 ~ 20x15
* decomposing this yields THRESHOLD_SCORE = (width / 20) * (height / 15)
* therefore each factor was rescaled based by purpose (refsize was 512)
*/
const float THRESHOLD_WSCALE = (purpose != UNCHANGED) ? 20 : (20 * width / 512.f);
const float THRESHOLD_HSCALE = (purpose != UNCHANGED) ? 15 : (15 * height / 512.f);
const float THRESHOLD_SCORE = (width / THRESHOLD_WSCALE) * (height / THRESHOLD_HSCALE);
NMSBoxes(localization_bbox, bbox_scores, THRESHOLD_SCORE, 0.1f, bbox_indices);
for (const auto &bbox_index : bbox_indices)
@@ -231,15 +244,14 @@ bool Detect::computeTransformationPoints()
}
void Detect::preprocess()
void Detect::preprocess(double detectorGradientMagnitudeThresh)
{
Mat scharr_x, scharr_y, temp;
static constexpr double THRESHOLD_MAGNITUDE = 64.;
Scharr(resized_barcode, scharr_x, CV_32F, 1, 0);
Scharr(resized_barcode, scharr_y, CV_32F, 0, 1);
// calculate magnitude of gradient and truncate
magnitude(scharr_x, scharr_y, temp);
threshold(temp, temp, THRESHOLD_MAGNITUDE, 1, THRESH_BINARY);
threshold(temp, temp, detectorGradientMagnitudeThresh, 1, THRESH_BINARY);
temp.convertTo(gradient_magnitude, CV_8U);
integral(gradient_magnitude, integral_edges, CV_32F);
@@ -24,9 +24,9 @@ private:
public:
void init(const Mat &src);
void init(const Mat &src, double detectorThreshDownSamplingLimit);
void localization();
void localization(const vector<float>& detectorWindowSizes, double detectorGradientMagnitudeThresh);
vector<vector<Point2f>> getTransformationPoints()
{ return transformation_points; }
@@ -44,7 +44,7 @@ protected:
int height, width;
Mat resized_barcode, gradient_magnitude, coherence, orientation, edge_nums, integral_x_sq, integral_y_sq, integral_xy, integral_edges;
void preprocess();
void preprocess(double detectorThreshGradientMagnitude);
void calCoherence(int window_size);