8#include "TinyRobotics/utils/AllocatorPSRAM.h"
10namespace tinyrobotics {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
43 CameraImageDiff() =
default;
45 void process(
const uint8_t* image, size_t width, size_t height) {
47 this->height_ = height;
48 diff_.resize(width * height);
49 prevImage_.resize(width * height);
50 for (uint32_t i = 0; i < width_ * height_; ++i) {
51 diff_[i] = (uint8_t)abs(int16_t(image[i]) - int16_t(prevImage_[i]));
53 memcpy(prevImage_.data(), image, width * height);
58 mask_.resize(width_ * height_);
59 for (uint32_t i = 0; i < width_ * height_; ++i) {
60 mask_[i] = (diff_[i] > thresh) ? 255 : 0;
67 for (uint32_t i = 0; i < width_ * height_; ++i)
68 if (mask_[i] != 0) ++cnt;
73
74
75
76
78 uint32_t left = 0, center = 0, right = 0;
79 int leftEnd = width_ / 3;
80 int centerEnd = 2 * width_ / 3;
81 for (
int y = 0; y < height_; ++y) {
82 for (
int x = 0; x < width_; ++x) {
83 int idx = y * width_ + x;
84 if (mask_[idx] != 0) {
87 else if (x < centerEnd)
94 return std::make_tuple(left, center, right);
98
99
100
101
103 uint32_t top = 0, center = 0, bottom = 0;
104 int topEnd = height_ / 3;
105 int centerEnd = 2 * height_ / 3;
106 for (
int y = 0; y < height_; ++y) {
107 for (
int x = 0; x < width_; ++x) {
108 int idx = y * width_ + x;
109 if (mask_[idx] != 0) {
112 else if (y < centerEnd)
119 return std::make_tuple(top, center, bottom);
123 prevImage_.resize(0);
127 int getWidth()
const {
return width_; }
128 int getHeight()
const {
return height_; }
129 int getPixelCount()
const {
return width_ * height_; }
132 uint8_t*
getDiff() {
return diff_.data(); }
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: AllocatorPSRAM.h:21
Computes pixel-wise differences between consecutive grayscale camera images.
Definition: CameraImageDiff.h:41
std::tuple< uint32_t, uint32_t, uint32_t > countChangesSplitVertical()
Definition: CameraImageDiff.h:77
uint32_t countChanges()
Count number of pixels above threshold.
Definition: CameraImageDiff.h:65
std::tuple< uint32_t, uint32_t, uint32_t > countChangesSplitHorizontal()
Definition: CameraImageDiff.h:102
void threshold(uint8_t thresh)
Simple thresholding: convert difference to binary mask.
Definition: CameraImageDiff.h:57
uint8_t * getDiff()
Definition: CameraImageDiff.h:132