Ocean
AlignmentPatternDetector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
11 
12 #include "ocean/base/Frame.h"
13 #include "ocean/base/Memory.h"
14 
15 #include "ocean/cv/Bresenham.h"
16 
17 #include "ocean/math/Box2.h"
18 #include "ocean/math/Vector2.h"
19 
20 namespace Ocean
21 {
22 
23 namespace CV
24 {
25 
26 namespace Detector
27 {
28 
29 namespace QRCodes
30 {
31 
32 /**
33  * This class implements an alignment pattern inside QR codes.
34  * @ingroup cvdetectorqrcodes
35  */
37 {
38  public:
39 
40  /**
41  * Default constructor that creates an invalid alignment pattern
42  */
43  AlignmentPattern() = default;
44 
45  /**
46  * Constructor.
47  * @param center The center location at which this alignment pattern was detected
48  * @param averageSegmentSize The average segment size in pixels that has been measured during the detection, range: [1, infinity)
49  */
50  inline AlignmentPattern(const Vector2& center, const unsigned int averageSegmentSize);
51 
52  /**
53  * Returns the center location at which this alignment pattern was detected
54  * @return The location of the center
55  */
56  inline const Vector2& center() const;
57 
58  /**
59  * Returns the average segment size in pixels that has been measured during the detection
60  * @return The average segment size
61  */
62  inline unsigned int averageSegmentSize() const;
63 
64  /**
65  * Returns true if this alignment pattern is valid
66  * @return True if this alignment pattern is valid, otherwise false
67  */
68  inline bool isValid() const;
69 
70  protected:
71 
72  /// The center location of the alignment pattern
74 
75  /// The average segment size in pixels that has been measured during the detection
76  unsigned int averageSegmentSize_ = 0u;
77 };
78 
79 /// Definition of a vector of alignment patterns
80 typedef std::vector<AlignmentPattern> AlignmentPatterns;
81 
82 /**
83  * This class implements a detector for alignment patterns inside QR codes.
84  * @ingroup cvdetectorqrcodes
85  */
86 class OCEAN_CV_DETECTOR_QRCODES_EXPORT AlignmentPatternDetector
87 {
88  public:
89 
90  /**
91  * Detects alignment patterns in the specified search region
92  * @param yFrame Pointer to the 8-bit grayscale input image in which alignment patterns will be searched, must be valid, origin must be in the upper left
93  * @param width The width of the input image, range: [searchX + searchWidth, infinity)
94  * @param height The height of the input image, range: [searchY + searchHeight, infinity)
95  * @param paddingElements The padding elements of the input image, range: [0, infinity)
96  * @param searchX The x-coordinate of the top-left corner of the search region, range: [0, width - searchWidth]
97  * @param searchY The y-coordinate of the top-left corner of the search region, range: [0, height - searchHeight]
98  * @param searchWidth The width of the search region, range: [1, width - searchX]
99  * @param searchHeight The height of the search region, range: [1, height - searchY]
100  * @param isNormalReflectance Indicates whether alignment patterns with normal or inverted reflectance are searched
101  * @param grayThreshold The gray value that has been determined as the separation between foreground and background modules (cf. `FinderPattern::grayThreshold()`)
102  * @return The list of detected alignment patterns
103  */
104  static AlignmentPatterns detectAlignmentPatterns(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int searchX, const unsigned int searchY, const unsigned int searchWidth, const unsigned int searchHeight, const bool isNormalReflectance, const unsigned int grayThreshold);
105 
106  protected:
107 
108  /**
109  * Detects alignment patterns in a row of an image
110  * @param yFrame Pointer to the 8-bit grayscale input image in which alignment patterns will be searched, must be valid, origin must be in the upper left
111  * @param width The width of the input image, range: [searchX + searchWidth, infinity)
112  * @param height The height of the input image, range: [searchY + searchHeight, infinity)
113  * @param paddingElements The padding elements of the input image, range: [0, infinity)
114  * @param row The row of the input image in which alignment patterns will be searched, range: [0, height)
115  * @param offsetX The offset in x-direction that will be added to the location of detected alignment patterns, range: [0, infinity)
116  * @param offsetY The offset in y-direction that will be added to the location of detected alignment patterns, range: [0, infinity)
117  * @param isNormalReflectance Indicates whether alignment patterns with normal or inverted reflectance are searched
118  * @param grayThreshold The gray value that has been determined as the separation between foreground and background modules (cf. `FinderPattern::grayThreshold()`), range: [0, 255]
119  * @param alignmentPatterns The resulting list of alignment patterns that have been found in the selected row
120  */
121  static void detectAlignmentPatternsInRow(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int row, const unsigned int xOffset, const unsigned int yOffset, const bool isNormalReflectance, const unsigned int grayThreshold, AlignmentPatterns& alignmentPatterns);
122 
123  /**
124  * Performs a circular check around a candidate location to confirm a detection in a row
125  * @param yFrame Pointer to the 8-bit grayscale input image in which alignment patterns will be searched, must be valid, origin must be in the upper left
126  * @param width The width of the input image, range: [searchX + searchWidth, infinity)
127  * @param height The height of the input image, range: [searchY + searchHeight, infinity)
128  * @param paddingElements The padding elements of the input image, range: [0, infinity)
129  * @param xCenter The x-coordinate of the candidate location that will be checked vertically, range: [0, width)
130  * @param yCenter The y-coordinate of the candidate location that will be checked vertically, range: [0, height)
131  * @param minLength The minimum length in pixels of the segments that are expected, range: [1, infinity)
132  * @param maxLength The maximum length in pixels of the segments that are expected, range: [1, infinity)
133  * @param isNormalReflectance Indicates whether alignment patterns with normal or inverted reflectance are searched
134  * @param grayThreshold The gray value that has been determined as the separation between foreground and background modules, range: [0, 255]
135  * @return True, if an acceptable sequence of fore- and background segments has been detected in all directions, otherwise false
136  */
137  static bool checkInCircle(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int xCenter, const unsigned int yCenter, const unsigned int minLength, const unsigned int maxLength, const bool isNormalReflectance, const unsigned int grayThreshold);
138 
139  /**
140  * Performs a check to confirm a detection in a specified direction
141  * @param yFrame Pointer to the 8-bit grayscale input image in which alignment patterns will be searched, must be valid, origin must be in the upper left
142  * @param width The width of the input image, range: [searchX + searchWidth, infinity)
143  * @param height The height of the input image, range: [searchY + searchHeight, infinity)
144  * @param paddingElements The padding elements of the input image, range: [0, infinity)
145  * @param xCenter The x-coordinate of the candidate location that will be checked vertically, range: [0, width)
146  * @param yCenter The y-coordinate of the candidate location that will be checked vertically, range: [0, height)
147  * @param minLength The minimum length in pixels of the segments that are expected, range: [1, infinity)
148  * @param maxLength The maximum length in pixels of the segments that are expected, range: [1, infinity)
149  * @param isNormalReflectance Indicates whether alignment patterns with normal or inverted reflectance are searched
150  * @param grayThreshold The gray value that has been determined as the separation between foreground and background modules, range: [0, 255]
151  * @param angle The angle (in RAD) of the search direction, range: [0, PI)
152  * @return True, if an acceptable sequence of fore- and background segments has been detected in all directions, otherwise false
153  */
154  static bool checkInDirection(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int xCenter, const unsigned int yCenter, const unsigned int minLength, const unsigned int maxLength, const bool isNormalReflectance, const unsigned int grayThreshold, const Scalar angle);
155 };
156 
157 inline AlignmentPattern::AlignmentPattern(const Vector2& center, const unsigned int averageSegmentSize) :
158  center_(center),
159  averageSegmentSize_(averageSegmentSize)
160 {
161  ocean_assert(isValid());
162 }
163 
164 inline const Vector2& AlignmentPattern::center() const
165 {
166  return center_;
167 }
168 
169 inline unsigned int AlignmentPattern::averageSegmentSize() const
170 {
171  return averageSegmentSize_;
172 }
173 
174 inline bool AlignmentPattern::isValid() const
175 {
176  return center_.x() >= Scalar(0) && center_.y() >= Scalar(0) && averageSegmentSize_ != 0u;
177 }
178 
179 } // namespace QRCodes
180 
181 } // namespace Detector
182 
183 } // namespace CV
184 
185 } // namespace Ocean
This class implements a detector for alignment patterns inside QR codes.
Definition: AlignmentPatternDetector.h:87
static bool checkInDirection(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int xCenter, const unsigned int yCenter, const unsigned int minLength, const unsigned int maxLength, const bool isNormalReflectance, const unsigned int grayThreshold, const Scalar angle)
Performs a check to confirm a detection in a specified direction.
static bool checkInCircle(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int xCenter, const unsigned int yCenter, const unsigned int minLength, const unsigned int maxLength, const bool isNormalReflectance, const unsigned int grayThreshold)
Performs a circular check around a candidate location to confirm a detection in a row.
static void detectAlignmentPatternsInRow(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int row, const unsigned int xOffset, const unsigned int yOffset, const bool isNormalReflectance, const unsigned int grayThreshold, AlignmentPatterns &alignmentPatterns)
Detects alignment patterns in a row of an image.
static AlignmentPatterns detectAlignmentPatterns(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int paddingElements, const unsigned int searchX, const unsigned int searchY, const unsigned int searchWidth, const unsigned int searchHeight, const bool isNormalReflectance, const unsigned int grayThreshold)
Detects alignment patterns in the specified search region.
This class implements an alignment pattern inside QR codes.
Definition: AlignmentPatternDetector.h:37
bool isValid() const
Returns true if this alignment pattern is valid.
Definition: AlignmentPatternDetector.h:174
Vector2 center_
The center location of the alignment pattern.
Definition: AlignmentPatternDetector.h:73
unsigned int averageSegmentSize_
The average segment size in pixels that has been measured during the detection.
Definition: AlignmentPatternDetector.h:76
AlignmentPattern()=default
Default constructor that creates an invalid alignment pattern.
unsigned int averageSegmentSize() const
Returns the average segment size in pixels that has been measured during the detection.
Definition: AlignmentPatternDetector.h:169
const Vector2 & center() const
Returns the center location at which this alignment pattern was detected.
Definition: AlignmentPatternDetector.h:164
static constexpr T minValue()
Returns the min scalar value.
Definition: Numeric.h:3250
const T & x() const noexcept
Returns the x value.
Definition: Vector2.h:698
const T & y() const noexcept
Returns the y value.
Definition: Vector2.h:710
float Scalar
Definition of a scalar type.
Definition: Math.h:128
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition: Vector2.h:21
std::vector< AlignmentPattern > AlignmentPatterns
Definition of a vector of alignment patterns.
Definition: AlignmentPatternDetector.h:80
std::vector< QRCode > QRCodes
Definition of a vector of QR codes.
Definition: QRCode.h:25
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15