This commit is contained in:
michael
2026-01-17 02:49:32 +01:00
parent a1ccda2e88
commit 4905663933
283 changed files with 32074 additions and 15759 deletions

View File

@@ -1,4 +1,8 @@
#include "defines.h"
#include "CAlignAndCutImage.h"
#include "ClassFlowAlignment.h"
#include "ClassControllCamera.h"
#include "CRotateImage.h"
#include "ClassLogFile.h"
@@ -6,9 +10,8 @@
#include <algorithm>
#include <esp_log.h>
#include "psram.h"
#include "../../include/defines.h"
static const char* TAG = "c_align_and_cut_image";
static const char *TAG = "c_align_and_cut_image";
CAlignAndCutImage::CAlignAndCutImage(std::string _name, CImageBasis *_org, CImageBasis *_temp) : CImageBasis(_name)
{
@@ -18,10 +21,8 @@ CAlignAndCutImage::CAlignAndCutImage(std::string _name, CImageBasis *_org, CImag
width = _org->width;
height = _org->height;
bpp = _org->bpp;
externalImage = true;
islocked = false;
externalImage = true;
islocked = false;
ImageTMP = _temp;
}
@@ -33,118 +34,122 @@ void CAlignAndCutImage::GetRefSize(int *ref_dx, int *ref_dy)
ref_dy[1] = t1_dy;
}
bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
int CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
{
int dx, dy;
int r0_x, r0_y, r1_x, r1_y;
bool isSimilar1, isSimilar2;
CFindTemplate *ft = new CFindTemplate("align", rgb_image, channels, width, height, bpp);
CFindTemplate* ft = new CFindTemplate("align", rgb_image, channels, width, height, bpp);
//////////////////////////////////////////////
bool isSimilar1 = ft->FindTemplate(_temp1); // search the alignment image 1
r0_x = _temp1->target_x;
r0_y = _temp1->target_y;
ESP_LOGD(TAG, "Before ft->FindTemplate(_temp1); %s", _temp1->image_file.c_str());
isSimilar1 = ft->FindTemplate(_temp1);
_temp1->width = ft->tpl_width;
_temp1->height = ft->tpl_height;
_temp1->height = ft->tpl_height;
int x1_relative_shift = (_temp1->target_x - _temp1->found_x);
int y1_relative_shift = (_temp1->target_y - _temp1->found_y);
int x1_absolute_shift = _temp1->target_x + (_temp1->target_x - _temp1->found_x);
int y1_absolute_shift = _temp1->target_y + (_temp1->target_y - _temp1->found_y);
//////////////////////////////////////////////
bool isSimilar2 = ft->FindTemplate(_temp2); // search the alignment image 2
r1_x = _temp2->target_x;
r1_y = _temp2->target_y;
ESP_LOGD(TAG, "Before ft->FindTemplate(_temp2); %s", _temp2->image_file.c_str());
isSimilar2 = ft->FindTemplate(_temp2);
_temp2->width = ft->tpl_width;
_temp2->height = ft->tpl_height;
_temp2->height = ft->tpl_height;
int x2_relative_shift = (_temp2->target_x - _temp2->found_x);
int y2_relative_shift = (_temp2->target_y - _temp2->found_y);
int x2_absolute_shift = _temp2->target_x + (_temp2->target_x - _temp2->found_x);
int y2_absolute_shift = _temp2->target_y + (_temp2->target_y - _temp2->found_y);
delete ft;
int ret = Alignment_OK;
dx = _temp1->target_x - _temp1->found_x;
dy = _temp1->target_y - _temp1->found_y;
//////////////////////////////////////////////
float radians_org = atan2((_temp2->found_y - _temp1->found_y), (_temp2->found_x - _temp1->found_x));
float radians_cur = atan2((y2_absolute_shift - y1_absolute_shift), (x2_absolute_shift - x1_absolute_shift));
float rotate_angle = (radians_cur - radians_org) * 180 / M_PI; // radians to degrees
r0_x += dx;
r0_y += dy;
//////////////////////////////////////////////
if ((fabs(rotate_angle) > _temp1->search_max_angle) || (fabs(rotate_angle) > _temp2->search_max_angle))
{
ret = Rotation_Alignment_Failed;
}
r1_x += dx;
r1_y += dy;
float w_org, w_ist, d_winkel;
w_org = atan2(_temp2->found_y - _temp1->found_y, _temp2->found_x - _temp1->found_x);
w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
d_winkel = (w_ist - w_org) * 180 / M_PI;
/*#ifdef DEBUG_DETAIL_ON
std::string zw = "\tdx:\t" + std::to_string(dx) + "\tdy:\t" + std::to_string(dy) + "\td_winkel:\t" + std::to_string(d_winkel);
zw = zw + "\tt1_x_y:\t" + std::to_string(_temp1->found_x) + "\t" + std::to_string(_temp1->found_y);
zw = zw + "\tpara1_found_min_avg_max_SAD:\t" + std::to_string(_temp1->fastalg_min) + "\t" + std::to_string(_temp1->fastalg_avg) + "\t" + std::to_string(_temp1->fastalg_max) + "\t"+ std::to_string(_temp1->fastalg_SAD);
zw = zw + "\tt2_x_y:\t" + std::to_string(_temp2->found_x) + "\t" + std::to_string(_temp2->found_y);
zw = zw + "\tpara2_found_min_avg_max:\t" + std::to_string(_temp2->fastalg_min) + "\t" + std::to_string(_temp2->fastalg_avg) + "\t" + std::to_string(_temp2->fastalg_max) + "\t"+ std::to_string(_temp2->fastalg_SAD);
LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
#endif*/
if ((abs(x1_relative_shift) >= _temp1->search_x) || (abs(y1_relative_shift) >= _temp1->search_y))
{
ret = Shift_Alignment_Failed;
}
//////////////////////////////////////////////
CRotateImage rt("Align", this, ImageTMP);
rt.Translate(dx, dy);
rt.Rotate(d_winkel, _temp1->target_x, _temp1->target_y);
ESP_LOGD(TAG, "Alignment: dx %d - dy %d - rot %f", dx, dy, d_winkel);
return (isSimilar1 && isSimilar2);
if (rotate_angle != 0)
{
rt.Translate(x1_relative_shift, y1_relative_shift);
if (Camera.ImageAntialiasing)
{
rt.RotateAntiAliasing(rotate_angle, _temp1->target_x, _temp1->target_y);
}
else
{
rt.Rotate(rotate_angle, _temp1->target_x, _temp1->target_y);
}
}
else if (x1_relative_shift != 0 || y1_relative_shift != 0)
{
rt.Translate(x1_relative_shift, y1_relative_shift);
}
return ((isSimilar1 && isSimilar2) ? Fast_Alignment_OK : ret);
}
void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
{
int x2, y2;
x2 = x1 + dx;
y2 = y1 + dy;
x2 = std::min(x2, width - 1);
y2 = std::min(y2, height - 1);
int x2 = std::min((x1 + dx), (width - 1));
int y2 = std::min((y1 + dy), (height - 1));
dx = x2 - x1;
dy = y2 - y1;
int memsize = dx * dy * channels;
uint8_t* odata = (unsigned char*) malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
uint8_t *temp_image = (unsigned char *)malloc_psram_heap(std::string(TAG) + "->temp_image", memsize, MALLOC_CAP_SPIRAM);
stbi_uc* p_target;
stbi_uc* p_source;
stbi_uc *p_target;
stbi_uc *p_source;
RGBImageLock();
for (int x = x1; x < x2; ++x)
{
for (int y = y1; y < y2; ++y)
{
p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
p_target = temp_image + (channels * ((y - y1) * dx + (x - x1)));
p_source = rgb_image + (channels * (y * width + x));
for (int _channels = 0; _channels < channels; ++_channels)
{
p_target[_channels] = p_source[_channels];
}
}
}
#ifdef STBI_ONLY_JPEG
stbi_write_jpg(_template1.c_str(), dx, dy, channels, odata, 100);
stbi_write_jpg(_template1.c_str(), dx, dy, channels, temp_image, 100);
#else
stbi_write_bmp(_template1.c_str(), dx, dy, channels, odata);
stbi_write_bmp(_template1.c_str(), dx, dy, channels, temp_image);
#endif
RGBImageRelease();
stbi_image_free(odata);
stbi_image_free(temp_image);
}
void CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *_target)
{
int x2, y2;
x2 = x1 + dx;
y2 = y1 + dy;
x2 = std::min(x2, width - 1);
y2 = std::min(y2, height - 1);
int x2 = std::min((x1 + dx), (width - 1));
int y2 = std::min((y1 + dy), (height - 1));
dx = x2 - x1;
dy = y2 - y1;
@@ -155,57 +160,63 @@ void CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *
return;
}
uint8_t* odata = _target->RGBImageLock();
uint8_t *temp_image = _target->RGBImageLock();
RGBImageLock();
stbi_uc* p_target;
stbi_uc* p_source;
stbi_uc *p_target;
stbi_uc *p_source;
for (int x = x1; x < x2; ++x)
{
for (int y = y1; y < y2; ++y)
{
p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
p_target = temp_image + (channels * ((y - y1) * dx + (x - x1)));
p_source = rgb_image + (channels * (y * width + x));
for (int _channels = 0; _channels < channels; ++_channels)
{
p_target[_channels] = p_source[_channels];
}
}
}
RGBImageRelease();
_target->RGBImageRelease();
}
CImageBasis* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
CImageBasis *CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
{
int x2, y2;
x2 = x1 + dx;
y2 = y1 + dy;
x2 = std::min(x2, width - 1);
y2 = std::min(y2, height - 1);
int x2 = std::min((x1 + dx), (width - 1));
int y2 = std::min((y1 + dy), (height - 1));
dx = x2 - x1;
dy = y2 - y1;
int memsize = dx * dy * channels;
uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
uint8_t *temp_image = (unsigned char *)malloc_psram_heap(std::string(TAG) + "->temp_image", memsize, MALLOC_CAP_SPIRAM);
stbi_uc* p_target;
stbi_uc* p_source;
stbi_uc *p_target;
stbi_uc *p_source;
RGBImageLock();
for (int x = x1; x < x2; ++x)
{
for (int y = y1; y < y2; ++y)
{
p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
p_target = temp_image + (channels * ((y - y1) * dx + (x - x1)));
p_source = rgb_image + (channels * (y * width + x));
for (int _channels = 0; _channels < channels; ++_channels)
{
p_target[_channels] = p_source[_channels];
}
}
}
CImageBasis* rs = new CImageBasis("CutAndSave", odata, channels, dx, dy, bpp);
CImageBasis *rs = new CImageBasis("CutAndSave", temp_image, channels, dx, dy, bpp);
RGBImageRelease();
rs->SetIndepended();
return rs;
}