summaryrefslogtreecommitdiff
path: root/src/image.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/image.cpp')
-rw-r--r--src/image.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/image.cpp b/src/image.cpp
new file mode 100644
index 0000000..a9280a4
--- /dev/null
+++ b/src/image.cpp
@@ -0,0 +1,110 @@
+#include "image.h"
+
+#include "macro.h"
+#include "pixels.h"
+
+float process_column(const uint16_t (&column)[])
+{
+ start_timer(process_column);
+
+ float result = std::numeric_limits<float>::quiet_NaN();
+
+ constexpr uint32_t signalThreshold = 900; // = SKO * sqrt(patternSize)
+ static constexpr uint32_t patternOffset = patternSize -
+ ((patternSize % 2 == 1) ? 1 : 0);
+ const uint32_t correlationSize = img_height - patternSize +
+ ((patternSize % 2 == 1) ? 1 : 0);
+ uint32_t correlation[img_height];
+ uint32_t integralSum[img_height];
+ uint32_t maxSum = signalThreshold * 50;
+ uint32_t x1 = 0;
+ int32_t y1 = 0;
+ int32_t y2 = 0;
+
+ memset(correlation, 0, img_height * sizeof(correlation[0]));
+ integralSum[0] = 0;
+
+ for (uint32_t i = 1; i < img_height; ++i)
+ {
+ // if (column[i] < 100)
+ // {
+ // column[i] = 0;
+ // }
+
+ integralSum[i] = column[i] / 256 + integralSum[i - 1];
+ }
+
+ for (uint32_t i = 0; i < correlationSize; ++i)
+ correlation[i + patternSize / 2] = column[i + patternSize / 2] / 256 *
+ (integralSum[i + patternOffset] -
+ integralSum[i]);
+
+ for (uint32_t i = 3; i < img_height - 2; ++i)
+ {
+ const auto sum = correlation[i - 1] + correlation[i] +
+ correlation[i + 1];
+
+ if (sum > maxSum)
+ {
+ const int32_t rioux0 = int32_t(correlation[i - 2 - 1] +
+ correlation[i - 1 - 1]) -
+ int32_t(correlation[i + 1 - 1] +
+ correlation[i + 2 - 1]);
+
+ if (rioux0 < 0)
+ {
+ const int32_t rioux1 = int32_t(correlation[i - 2] +
+ correlation[i - 1]) -
+ int32_t(correlation[i + 1] +
+ correlation[i + 2]);
+
+ if (rioux1 >= 0)
+ {
+ x1 = i - 1;
+ y1 = rioux0;
+ y2 = rioux1;
+ maxSum = sum;
+ }
+ }
+ }
+ }
+
+ result = (y2 != y1) ? (float(x1) - (float(y1) / (y2 - y1)))
+ : std::numeric_limits<float>::quiet_NaN();
+
+ return result;
+}
+
+void Image::rotate()
+{
+ start_timer(rotate);
+
+ using namespace std;
+
+ for (size_t i = 0; i < img_height; ++i)
+ {
+ for (size_t j = 0; j < img_width; ++j)
+ {
+ rotated_cw[j][i] = data[img_height - i][j];
+ }
+ }
+
+ stop_timer(rotate);
+}
+
+std::shared_ptr<Pixels> Image::pixels() const
+{
+ auto result = std::make_shared<Pixels>();
+ result->counters = counters;
+
+ start_timer(process_columns);
+
+ for (size_t i = 0; i < width; i++)
+ {
+ result->pixels[i] = process_column(rotated_cw[i]);
+ }
+
+ stop_timer(process_columns);
+
+ return result;
+}