2018-06-27 21:37:10 +08:00
|
|
|
// This file is part of OpenCV project.
|
|
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2018, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
#include "opencv2/objdetect.hpp"
|
2018-08-18 00:01:02 +08:00
|
|
|
#include "opencv2/calib3d.hpp"
|
|
|
|
|
|
|
|
#ifdef HAVE_QUIRC
|
|
|
|
#include "quirc.h"
|
|
|
|
#endif
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
2018-08-18 00:01:02 +08:00
|
|
|
#include <queue>
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
using std::vector;
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
class QRDetect
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
public:
|
2018-08-07 22:37:51 +08:00
|
|
|
void init(const Mat& src, double eps_vertical_ = 0.2, double eps_horizontal_ = 0.1);
|
2018-06-27 21:37:10 +08:00
|
|
|
bool localization();
|
2018-08-07 22:37:51 +08:00
|
|
|
bool computeTransformationPoints();
|
2018-06-27 21:37:10 +08:00
|
|
|
Mat getBinBarcode() { return bin_barcode; }
|
|
|
|
Mat getStraightBarcode() { return straight_barcode; }
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Point2f> getTransformationPoints() { return transformation_points; }
|
2018-08-18 00:01:02 +08:00
|
|
|
static Point2f intersectionLines(Point2f a1, Point2f a2, Point2f b1, Point2f b2);
|
2018-07-11 01:24:09 +08:00
|
|
|
protected:
|
2018-08-07 22:37:51 +08:00
|
|
|
vector<Vec3d> searchHorizontalLines();
|
|
|
|
vector<Point2f> separateVerticalLines(const vector<Vec3d> &list_lines);
|
2018-07-11 01:24:09 +08:00
|
|
|
void fixationPoints(vector<Point2f> &local_point);
|
|
|
|
vector<Point2f> getQuadrilateral(vector<Point2f> angle_list);
|
|
|
|
bool testBypassRoute(vector<Point2f> hull, int start, int finish);
|
2018-08-01 22:49:43 +08:00
|
|
|
inline double getCosVectors(Point2f a, Point2f b, Point2f c);
|
2018-07-11 01:24:09 +08:00
|
|
|
|
|
|
|
Mat barcode, bin_barcode, straight_barcode;
|
|
|
|
vector<Point2f> localization_points, transformation_points;
|
2018-07-14 05:51:04 +08:00
|
|
|
double eps_vertical, eps_horizontal, coeff_expansion;
|
2018-06-27 21:37:10 +08:00
|
|
|
};
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
void QRDetect::init(const Mat& src, double eps_vertical_, double eps_horizontal_)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
CV_Assert(!src.empty());
|
|
|
|
const double min_side = std::min(src.size().width, src.size().height);
|
2018-07-11 01:24:09 +08:00
|
|
|
if (min_side < 512.0)
|
|
|
|
{
|
|
|
|
coeff_expansion = 512.0 / min_side;
|
2018-08-07 22:37:51 +08:00
|
|
|
const int width = cvRound(src.size().width * coeff_expansion);
|
|
|
|
const int height = cvRound(src.size().height * coeff_expansion);
|
2018-07-11 01:24:09 +08:00
|
|
|
Size new_size(width, height);
|
2018-08-07 22:37:51 +08:00
|
|
|
resize(src, barcode, new_size, 0, 0, INTER_LINEAR);
|
2018-07-11 01:24:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
coeff_expansion = 1.0;
|
|
|
|
barcode = src;
|
|
|
|
}
|
2018-08-07 22:37:51 +08:00
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
eps_vertical = eps_vertical_;
|
|
|
|
eps_horizontal = eps_horizontal_;
|
2018-08-07 22:37:51 +08:00
|
|
|
adaptiveThreshold(barcode, bin_barcode, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 83, 2);
|
2018-08-18 00:01:02 +08:00
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
vector<Vec3d> QRDetect::searchHorizontalLines()
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Vec3d> result;
|
2018-08-07 22:37:51 +08:00
|
|
|
const int height_bin_barcode = bin_barcode.rows;
|
|
|
|
const int width_bin_barcode = bin_barcode.cols;
|
|
|
|
const size_t test_lines_size = 5;
|
|
|
|
double test_lines[test_lines_size];
|
|
|
|
const size_t count_pixels_position = 1024;
|
|
|
|
size_t pixels_position[count_pixels_position];
|
|
|
|
size_t index = 0;
|
|
|
|
|
|
|
|
for (int y = 0; y < height_bin_barcode; y++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
const uint8_t *bin_barcode_row = bin_barcode.ptr<uint8_t>(y);
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
int pos = 0;
|
|
|
|
for (; pos < width_bin_barcode; pos++) { if (bin_barcode_row[pos] == 0) break; }
|
|
|
|
if (pos == width_bin_barcode) { continue; }
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
index = 0;
|
|
|
|
pixels_position[index] = pixels_position[index + 1] = pixels_position[index + 2] = pos;
|
|
|
|
index +=3;
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
uint8_t future_pixel = 255;
|
|
|
|
for (int x = pos; x < width_bin_barcode; x++)
|
|
|
|
{
|
|
|
|
if (bin_barcode_row[x] == future_pixel)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
future_pixel = 255 - future_pixel;
|
|
|
|
pixels_position[index] = x;
|
|
|
|
index++;
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
2018-08-07 22:37:51 +08:00
|
|
|
}
|
|
|
|
pixels_position[index] = width_bin_barcode - 1;
|
|
|
|
index++;
|
|
|
|
for (size_t i = 2; i < index - 4; i+=2)
|
|
|
|
{
|
|
|
|
test_lines[0] = static_cast<double>(pixels_position[i - 1] - pixels_position[i - 2]);
|
|
|
|
test_lines[1] = static_cast<double>(pixels_position[i ] - pixels_position[i - 1]);
|
|
|
|
test_lines[2] = static_cast<double>(pixels_position[i + 1] - pixels_position[i ]);
|
|
|
|
test_lines[3] = static_cast<double>(pixels_position[i + 2] - pixels_position[i + 1]);
|
|
|
|
test_lines[4] = static_cast<double>(pixels_position[i + 3] - pixels_position[i + 2]);
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
double length = 0.0, weight = 0.0;
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
for (size_t j = 0; j < test_lines_size; j++) { length += test_lines[j]; }
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
if (length == 0) { continue; }
|
|
|
|
for (size_t j = 0; j < test_lines_size; j++)
|
|
|
|
{
|
|
|
|
if (j == 2) { weight += fabs((test_lines[j] / length) - 3.0/7.0); }
|
|
|
|
else { weight += fabs((test_lines[j] / length) - 1.0/7.0); }
|
|
|
|
}
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
if (weight < eps_vertical)
|
|
|
|
{
|
|
|
|
Vec3d line;
|
|
|
|
line[0] = static_cast<double>(pixels_position[i - 2]);
|
|
|
|
line[1] = y;
|
|
|
|
line[2] = length;
|
|
|
|
result.push_back(line);
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
vector<Point2f> QRDetect::separateVerticalLines(const vector<Vec3d> &list_lines)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Vec3d> result;
|
2018-06-27 21:37:10 +08:00
|
|
|
int temp_length = 0;
|
2018-08-07 22:37:51 +08:00
|
|
|
uint8_t next_pixel;
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<double> test_lines;
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
for (size_t pnt = 0; pnt < list_lines.size(); pnt++)
|
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
const int x = cvRound(list_lines[pnt][0] + list_lines[pnt][2] * 0.5);
|
|
|
|
const int y = cvRound(list_lines[pnt][1]);
|
|
|
|
|
|
|
|
// --------------- Search vertical up-lines --------------- //
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
test_lines.clear();
|
2018-08-07 22:37:51 +08:00
|
|
|
uint8_t future_pixel_up = 255;
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
for (int j = y; j < bin_barcode.rows - 1; j++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
next_pixel = bin_barcode.at<uint8_t>(j + 1, x);
|
2018-06-27 21:37:10 +08:00
|
|
|
temp_length++;
|
2018-08-07 22:37:51 +08:00
|
|
|
if (next_pixel == future_pixel_up)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
future_pixel_up = 255 - future_pixel_up;
|
2018-06-27 21:37:10 +08:00
|
|
|
test_lines.push_back(temp_length);
|
|
|
|
temp_length = 0;
|
|
|
|
if (test_lines.size() == 3) { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
// --------------- Search vertical down-lines --------------- //
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
uint8_t future_pixel_down = 255;
|
2018-06-27 21:37:10 +08:00
|
|
|
for (int j = y; j >= 1; j--)
|
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
next_pixel = bin_barcode.at<uint8_t>(j - 1, x);
|
2018-06-27 21:37:10 +08:00
|
|
|
temp_length++;
|
2018-08-07 22:37:51 +08:00
|
|
|
if (next_pixel == future_pixel_down)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
future_pixel_down = 255 - future_pixel_down;
|
2018-06-27 21:37:10 +08:00
|
|
|
test_lines.push_back(temp_length);
|
|
|
|
temp_length = 0;
|
|
|
|
if (test_lines.size() == 6) { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
// --------------- Compute vertical lines --------------- //
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
if (test_lines.size() == 6)
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
double length = 0.0, weight = 0.0;
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < test_lines.size(); i++) { length += test_lines[i]; }
|
|
|
|
|
2018-07-17 21:14:54 +08:00
|
|
|
CV_Assert(length > 0);
|
2018-06-27 21:37:10 +08:00
|
|
|
for (size_t i = 0; i < test_lines.size(); i++)
|
|
|
|
{
|
2018-07-14 05:51:04 +08:00
|
|
|
if (i % 3 == 0) { weight += fabs((test_lines[i] / length) - 3.0/14.0); }
|
|
|
|
else { weight += fabs((test_lines[i] / length) - 1.0/ 7.0); }
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
if(weight < eps_horizontal)
|
|
|
|
{
|
|
|
|
result.push_back(list_lines[pnt]);
|
|
|
|
}
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Point2f> point2f_result;
|
|
|
|
for (size_t i = 0; i < result.size(); i++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
point2f_result.push_back(
|
2018-08-07 22:37:51 +08:00
|
|
|
Point2f(static_cast<float>(result[i][0] + result[i][2] * 0.5),
|
|
|
|
static_cast<float>(result[i][1])));
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
2018-07-11 01:24:09 +08:00
|
|
|
return point2f_result;
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
void QRDetect::fixationPoints(vector<Point2f> &local_point)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
double cos_angles[3], norm_triangl[3];
|
|
|
|
|
|
|
|
norm_triangl[0] = norm(local_point[1] - local_point[2]);
|
|
|
|
norm_triangl[1] = norm(local_point[0] - local_point[2]);
|
|
|
|
norm_triangl[2] = norm(local_point[1] - local_point[0]);
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
cos_angles[0] = (norm_triangl[1] * norm_triangl[1] + norm_triangl[2] * norm_triangl[2]
|
|
|
|
- norm_triangl[0] * norm_triangl[0]) / (2 * norm_triangl[1] * norm_triangl[2]);
|
|
|
|
cos_angles[1] = (norm_triangl[0] * norm_triangl[0] + norm_triangl[2] * norm_triangl[2]
|
|
|
|
- norm_triangl[1] * norm_triangl[1]) / (2 * norm_triangl[0] * norm_triangl[2]);
|
|
|
|
cos_angles[2] = (norm_triangl[0] * norm_triangl[0] + norm_triangl[1] * norm_triangl[1]
|
|
|
|
- norm_triangl[2] * norm_triangl[2]) / (2 * norm_triangl[0] * norm_triangl[1]);
|
|
|
|
|
|
|
|
const double angle_barrier = 0.85;
|
|
|
|
if (fabs(cos_angles[0]) > angle_barrier || fabs(cos_angles[1]) > angle_barrier || fabs(cos_angles[2]) > angle_barrier)
|
|
|
|
{
|
|
|
|
local_point.clear();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-11 01:24:09 +08:00
|
|
|
|
|
|
|
size_t i_min_cos =
|
2018-08-07 22:37:51 +08:00
|
|
|
(cos_angles[0] < cos_angles[1] && cos_angles[0] < cos_angles[2]) ? 0 :
|
|
|
|
(cos_angles[1] < cos_angles[0] && cos_angles[1] < cos_angles[2]) ? 1 : 2;
|
2018-07-11 01:24:09 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
size_t index_max = 0;
|
|
|
|
double max_area = std::numeric_limits<double>::min();
|
|
|
|
for (size_t i = 0; i < local_point.size(); i++)
|
|
|
|
{
|
|
|
|
const size_t current_index = i % 3;
|
|
|
|
const size_t left_index = (i + 1) % 3;
|
|
|
|
const size_t right_index = (i + 2) % 3;
|
|
|
|
|
|
|
|
const Point2f current_point(local_point[current_index]),
|
|
|
|
left_point(local_point[left_index]), right_point(local_point[right_index]),
|
|
|
|
central_point(intersectionLines(current_point,
|
|
|
|
Point2f(static_cast<float>((local_point[left_index].x + local_point[right_index].x) * 0.5),
|
|
|
|
static_cast<float>((local_point[left_index].y + local_point[right_index].y) * 0.5)),
|
|
|
|
Point2f(0, static_cast<float>(bin_barcode.rows - 1)),
|
|
|
|
Point2f(static_cast<float>(bin_barcode.cols - 1),
|
|
|
|
static_cast<float>(bin_barcode.rows - 1))));
|
|
|
|
|
|
|
|
|
|
|
|
vector<Point2f> list_area_pnt;
|
|
|
|
list_area_pnt.push_back(current_point);
|
|
|
|
|
|
|
|
vector<LineIterator> list_line_iter;
|
|
|
|
list_line_iter.push_back(LineIterator(bin_barcode, current_point, left_point));
|
|
|
|
list_line_iter.push_back(LineIterator(bin_barcode, current_point, central_point));
|
|
|
|
list_line_iter.push_back(LineIterator(bin_barcode, current_point, right_point));
|
2018-07-11 01:24:09 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
for (size_t k = 0; k < list_line_iter.size(); k++)
|
|
|
|
{
|
|
|
|
uint8_t future_pixel = 255, count_index = 0;
|
|
|
|
for(int j = 0; j < list_line_iter[k].count; j++, ++list_line_iter[k])
|
|
|
|
{
|
|
|
|
if (list_line_iter[k].pos().x >= bin_barcode.cols ||
|
|
|
|
list_line_iter[k].pos().y >= bin_barcode.rows) { break; }
|
|
|
|
const uint8_t value = bin_barcode.at<uint8_t>(list_line_iter[k].pos());
|
|
|
|
if (value == future_pixel)
|
|
|
|
{
|
|
|
|
future_pixel = 255 - future_pixel;
|
|
|
|
count_index++;
|
|
|
|
if (count_index == 3)
|
|
|
|
{
|
|
|
|
list_area_pnt.push_back(list_line_iter[k].pos());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const double temp_check_area = contourArea(list_area_pnt);
|
|
|
|
if (temp_check_area > max_area)
|
|
|
|
{
|
|
|
|
index_max = current_index;
|
|
|
|
max_area = temp_check_area;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (index_max == i_min_cos) { std::swap(local_point[0], local_point[index_max]); }
|
|
|
|
else { local_point.clear(); return; }
|
|
|
|
|
|
|
|
const Point2f rpt = local_point[0], bpt = local_point[1], gpt = local_point[2];
|
2018-07-11 01:24:09 +08:00
|
|
|
Matx22f m(rpt.x - bpt.x, rpt.y - bpt.y, gpt.x - rpt.x, gpt.y - rpt.y);
|
|
|
|
if( determinant(m) > 0 )
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
std::swap(local_point[1], local_point[2]);
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
bool QRDetect::localization()
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
Point2f begin, end;
|
2018-08-07 22:37:51 +08:00
|
|
|
vector<Vec3d> list_lines_x = searchHorizontalLines();
|
2018-07-11 01:24:09 +08:00
|
|
|
if( list_lines_x.empty() ) { return false; }
|
2018-08-07 22:37:51 +08:00
|
|
|
vector<Point2f> list_lines_y = separateVerticalLines(list_lines_x);
|
|
|
|
if( list_lines_y.size() < 3 ) { return false; }
|
2018-07-11 01:24:09 +08:00
|
|
|
|
|
|
|
vector<Point2f> centers;
|
|
|
|
Mat labels;
|
|
|
|
kmeans(list_lines_y, 3, labels,
|
2018-08-07 22:37:51 +08:00
|
|
|
TermCriteria( TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1),
|
2018-07-11 01:24:09 +08:00
|
|
|
3, KMEANS_PP_CENTERS, localization_points);
|
|
|
|
|
|
|
|
fixationPoints(localization_points);
|
2018-06-27 21:37:10 +08:00
|
|
|
if (localization_points.size() != 3) { return false; }
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
if (coeff_expansion > 1.0)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
const int width = cvRound(bin_barcode.size().width / coeff_expansion);
|
|
|
|
const int height = cvRound(bin_barcode.size().height / coeff_expansion);
|
2018-07-11 01:24:09 +08:00
|
|
|
Size new_size(width, height);
|
2018-10-16 01:13:16 +08:00
|
|
|
Mat intermediate;
|
2018-08-07 22:37:51 +08:00
|
|
|
resize(bin_barcode, intermediate, new_size, 0, 0, INTER_LINEAR);
|
2018-07-11 01:24:09 +08:00
|
|
|
bin_barcode = intermediate.clone();
|
|
|
|
for (size_t i = 0; i < localization_points.size(); i++)
|
|
|
|
{
|
|
|
|
localization_points[i] /= coeff_expansion;
|
|
|
|
}
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
for (size_t i = 0; i < localization_points.size(); i++)
|
|
|
|
{
|
|
|
|
for (size_t j = i + 1; j < localization_points.size(); j++)
|
|
|
|
{
|
|
|
|
if (norm(localization_points[i] - localization_points[j]) < 10)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-27 21:37:10 +08:00
|
|
|
return true;
|
2018-07-11 01:24:09 +08:00
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
bool QRDetect::computeTransformationPoints()
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
if (localization_points.size() != 3) { return false; }
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Point> locations, non_zero_elem[3], newHull;
|
|
|
|
vector<Point2f> new_non_zero_elem[3];
|
|
|
|
for (size_t i = 0; i < 3; i++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
Mat mask = Mat::zeros(bin_barcode.rows + 2, bin_barcode.cols + 2, CV_8UC1);
|
|
|
|
uint8_t next_pixel, future_pixel = 255;
|
2018-08-07 22:37:51 +08:00
|
|
|
int count_test_lines = 0, index = cvRound(localization_points[i].x);
|
2018-07-11 01:24:09 +08:00
|
|
|
for (; index < bin_barcode.cols - 1; index++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
next_pixel = bin_barcode.at<uint8_t>(
|
2018-08-07 22:37:51 +08:00
|
|
|
cvRound(localization_points[i].y), index + 1);
|
2018-07-11 01:24:09 +08:00
|
|
|
if (next_pixel == future_pixel)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
future_pixel = 255 - future_pixel;
|
|
|
|
count_test_lines++;
|
|
|
|
if (count_test_lines == 2)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
floodFill(bin_barcode, mask,
|
2018-08-07 22:37:51 +08:00
|
|
|
Point(index + 1, cvRound(localization_points[i].y)), 255,
|
2018-07-11 01:24:09 +08:00
|
|
|
0, Scalar(), Scalar(), FLOODFILL_MASK_ONLY);
|
|
|
|
break;
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 01:24:09 +08:00
|
|
|
Mat mask_roi = mask(Range(1, bin_barcode.rows - 1), Range(1, bin_barcode.cols - 1));
|
|
|
|
findNonZero(mask_roi, non_zero_elem[i]);
|
|
|
|
newHull.insert(newHull.end(), non_zero_elem[i].begin(), non_zero_elem[i].end());
|
|
|
|
}
|
|
|
|
convexHull(Mat(newHull), locations);
|
|
|
|
for (size_t i = 0; i < locations.size(); i++)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < 3; j++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
for (size_t k = 0; k < non_zero_elem[j].size(); k++)
|
|
|
|
{
|
|
|
|
if (locations[i] == non_zero_elem[j][k])
|
|
|
|
{
|
|
|
|
new_non_zero_elem[j].push_back(locations[i]);
|
|
|
|
}
|
|
|
|
}
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
double pentagon_diag_norm = -1;
|
|
|
|
Point2f down_left_edge_point, up_right_edge_point, up_left_edge_point;
|
|
|
|
for (size_t i = 0; i < new_non_zero_elem[1].size(); i++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
for (size_t j = 0; j < new_non_zero_elem[2].size(); j++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
double temp_norm = norm(new_non_zero_elem[1][i] - new_non_zero_elem[2][j]);
|
|
|
|
if (temp_norm > pentagon_diag_norm)
|
|
|
|
{
|
|
|
|
down_left_edge_point = new_non_zero_elem[1][i];
|
|
|
|
up_right_edge_point = new_non_zero_elem[2][j];
|
|
|
|
pentagon_diag_norm = temp_norm;
|
|
|
|
}
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 01:55:15 +08:00
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
if (down_left_edge_point == Point2f(0, 0) ||
|
2018-08-06 01:55:15 +08:00
|
|
|
up_right_edge_point == Point2f(0, 0) ||
|
|
|
|
new_non_zero_elem[0].size() == 0) { return false; }
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
double max_area = -1;
|
|
|
|
up_left_edge_point = new_non_zero_elem[0][0];
|
2018-08-06 01:55:15 +08:00
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
for (size_t i = 0; i < new_non_zero_elem[0].size(); i++)
|
|
|
|
{
|
2018-08-01 22:49:43 +08:00
|
|
|
vector<Point2f> list_edge_points;
|
|
|
|
list_edge_points.push_back(new_non_zero_elem[0][i]);
|
|
|
|
list_edge_points.push_back(down_left_edge_point);
|
|
|
|
list_edge_points.push_back(up_right_edge_point);
|
|
|
|
|
2018-08-06 01:55:15 +08:00
|
|
|
double temp_area = fabs(contourArea(list_edge_points));
|
2018-07-11 01:24:09 +08:00
|
|
|
if (max_area < temp_area)
|
|
|
|
{
|
|
|
|
up_left_edge_point = new_non_zero_elem[0][i];
|
|
|
|
max_area = temp_area;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2f down_max_delta_point, up_max_delta_point;
|
|
|
|
double norm_down_max_delta = -1, norm_up_max_delta = -1;
|
|
|
|
for (size_t i = 0; i < new_non_zero_elem[1].size(); i++)
|
|
|
|
{
|
|
|
|
double temp_norm_delta = norm(up_left_edge_point - new_non_zero_elem[1][i])
|
|
|
|
+ norm(down_left_edge_point - new_non_zero_elem[1][i]);
|
|
|
|
if (norm_down_max_delta < temp_norm_delta)
|
|
|
|
{
|
|
|
|
down_max_delta_point = new_non_zero_elem[1][i];
|
|
|
|
norm_down_max_delta = temp_norm_delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 01:55:15 +08:00
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
for (size_t i = 0; i < new_non_zero_elem[2].size(); i++)
|
|
|
|
{
|
|
|
|
double temp_norm_delta = norm(up_left_edge_point - new_non_zero_elem[2][i])
|
|
|
|
+ norm(up_right_edge_point - new_non_zero_elem[2][i]);
|
|
|
|
if (norm_up_max_delta < temp_norm_delta)
|
|
|
|
{
|
|
|
|
up_max_delta_point = new_non_zero_elem[2][i];
|
|
|
|
norm_up_max_delta = temp_norm_delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transformation_points.push_back(down_left_edge_point);
|
|
|
|
transformation_points.push_back(up_left_edge_point);
|
|
|
|
transformation_points.push_back(up_right_edge_point);
|
|
|
|
transformation_points.push_back(
|
|
|
|
intersectionLines(down_left_edge_point, down_max_delta_point,
|
|
|
|
up_right_edge_point, up_max_delta_point));
|
|
|
|
|
|
|
|
vector<Point2f> quadrilateral = getQuadrilateral(transformation_points);
|
|
|
|
transformation_points = quadrilateral;
|
|
|
|
|
|
|
|
return true;
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
Point2f QRDetect::intersectionLines(Point2f a1, Point2f a2, Point2f b1, Point2f b2)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
Point2f result_square_angle(
|
|
|
|
((a1.x * a2.y - a1.y * a2.x) * (b1.x - b2.x) -
|
|
|
|
(b1.x * b2.y - b1.y * b2.x) * (a1.x - a2.x)) /
|
|
|
|
((a1.x - a2.x) * (b1.y - b2.y) -
|
|
|
|
(a1.y - a2.y) * (b1.x - b2.x)),
|
|
|
|
((a1.x * a2.y - a1.y * a2.x) * (b1.y - b2.y) -
|
|
|
|
(b1.x * b2.y - b1.y * b2.x) * (a1.y - a2.y)) /
|
|
|
|
((a1.x - a2.x) * (b1.y - b2.y) -
|
|
|
|
(a1.y - a2.y) * (b1.x - b2.x))
|
|
|
|
);
|
2018-06-27 21:37:10 +08:00
|
|
|
return result_square_angle;
|
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
// test function (if true then ------> else <------ )
|
2018-08-07 22:37:51 +08:00
|
|
|
bool QRDetect::testBypassRoute(vector<Point2f> hull, int start, int finish)
|
2018-07-11 01:24:09 +08:00
|
|
|
{
|
|
|
|
int index_hull = start, next_index_hull, hull_size = (int)hull.size();
|
|
|
|
double test_length[2] = { 0.0, 0.0 };
|
|
|
|
do
|
|
|
|
{
|
|
|
|
next_index_hull = index_hull + 1;
|
|
|
|
if (next_index_hull == hull_size) { next_index_hull = 0; }
|
|
|
|
test_length[0] += norm(hull[index_hull] - hull[next_index_hull]);
|
|
|
|
index_hull = next_index_hull;
|
|
|
|
}
|
|
|
|
while(index_hull != finish);
|
|
|
|
|
|
|
|
index_hull = start;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
next_index_hull = index_hull - 1;
|
|
|
|
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
|
|
|
|
test_length[1] += norm(hull[index_hull] - hull[next_index_hull]);
|
|
|
|
index_hull = next_index_hull;
|
|
|
|
}
|
|
|
|
while(index_hull != finish);
|
|
|
|
|
|
|
|
if (test_length[0] < test_length[1]) { return true; } else { return false; }
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
vector<Point2f> QRDetect::getQuadrilateral(vector<Point2f> angle_list)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
|
|
|
size_t angle_size = angle_list.size();
|
|
|
|
uint8_t value, mask_value;
|
2018-07-11 01:24:09 +08:00
|
|
|
Mat mask = Mat::zeros(bin_barcode.rows + 2, bin_barcode.cols + 2, CV_8UC1);
|
|
|
|
Mat fill_bin_barcode = bin_barcode.clone();
|
2018-06-27 21:37:10 +08:00
|
|
|
for (size_t i = 0; i < angle_size; i++)
|
|
|
|
{
|
|
|
|
LineIterator line_iter(bin_barcode, angle_list[ i % angle_size],
|
|
|
|
angle_list[(i + 1) % angle_size]);
|
|
|
|
for(int j = 0; j < line_iter.count; j++, ++line_iter)
|
|
|
|
{
|
|
|
|
value = bin_barcode.at<uint8_t>(line_iter.pos());
|
|
|
|
mask_value = mask.at<uint8_t>(line_iter.pos() + Point(1, 1));
|
|
|
|
if (value == 0 && mask_value == 0)
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
floodFill(fill_bin_barcode, mask, line_iter.pos(), 255,
|
|
|
|
0, Scalar(), Scalar(), FLOODFILL_MASK_ONLY);
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Point> locations;
|
|
|
|
Mat mask_roi = mask(Range(1, bin_barcode.rows - 1), Range(1, bin_barcode.cols - 1));
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-08-18 00:01:02 +08:00
|
|
|
findNonZero(mask_roi, locations);
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < angle_list.size(); i++)
|
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
int x = cvRound(angle_list[i].x);
|
|
|
|
int y = cvRound(angle_list[i].y);
|
2018-07-11 01:24:09 +08:00
|
|
|
locations.push_back(Point(x, y));
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Point> integer_hull;
|
|
|
|
convexHull(Mat(locations), integer_hull);
|
|
|
|
int hull_size = (int)integer_hull.size();
|
|
|
|
vector<Point2f> hull(hull_size);
|
|
|
|
for (int i = 0; i < hull_size; i++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-07 22:37:51 +08:00
|
|
|
float x = saturate_cast<float>(integer_hull[i].x);
|
|
|
|
float y = saturate_cast<float>(integer_hull[i].y);
|
2018-07-11 01:24:09 +08:00
|
|
|
hull[i] = Point2f(x, y);
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 01:55:15 +08:00
|
|
|
const double experimental_area = fabs(contourArea(hull));
|
2018-07-14 05:51:04 +08:00
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Point2f> result_hull_point(angle_size);
|
|
|
|
double min_norm;
|
2018-06-27 21:37:10 +08:00
|
|
|
for (size_t i = 0; i < angle_size; i++)
|
|
|
|
{
|
|
|
|
min_norm = std::numeric_limits<double>::max();
|
|
|
|
Point closest_pnt;
|
2018-07-11 01:24:09 +08:00
|
|
|
for (int j = 0; j < hull_size; j++)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
double temp_norm = norm(hull[j] - angle_list[i]);
|
|
|
|
if (min_norm > temp_norm)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
min_norm = temp_norm;
|
|
|
|
closest_pnt = hull[j];
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result_hull_point[i] = closest_pnt;
|
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
int start_line[2] = { 0, 0 }, finish_line[2] = { 0, 0 }, unstable_pnt = 0;
|
2018-06-27 21:37:10 +08:00
|
|
|
for (int i = 0; i < hull_size; i++)
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
if (result_hull_point[2] == hull[i]) { start_line[0] = i; }
|
|
|
|
if (result_hull_point[1] == hull[i]) { finish_line[0] = start_line[1] = i; }
|
|
|
|
if (result_hull_point[0] == hull[i]) { finish_line[1] = i; }
|
|
|
|
if (result_hull_point[3] == hull[i]) { unstable_pnt = i; }
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
int index_hull, extra_index_hull, next_index_hull, extra_next_index_hull;
|
2018-06-27 21:37:10 +08:00
|
|
|
Point result_side_begin[4], result_side_end[4];
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
bool bypass_orientation = testBypassRoute(hull, start_line[0], finish_line[0]);
|
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
min_norm = std::numeric_limits<double>::max();
|
|
|
|
index_hull = start_line[0];
|
|
|
|
do
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
if (bypass_orientation) { next_index_hull = index_hull + 1; }
|
2018-06-27 21:37:10 +08:00
|
|
|
else { next_index_hull = index_hull - 1; }
|
|
|
|
|
|
|
|
if (next_index_hull == hull_size) { next_index_hull = 0; }
|
|
|
|
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
Point angle_closest_pnt = norm(hull[index_hull] - angle_list[1]) >
|
|
|
|
norm(hull[index_hull] - angle_list[2]) ? angle_list[2] : angle_list[1];
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
Point intrsc_line_hull =
|
2018-07-11 01:24:09 +08:00
|
|
|
intersectionLines(hull[index_hull], hull[next_index_hull],
|
|
|
|
angle_list[1], angle_list[2]);
|
|
|
|
double temp_norm = getCosVectors(hull[index_hull], intrsc_line_hull, angle_closest_pnt);
|
2018-06-27 21:37:10 +08:00
|
|
|
if (min_norm > temp_norm &&
|
2018-07-11 01:24:09 +08:00
|
|
|
norm(hull[index_hull] - hull[next_index_hull]) >
|
2018-08-01 22:49:43 +08:00
|
|
|
norm(angle_list[1] - angle_list[2]) * 0.1)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
|
|
|
min_norm = temp_norm;
|
2018-07-11 01:24:09 +08:00
|
|
|
result_side_begin[0] = hull[index_hull];
|
|
|
|
result_side_end[0] = hull[next_index_hull];
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
index_hull = next_index_hull;
|
|
|
|
}
|
|
|
|
while(index_hull != finish_line[0]);
|
|
|
|
|
|
|
|
if (min_norm == std::numeric_limits<double>::max())
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
result_side_begin[0] = angle_list[1];
|
|
|
|
result_side_end[0] = angle_list[2];
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
min_norm = std::numeric_limits<double>::max();
|
|
|
|
index_hull = start_line[1];
|
2018-07-11 01:24:09 +08:00
|
|
|
bypass_orientation = testBypassRoute(hull, start_line[1], finish_line[1]);
|
2018-06-27 21:37:10 +08:00
|
|
|
do
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
if (bypass_orientation) { next_index_hull = index_hull + 1; }
|
2018-06-27 21:37:10 +08:00
|
|
|
else { next_index_hull = index_hull - 1; }
|
|
|
|
|
|
|
|
if (next_index_hull == hull_size) { next_index_hull = 0; }
|
|
|
|
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
Point angle_closest_pnt = norm(hull[index_hull] - angle_list[0]) >
|
|
|
|
norm(hull[index_hull] - angle_list[1]) ? angle_list[1] : angle_list[0];
|
2018-06-27 21:37:10 +08:00
|
|
|
|
|
|
|
Point intrsc_line_hull =
|
2018-07-11 01:24:09 +08:00
|
|
|
intersectionLines(hull[index_hull], hull[next_index_hull],
|
|
|
|
angle_list[0], angle_list[1]);
|
|
|
|
double temp_norm = getCosVectors(hull[index_hull], intrsc_line_hull, angle_closest_pnt);
|
2018-06-27 21:37:10 +08:00
|
|
|
if (min_norm > temp_norm &&
|
2018-07-11 01:24:09 +08:00
|
|
|
norm(hull[index_hull] - hull[next_index_hull]) >
|
2018-08-01 22:49:43 +08:00
|
|
|
norm(angle_list[0] - angle_list[1]) * 0.05)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
|
|
|
min_norm = temp_norm;
|
2018-07-11 01:24:09 +08:00
|
|
|
result_side_begin[1] = hull[index_hull];
|
|
|
|
result_side_end[1] = hull[next_index_hull];
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
index_hull = next_index_hull;
|
|
|
|
}
|
|
|
|
while(index_hull != finish_line[1]);
|
|
|
|
|
|
|
|
if (min_norm == std::numeric_limits<double>::max())
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
result_side_begin[1] = angle_list[0];
|
|
|
|
result_side_end[1] = angle_list[1];
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
bypass_orientation = testBypassRoute(hull, start_line[0], unstable_pnt);
|
2018-08-07 22:37:51 +08:00
|
|
|
const bool extra_bypass_orientation = testBypassRoute(hull, finish_line[1], unstable_pnt);
|
2018-06-27 21:37:10 +08:00
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
vector<Point2f> result_angle_list(4), test_result_angle_list(4);
|
2018-08-07 22:37:51 +08:00
|
|
|
double min_diff_area = std::numeric_limits<double>::max();
|
2018-06-27 21:37:10 +08:00
|
|
|
index_hull = start_line[0];
|
2018-08-07 22:37:51 +08:00
|
|
|
const double standart_norm = std::max(
|
2018-07-14 05:51:04 +08:00
|
|
|
norm(result_side_begin[0] - result_side_end[0]),
|
|
|
|
norm(result_side_begin[1] - result_side_end[1]));
|
2018-06-27 21:37:10 +08:00
|
|
|
do
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
if (bypass_orientation) { next_index_hull = index_hull + 1; }
|
2018-06-27 21:37:10 +08:00
|
|
|
else { next_index_hull = index_hull - 1; }
|
|
|
|
|
|
|
|
if (next_index_hull == hull_size) { next_index_hull = 0; }
|
|
|
|
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
|
|
|
|
|
2018-08-01 22:49:43 +08:00
|
|
|
if (norm(hull[index_hull] - hull[next_index_hull]) < standart_norm * 0.1)
|
2018-07-14 05:51:04 +08:00
|
|
|
{ index_hull = next_index_hull; continue; }
|
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
extra_index_hull = finish_line[1];
|
|
|
|
do
|
|
|
|
{
|
2018-07-11 01:24:09 +08:00
|
|
|
if (extra_bypass_orientation) { extra_next_index_hull = extra_index_hull + 1; }
|
2018-06-27 21:37:10 +08:00
|
|
|
else { extra_next_index_hull = extra_index_hull - 1; }
|
|
|
|
|
|
|
|
if (extra_next_index_hull == hull_size) { extra_next_index_hull = 0; }
|
|
|
|
if (extra_next_index_hull == -1) { extra_next_index_hull = hull_size - 1; }
|
|
|
|
|
2018-08-01 22:49:43 +08:00
|
|
|
if (norm(hull[extra_index_hull] - hull[extra_next_index_hull]) < standart_norm * 0.1)
|
2018-07-14 05:51:04 +08:00
|
|
|
{ extra_index_hull = extra_next_index_hull; continue; }
|
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
test_result_angle_list[0]
|
2018-07-11 01:24:09 +08:00
|
|
|
= intersectionLines(result_side_begin[0], result_side_end[0],
|
|
|
|
result_side_begin[1], result_side_end[1]);
|
2018-06-27 21:37:10 +08:00
|
|
|
test_result_angle_list[1]
|
2018-07-11 01:24:09 +08:00
|
|
|
= intersectionLines(result_side_begin[1], result_side_end[1],
|
|
|
|
hull[extra_index_hull], hull[extra_next_index_hull]);
|
2018-06-27 21:37:10 +08:00
|
|
|
test_result_angle_list[2]
|
2018-07-11 01:24:09 +08:00
|
|
|
= intersectionLines(hull[extra_index_hull], hull[extra_next_index_hull],
|
|
|
|
hull[index_hull], hull[next_index_hull]);
|
2018-06-27 21:37:10 +08:00
|
|
|
test_result_angle_list[3]
|
2018-07-11 01:24:09 +08:00
|
|
|
= intersectionLines(hull[index_hull], hull[next_index_hull],
|
|
|
|
result_side_begin[0], result_side_end[0]);
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
const double test_diff_area
|
|
|
|
= fabs(fabs(contourArea(test_result_angle_list)) - experimental_area);
|
2018-07-14 05:51:04 +08:00
|
|
|
if (min_diff_area > test_diff_area)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-07-14 05:51:04 +08:00
|
|
|
min_diff_area = test_diff_area;
|
2018-06-27 21:37:10 +08:00
|
|
|
for (size_t i = 0; i < test_result_angle_list.size(); i++)
|
|
|
|
{
|
|
|
|
result_angle_list[i] = test_result_angle_list[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extra_index_hull = extra_next_index_hull;
|
|
|
|
}
|
|
|
|
while(extra_index_hull != unstable_pnt);
|
|
|
|
|
|
|
|
index_hull = next_index_hull;
|
|
|
|
}
|
|
|
|
while(index_hull != unstable_pnt);
|
2018-08-06 01:55:15 +08:00
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
// check label points
|
2018-08-06 01:55:15 +08:00
|
|
|
if (norm(result_angle_list[0] - angle_list[1]) > 2) { result_angle_list[0] = angle_list[1]; }
|
|
|
|
if (norm(result_angle_list[1] - angle_list[0]) > 2) { result_angle_list[1] = angle_list[0]; }
|
|
|
|
if (norm(result_angle_list[3] - angle_list[2]) > 2) { result_angle_list[3] = angle_list[2]; }
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
// check calculation point
|
|
|
|
if (norm(result_angle_list[2] - angle_list[3]) >
|
|
|
|
(norm(result_angle_list[0] - result_angle_list[1]) +
|
|
|
|
norm(result_angle_list[0] - result_angle_list[3])) * 0.5 )
|
|
|
|
{ result_angle_list[2] = angle_list[3]; }
|
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
return result_angle_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// / | b
|
|
|
|
// / |
|
|
|
|
// / |
|
|
|
|
// a/ | c
|
|
|
|
|
2018-08-07 22:37:51 +08:00
|
|
|
inline double QRDetect::getCosVectors(Point2f a, Point2f b, Point2f c)
|
2018-06-27 21:37:10 +08:00
|
|
|
{
|
2018-08-01 22:49:43 +08:00
|
|
|
return ((a - b).x * (c - b).x + (a - b).y * (c - b).y) / (norm(a - b) * norm(c - b));
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:24:09 +08:00
|
|
|
struct QRCodeDetector::Impl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Impl() { epsX = 0.2; epsY = 0.1; }
|
|
|
|
~Impl() {}
|
|
|
|
|
|
|
|
double epsX, epsY;
|
|
|
|
};
|
|
|
|
|
|
|
|
QRCodeDetector::QRCodeDetector() : p(new Impl) {}
|
|
|
|
QRCodeDetector::~QRCodeDetector() {}
|
|
|
|
|
|
|
|
void QRCodeDetector::setEpsX(double epsX) { p->epsX = epsX; }
|
|
|
|
void QRCodeDetector::setEpsY(double epsY) { p->epsY = epsY; }
|
|
|
|
|
|
|
|
bool QRCodeDetector::detect(InputArray in, OutputArray points) const
|
|
|
|
{
|
|
|
|
Mat inarr = in.getMat();
|
|
|
|
CV_Assert(!inarr.empty());
|
|
|
|
CV_Assert(inarr.type() == CV_8UC1);
|
2018-08-07 22:37:51 +08:00
|
|
|
QRDetect qrdet;
|
|
|
|
qrdet.init(inarr, p->epsX, p->epsY);
|
|
|
|
if (!qrdet.localization()) { return false; }
|
|
|
|
if (!qrdet.computeTransformationPoints()) { return false; }
|
|
|
|
vector<Point2f> pnts2f = qrdet.getTransformationPoints();
|
2018-07-11 01:24:09 +08:00
|
|
|
Mat(pnts2f).convertTo(points, points.fixedType() ? points.type() : CV_32FC2);
|
2018-06-27 21:37:10 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-18 00:01:02 +08:00
|
|
|
CV_EXPORTS bool detectQRCode(InputArray in, vector<Point> &points, double eps_x, double eps_y)
|
2018-07-11 01:24:09 +08:00
|
|
|
{
|
|
|
|
QRCodeDetector qrdetector;
|
|
|
|
qrdetector.setEpsX(eps_x);
|
|
|
|
qrdetector.setEpsY(eps_y);
|
|
|
|
|
|
|
|
return qrdetector.detect(in, points);
|
|
|
|
}
|
|
|
|
|
2018-08-18 00:01:02 +08:00
|
|
|
class QRDecode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void init(const Mat &src, const vector<Point2f> &points);
|
|
|
|
Mat getIntermediateBarcode() { return intermediate; }
|
|
|
|
Mat getStraightBarcode() { return straight; }
|
|
|
|
size_t getVersion() { return version; }
|
|
|
|
std::string getDecodeInformation() { return result_info; }
|
|
|
|
bool fullDecodingProcess();
|
|
|
|
protected:
|
|
|
|
bool updatePerspective();
|
|
|
|
bool versionDefinition();
|
|
|
|
bool samplingForVersion();
|
|
|
|
bool decodingProcess();
|
|
|
|
Mat original, no_border_intermediate, intermediate, straight;
|
|
|
|
vector<Point2f> original_points;
|
|
|
|
std::string result_info;
|
|
|
|
uint8_t version, version_size;
|
|
|
|
float test_perspective_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
void QRDecode::init(const Mat &src, const vector<Point2f> &points)
|
|
|
|
{
|
|
|
|
original = src.clone();
|
|
|
|
intermediate = Mat::zeros(src.size(), CV_8UC1);
|
|
|
|
original_points = points;
|
|
|
|
version = 0;
|
|
|
|
version_size = 0;
|
|
|
|
test_perspective_size = 251;
|
|
|
|
result_info = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QRDecode::updatePerspective()
|
|
|
|
{
|
2018-10-16 01:13:16 +08:00
|
|
|
const Point2f centerPt = QRDetect::intersectionLines(original_points[0], original_points[2],
|
|
|
|
original_points[1], original_points[3]);
|
|
|
|
if (cvIsNaN(centerPt.x) || cvIsNaN(centerPt.y))
|
|
|
|
return false;
|
|
|
|
|
2018-08-18 00:01:02 +08:00
|
|
|
const Size temporary_size(cvRound(test_perspective_size), cvRound(test_perspective_size));
|
|
|
|
|
|
|
|
vector<Point2f> perspective_points;
|
|
|
|
perspective_points.push_back(Point2f(0.f, 0.f));
|
|
|
|
perspective_points.push_back(Point2f(test_perspective_size, 0.f));
|
|
|
|
|
|
|
|
perspective_points.push_back(Point2f(test_perspective_size, test_perspective_size));
|
|
|
|
perspective_points.push_back(Point2f(0.f, test_perspective_size));
|
|
|
|
|
2018-10-16 01:13:16 +08:00
|
|
|
perspective_points.push_back(Point2f(test_perspective_size * 0.5f, test_perspective_size * 0.5f));
|
|
|
|
|
|
|
|
vector<Point2f> pts = original_points;
|
|
|
|
pts.push_back(centerPt);
|
|
|
|
|
|
|
|
Mat H = findHomography(pts, perspective_points);
|
|
|
|
Mat bin_original;
|
2018-08-18 00:01:02 +08:00
|
|
|
adaptiveThreshold(original, bin_original, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 83, 2);
|
2018-10-16 01:13:16 +08:00
|
|
|
Mat temp_intermediate;
|
2018-08-18 00:01:02 +08:00
|
|
|
warpPerspective(bin_original, temp_intermediate, H, temporary_size, INTER_NEAREST);
|
|
|
|
no_border_intermediate = temp_intermediate(Range(1, temp_intermediate.rows), Range(1, temp_intermediate.cols));
|
|
|
|
|
|
|
|
const int border = cvRound(0.1 * test_perspective_size);
|
|
|
|
const int borderType = BORDER_CONSTANT;
|
|
|
|
copyMakeBorder(no_border_intermediate, intermediate, border, border, border, border, borderType, Scalar(255));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:35:02 +08:00
|
|
|
inline Point computeOffset(const vector<Point>& v)
|
|
|
|
{
|
|
|
|
// compute the width/height of convex hull
|
|
|
|
Rect areaBox = boundingRect(v);
|
|
|
|
|
|
|
|
// compute the good offset
|
|
|
|
// the box is consisted by 7 steps
|
|
|
|
// to pick the middle of the stripe, it needs to be 1/14 of the size
|
|
|
|
const int cStep = 7 * 2;
|
|
|
|
Point offset = Point(areaBox.width, areaBox.height);
|
|
|
|
offset /= cStep;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2018-08-18 00:01:02 +08:00
|
|
|
bool QRDecode::versionDefinition()
|
|
|
|
{
|
|
|
|
LineIterator line_iter(intermediate, Point2f(0, 0), Point2f(test_perspective_size, test_perspective_size));
|
|
|
|
Point black_point = Point(0, 0);
|
|
|
|
for(int j = 0; j < line_iter.count; j++, ++line_iter)
|
|
|
|
{
|
|
|
|
const uint8_t value = intermediate.at<uint8_t>(line_iter.pos());
|
|
|
|
if (value == 0) { black_point = line_iter.pos(); break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat mask = Mat::zeros(intermediate.rows + 2, intermediate.cols + 2, CV_8UC1);
|
|
|
|
floodFill(intermediate, mask, black_point, 255, 0, Scalar(), Scalar(), FLOODFILL_MASK_ONLY);
|
|
|
|
|
|
|
|
vector<Point> locations, non_zero_elem;
|
|
|
|
Mat mask_roi = mask(Range(1, intermediate.rows - 1), Range(1, intermediate.cols - 1));
|
|
|
|
findNonZero(mask_roi, non_zero_elem);
|
|
|
|
convexHull(Mat(non_zero_elem), locations);
|
2018-10-24 08:35:02 +08:00
|
|
|
Point offset = computeOffset(locations);
|
2018-08-18 00:01:02 +08:00
|
|
|
|
|
|
|
Point temp_remote = locations[0], remote_point;
|
2018-10-24 08:35:02 +08:00
|
|
|
const Point delta_diff = offset;
|
2018-08-18 00:01:02 +08:00
|
|
|
for (size_t i = 0; i < locations.size(); i++)
|
|
|
|
{
|
2018-10-24 08:35:02 +08:00
|
|
|
if (norm(black_point - temp_remote) <= norm(black_point - locations[i]))
|
2018-08-18 00:01:02 +08:00
|
|
|
{
|
|
|
|
const uint8_t value = intermediate.at<uint8_t>(temp_remote - delta_diff);
|
|
|
|
temp_remote = locations[i];
|
2018-10-24 08:35:02 +08:00
|
|
|
if (value == 0) { remote_point = temp_remote - delta_diff; }
|
|
|
|
else { remote_point = temp_remote - (delta_diff / 2); }
|
2018-08-18 00:01:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t transition_x = 0 , transition_y = 0;
|
|
|
|
|
|
|
|
uint8_t future_pixel = 255;
|
|
|
|
const uint8_t *intermediate_row = intermediate.ptr<uint8_t>(remote_point.y);
|
|
|
|
for(int i = remote_point.x; i < intermediate.cols; i++)
|
|
|
|
{
|
|
|
|
if (intermediate_row[i] == future_pixel)
|
|
|
|
{
|
|
|
|
future_pixel = 255 - future_pixel;
|
|
|
|
transition_x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
future_pixel = 255;
|
|
|
|
for(int j = remote_point.y; j < intermediate.rows; j++)
|
|
|
|
{
|
|
|
|
const uint8_t value = intermediate.at<uint8_t>(Point(j, remote_point.x));
|
|
|
|
if (value == future_pixel)
|
|
|
|
{
|
|
|
|
future_pixel = 255 - future_pixel;
|
|
|
|
transition_y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
version = saturate_cast<uint8_t>((std::min(transition_x, transition_y) - 1) * 0.25 - 1);
|
|
|
|
if ( !( 0 < version && version <= 40 ) ) { return false; }
|
|
|
|
version_size = 21 + (version - 1) * 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QRDecode::samplingForVersion()
|
|
|
|
{
|
|
|
|
const double multiplyingFactor = (version < 3) ? 1 :
|
|
|
|
(version == 3) ? 1.5 :
|
|
|
|
version * (5 + version - 4);
|
|
|
|
const Size newFactorSize(
|
|
|
|
cvRound(no_border_intermediate.size().width * multiplyingFactor),
|
|
|
|
cvRound(no_border_intermediate.size().height * multiplyingFactor));
|
|
|
|
Mat postIntermediate(newFactorSize, CV_8UC1);
|
|
|
|
resize(no_border_intermediate, postIntermediate, newFactorSize, 0, 0, INTER_AREA);
|
|
|
|
|
|
|
|
const int no_inter_rows = postIntermediate.rows;
|
|
|
|
const int no_inter_cols = postIntermediate.cols;
|
|
|
|
const int delta_rows = cvRound((no_inter_rows * 1.0) / version_size);
|
|
|
|
const int delta_cols = cvRound((no_inter_cols * 1.0) / version_size);
|
|
|
|
|
|
|
|
vector<double> listFrequencyElem;
|
|
|
|
for (int r = 0; r < no_inter_rows; r += delta_rows)
|
|
|
|
{
|
|
|
|
for (int c = 0; c < no_inter_cols; c += delta_cols)
|
|
|
|
{
|
|
|
|
Mat tile = postIntermediate(
|
|
|
|
Range(r, min(r + delta_rows, no_inter_rows)),
|
|
|
|
Range(c, min(c + delta_cols, no_inter_cols)));
|
|
|
|
const double frequencyElem = (countNonZero(tile) * 1.0) / tile.total();
|
|
|
|
listFrequencyElem.push_back(frequencyElem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double dispersionEFE = std::numeric_limits<double>::max();
|
|
|
|
double experimentalFrequencyElem = 0;
|
|
|
|
for (double expVal = 0; expVal < 1; expVal+=0.001)
|
|
|
|
{
|
|
|
|
double testDispersionEFE = 0.0;
|
|
|
|
for (size_t i = 0; i < listFrequencyElem.size(); i++)
|
|
|
|
{
|
|
|
|
testDispersionEFE += (listFrequencyElem[i] - expVal) *
|
|
|
|
(listFrequencyElem[i] - expVal);
|
|
|
|
}
|
|
|
|
testDispersionEFE /= (listFrequencyElem.size() - 1);
|
|
|
|
if (dispersionEFE > testDispersionEFE)
|
|
|
|
{
|
|
|
|
dispersionEFE = testDispersionEFE;
|
|
|
|
experimentalFrequencyElem = expVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
straight = Mat(Size(version_size, version_size), CV_8UC1, Scalar(0));
|
|
|
|
size_t k = 0;
|
|
|
|
for (int r = 0; r < no_inter_rows &&
|
|
|
|
k < listFrequencyElem.size() &&
|
|
|
|
floor((r * 1.0) / delta_rows) < version_size; r += delta_rows)
|
|
|
|
{
|
|
|
|
for (int c = 0; c < no_inter_cols &&
|
|
|
|
k < listFrequencyElem.size() &&
|
|
|
|
floor((c * 1.0) / delta_cols) < version_size; c += delta_cols, k++)
|
|
|
|
{
|
|
|
|
Mat tile = postIntermediate(
|
|
|
|
Range(r, min(r + delta_rows, no_inter_rows)),
|
|
|
|
Range(c, min(c + delta_cols, no_inter_cols)));
|
|
|
|
|
|
|
|
if (listFrequencyElem[k] < experimentalFrequencyElem) { tile.setTo(0); }
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tile.setTo(255);
|
|
|
|
straight.at<uint8_t>(cvRound(floor((r * 1.0) / delta_rows)),
|
|
|
|
cvRound(floor((c * 1.0) / delta_cols))) = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QRDecode::decodingProcess()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_QUIRC
|
|
|
|
if (straight.empty()) { return false; }
|
|
|
|
|
|
|
|
quirc_code qr_code;
|
|
|
|
memset(&qr_code, 0, sizeof(qr_code));
|
|
|
|
|
|
|
|
qr_code.size = straight.size().width;
|
|
|
|
for (int x = 0; x < qr_code.size; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < qr_code.size; y++)
|
|
|
|
{
|
|
|
|
int position = y * qr_code.size + x;
|
|
|
|
qr_code.cell_bitmap[position >> 3]
|
|
|
|
|= straight.at<uint8_t>(y, x) ? 0 : (1 << (position & 7));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quirc_data qr_code_data;
|
|
|
|
quirc_decode_error_t errorCode = quirc_decode(&qr_code, &qr_code_data);
|
|
|
|
if (errorCode != 0) { return false; }
|
|
|
|
|
|
|
|
for (int i = 0; i < qr_code_data.payload_len; i++)
|
|
|
|
{
|
|
|
|
result_info += qr_code_data.payload[i];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QRDecode::fullDecodingProcess()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_QUIRC
|
|
|
|
if (!updatePerspective()) { return false; }
|
|
|
|
if (!versionDefinition()) { return false; }
|
|
|
|
if (!samplingForVersion()) { return false; }
|
|
|
|
if (!decodingProcess()) { return false; }
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
std::cout << "Library QUIRC is not linked. No decoding is performed. Take it to the OpenCV repository." << std::endl;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_EXPORTS bool decodeQRCode(InputArray in, InputArray points, std::string &decoded_info, OutputArray straight_qrcode)
|
|
|
|
{
|
|
|
|
Mat inarr = in.getMat();
|
|
|
|
CV_Assert(!inarr.empty());
|
|
|
|
inarr.convertTo(inarr, CV_8UC1);
|
|
|
|
|
|
|
|
CV_Assert(points.isVector());
|
|
|
|
vector<Point2f> src_points;
|
|
|
|
points.copyTo(src_points);
|
|
|
|
CV_Assert(src_points.size() == 4);
|
2018-10-16 01:13:16 +08:00
|
|
|
CV_CheckGT(contourArea(src_points), 0.0, "Invalid QR code source points");
|
2018-08-18 00:01:02 +08:00
|
|
|
|
|
|
|
QRDecode qrdec;
|
|
|
|
qrdec.init(inarr, src_points);
|
|
|
|
bool exit_flag = qrdec.fullDecodingProcess();
|
|
|
|
|
|
|
|
decoded_info = qrdec.getDecodeInformation();
|
|
|
|
|
2018-10-16 01:13:16 +08:00
|
|
|
if (exit_flag && straight_qrcode.needed())
|
2018-08-18 00:01:02 +08:00
|
|
|
{
|
|
|
|
qrdec.getStraightBarcode().convertTo(straight_qrcode,
|
|
|
|
straight_qrcode.fixedType() ?
|
|
|
|
straight_qrcode.type() : CV_32FC2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return exit_flag;
|
|
|
|
}
|
|
|
|
|
2018-06-27 21:37:10 +08:00
|
|
|
}
|