mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-06 19:46:54 +03:00
Inital
This commit is contained in:
@@ -14,60 +14,114 @@ using namespace std;
|
||||
|
||||
#define GET_MEMORY malloc
|
||||
|
||||
/*
|
||||
CResizeImage::CResizeImage(std::string _image, int _new_dx, int _new_dy)
|
||||
void writejpghelp(void *context, void *data, int size)
|
||||
{
|
||||
CImageBasis::CImageBasis(_image);
|
||||
// printf("Size all: %d, size %d\n", ((ImageData*)context)->size, size);
|
||||
ImageData* _zw = (ImageData*) context;
|
||||
uint8_t *voidstart = _zw->data;
|
||||
uint8_t *datastart = (uint8_t*) data;
|
||||
voidstart += _zw->size;
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
*(voidstart + i) = *(datastart + i);
|
||||
|
||||
_zw->size += size;
|
||||
}
|
||||
|
||||
|
||||
ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
|
||||
{
|
||||
ImageData* ii = new ImageData;
|
||||
|
||||
auto rv2 = stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
|
||||
|
||||
return ii;
|
||||
}
|
||||
|
||||
bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
|
||||
{
|
||||
int gr = height * width * channels;
|
||||
if (gr != _size) // Größe passt nicht
|
||||
{
|
||||
printf("Kann Bild nicht von Speicher kopierte - Größen passen nicht zusammen: soll %d, ist %d\n", _size, gr);
|
||||
return false;
|
||||
}
|
||||
memCopy(_source, rgb_image, _size);
|
||||
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
|
||||
{
|
||||
stbi_uc* p_source;
|
||||
p_source = this->rgb_image + (this->channels * (y * this->width + x));
|
||||
p_source = rgb_image + (channels * (y * width + x));
|
||||
return p_source[ch];
|
||||
}
|
||||
|
||||
void CResizeImage::Resize(int _new_dx, int _new_dy)
|
||||
{
|
||||
int memsize = _new_dx * _new_dy * this->channels;
|
||||
int memsize = _new_dx * _new_dy * channels;
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
|
||||
stbir_resize_uint8(this->rgb_image, this->width, this->height, 0, odata, _new_dx, _new_dy, 0, this->channels);
|
||||
stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
|
||||
|
||||
stbi_image_free(this->rgb_image);
|
||||
this->rgb_image = (unsigned char*)GET_MEMORY(memsize);
|
||||
stbi_image_free(rgb_image);
|
||||
rgb_image = (unsigned char*)GET_MEMORY(memsize);
|
||||
|
||||
this->memCopy(odata, this->rgb_image, memsize);
|
||||
this->width = _new_dx;
|
||||
this->height = _new_dy;
|
||||
memCopy(odata, rgb_image, memsize);
|
||||
width = _new_dx;
|
||||
height = _new_dy;
|
||||
stbi_image_free(odata);
|
||||
}
|
||||
|
||||
|
||||
CRotate::CRotate(CImageBasis *_org, CImageBasis *_temp)
|
||||
{
|
||||
rgb_image = _org->rgb_image;
|
||||
channels = _org->channels;
|
||||
width = _org->width;
|
||||
height = _org->height;
|
||||
bpp = _org->bpp;
|
||||
externalImage = true;
|
||||
ImageTMP = _temp;
|
||||
}
|
||||
|
||||
void CRotate::Mirror(){
|
||||
int memsize = this->width * this->height * this->channels;
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
int memsize = width * height * channels;
|
||||
uint8_t* odata;
|
||||
if (ImageTMP)
|
||||
{
|
||||
odata = ImageTMP->rgb_image;
|
||||
}
|
||||
else
|
||||
{
|
||||
odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
}
|
||||
|
||||
|
||||
int x_source, y_source;
|
||||
stbi_uc* p_target;
|
||||
stbi_uc* p_source;
|
||||
|
||||
for (int x = 0; x < this->width; ++x)
|
||||
for (int y = 0; y < this->height; ++y)
|
||||
for (int x = 0; x < width; ++x)
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
p_target = odata + (this->channels * (y * this->width + x));
|
||||
p_target = odata + (channels * (y * width + x));
|
||||
|
||||
x_source = this->width - x;
|
||||
x_source = width - x;
|
||||
y_source = y;
|
||||
|
||||
p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
|
||||
for (int channels = 0; channels < this->channels; ++channels)
|
||||
p_target[channels] = p_source[channels];
|
||||
p_source = rgb_image + (channels * (y_source * width + x_source));
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_target[_channels] = p_source[_channels];
|
||||
}
|
||||
|
||||
// memcpy(this->rgb_image, odata, memsize);
|
||||
this->memCopy(odata, this->rgb_image, memsize);
|
||||
stbi_image_free(odata);
|
||||
// memcpy(rgb_image, odata, memsize);
|
||||
memCopy(odata, rgb_image, memsize);
|
||||
if (!ImageTMP)
|
||||
{
|
||||
stbi_image_free(odata);
|
||||
}
|
||||
}
|
||||
|
||||
void CRotate::Rotate(float _angle, int _centerx, int _centery)
|
||||
@@ -86,17 +140,26 @@ void CRotate::Rotate(float _angle, int _centerx, int _centery)
|
||||
m[1][1] = m[0][0];
|
||||
m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
|
||||
|
||||
int memsize = this->width * this->height * this->channels;
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
int memsize = width * height * channels;
|
||||
uint8_t* odata;
|
||||
if (ImageTMP)
|
||||
{
|
||||
odata = ImageTMP->rgb_image;
|
||||
}
|
||||
else
|
||||
{
|
||||
odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
}
|
||||
|
||||
|
||||
int x_source, y_source;
|
||||
stbi_uc* p_target;
|
||||
stbi_uc* p_source;
|
||||
|
||||
for (int x = 0; x < this->width; ++x)
|
||||
for (int y = 0; y < this->height; ++y)
|
||||
for (int x = 0; x < width; ++x)
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
p_target = odata + (this->channels * (y * this->width + x));
|
||||
p_target = odata + (channels * (y * width + x));
|
||||
|
||||
x_source = int(m[0][0] * x + m[0][1] * y);
|
||||
y_source = int(m[1][0] * x + m[1][1] * y);
|
||||
@@ -104,94 +167,112 @@ void CRotate::Rotate(float _angle, int _centerx, int _centery)
|
||||
x_source += int(m[0][2]);
|
||||
y_source += int(m[1][2]);
|
||||
|
||||
if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
|
||||
if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
|
||||
{
|
||||
p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
|
||||
for (int channels = 0; channels < this->channels; ++channels)
|
||||
p_target[channels] = p_source[channels];
|
||||
p_source = rgb_image + (channels * (y_source * width + x_source));
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_target[_channels] = p_source[_channels];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int channels = 0; channels < this->channels; ++channels)
|
||||
p_target[channels] = 255;
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_target[_channels] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
// memcpy(this->rgb_image, odata, memsize);
|
||||
this->memCopy(odata, this->rgb_image, memsize);
|
||||
stbi_image_free(odata);
|
||||
// memcpy(rgb_image, odata, memsize);
|
||||
memCopy(odata, rgb_image, memsize);
|
||||
|
||||
if (!ImageTMP)
|
||||
{
|
||||
stbi_image_free(odata);
|
||||
}
|
||||
}
|
||||
|
||||
void CRotate::Rotate(float _angle)
|
||||
{
|
||||
this->Rotate(_angle, this->width / 2, this->height / 2);
|
||||
// printf("width %d, height %d\n", width, height);
|
||||
Rotate(_angle, width / 2, height / 2);
|
||||
}
|
||||
|
||||
void CRotate::Translate(int _dx, int _dy)
|
||||
{
|
||||
int memsize = this->width * this->height * this->channels;
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
int memsize = width * height * channels;
|
||||
uint8_t* odata;
|
||||
if (ImageTMP)
|
||||
{
|
||||
odata = ImageTMP->rgb_image;
|
||||
}
|
||||
else
|
||||
{
|
||||
odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int x_source, y_source;
|
||||
stbi_uc* p_target;
|
||||
stbi_uc* p_source;
|
||||
|
||||
for (int x = 0; x < this->width; ++x)
|
||||
for (int y = 0; y < this->height; ++y)
|
||||
for (int x = 0; x < width; ++x)
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
p_target = odata + (this->channels * (y * this->width + x));
|
||||
p_target = odata + (channels * (y * width + x));
|
||||
|
||||
x_source = x - _dx;
|
||||
y_source = y - _dy;
|
||||
|
||||
if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
|
||||
if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
|
||||
{
|
||||
p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
|
||||
for (int channels = 0; channels < this->channels; ++channels)
|
||||
p_target[channels] = p_source[channels];
|
||||
p_source = rgb_image + (channels * (y_source * width + x_source));
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_target[_channels] = p_source[_channels];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int channels = 0; channels < this->channels; ++channels)
|
||||
p_target[channels] = 255;
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_target[_channels] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
// memcpy(this->rgb_image, odata, memsize);
|
||||
this->memCopy(odata, this->rgb_image, memsize);
|
||||
stbi_image_free(odata);
|
||||
// memcpy(rgb_image, odata, memsize);
|
||||
memCopy(odata, rgb_image, memsize);
|
||||
if (!ImageTMP)
|
||||
{
|
||||
stbi_image_free(odata);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
CFindTemplate::CFindTemplate(std::string _image)
|
||||
{
|
||||
this->channels = 1;
|
||||
this->rgb_image = stbi_load(_image.c_str(), &(this->width), &(this->height), &(this->bpp), this->channels);
|
||||
channels = 1;
|
||||
rgb_image = stbi_load(_image.c_str(), &(width), &(height), &(bpp), channels);
|
||||
}
|
||||
*/
|
||||
|
||||
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y)
|
||||
{
|
||||
this->FindTemplate(_template, found_x, found_y, 0, 0);
|
||||
FindTemplate(_template, found_x, found_y, 0, 0);
|
||||
}
|
||||
|
||||
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy)
|
||||
{
|
||||
uint8_t* rgb_template = stbi_load(_template.c_str(), &tpl_width, &tpl_height, &tpl_bpp, this->channels);
|
||||
uint8_t* rgb_template = stbi_load(_template.c_str(), &tpl_width, &tpl_height, &tpl_bpp, channels);
|
||||
|
||||
int ow, ow_start, ow_stop;
|
||||
int oh, oh_start, oh_stop;
|
||||
|
||||
if (_dx == 0)
|
||||
{
|
||||
_dx = this->width;
|
||||
_dx = width;
|
||||
*found_x = 0;
|
||||
}
|
||||
|
||||
if (_dy == 0)
|
||||
{
|
||||
_dy = this->height;
|
||||
_dy = height;
|
||||
*found_y = 0;
|
||||
}
|
||||
|
||||
@@ -199,18 +280,18 @@ void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found
|
||||
ow_start = *found_x - _dx;
|
||||
ow_start = std::max(ow_start, 0);
|
||||
ow_stop = *found_x + _dx;
|
||||
if ((ow_stop + tpl_width) > this->width)
|
||||
ow_stop = this->width - tpl_width;
|
||||
if ((ow_stop + tpl_width) > width)
|
||||
ow_stop = width - tpl_width;
|
||||
ow = ow_stop - ow_start + 1;
|
||||
|
||||
oh_start = *found_y - _dy;
|
||||
oh_start = std::max(oh_start, 0);
|
||||
oh_stop = *found_y + _dy;
|
||||
if ((oh_stop + tpl_height) > this->height)
|
||||
oh_stop = this->height - tpl_height;
|
||||
if ((oh_stop + tpl_height) > height)
|
||||
oh_stop = height - tpl_height;
|
||||
oh = oh_stop - oh_start + 1;
|
||||
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(ow * oh * this->channels);
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(ow * oh * channels);
|
||||
|
||||
double aktSAD;
|
||||
double minSAD = pow(tpl_width * tpl_height * 255, 2);
|
||||
@@ -222,11 +303,11 @@ void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found
|
||||
for (int tpl_x = 0; tpl_x < tpl_width; tpl_x++)
|
||||
for (int tpl_y = 0; tpl_y < tpl_height; tpl_y++)
|
||||
{
|
||||
stbi_uc* p_org = this->rgb_image + (this->channels * ((youter + tpl_y) * this->width + (xouter + tpl_x)));
|
||||
stbi_uc* p_tpl = rgb_template + (this->channels * (tpl_y * tpl_width + tpl_x));
|
||||
stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + (xouter + tpl_x)));
|
||||
stbi_uc* p_tpl = rgb_template + (channels * (tpl_y * tpl_width + tpl_x));
|
||||
aktSAD += pow(p_tpl[0] - p_org[0], 2);
|
||||
}
|
||||
stbi_uc* p_out = odata + (this->channels * ((youter - oh_start) * ow + (xouter - ow_start)));
|
||||
stbi_uc* p_out = odata + (channels * ((youter - oh_start) * ow + (xouter - ow_start)));
|
||||
|
||||
p_out[0] = int(sqrt(aktSAD / (tpl_width * tpl_height)));
|
||||
if (aktSAD < minSAD)
|
||||
@@ -237,7 +318,7 @@ void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found
|
||||
}
|
||||
}
|
||||
|
||||
stbi_write_bmp("sdcard\\find.bmp", ow, oh, this->channels, odata);
|
||||
stbi_write_bmp("sdcard\\find.bmp", ow, oh, channels, odata);
|
||||
|
||||
stbi_image_free(odata);
|
||||
stbi_image_free(rgb_template);
|
||||
@@ -245,14 +326,14 @@ void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found
|
||||
|
||||
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, std::string _imageout)
|
||||
{
|
||||
this->FindTemplate(_template, found_x, found_y);
|
||||
this->SaveToFile(_imageout);
|
||||
FindTemplate(_template, found_x, found_y);
|
||||
SaveToFile(_imageout);
|
||||
}
|
||||
|
||||
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy, std::string _imageout)
|
||||
{
|
||||
this->FindTemplate(_template, found_x, found_y, _dx, _dy);
|
||||
this->SaveToFile(_imageout);
|
||||
FindTemplate(_template, found_x, found_y, _dx, _dy);
|
||||
SaveToFile(_imageout);
|
||||
}
|
||||
|
||||
|
||||
@@ -269,10 +350,10 @@ void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
|
||||
|
||||
bool CImageBasis::isInImage(int x, int y)
|
||||
{
|
||||
if ((x < 0) || (x > this->width - 1))
|
||||
if ((x < 0) || (x > width - 1))
|
||||
return false;
|
||||
|
||||
if ((y < 0) || (y > this->height- 1))
|
||||
if ((y < 0) || (y > height- 1))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -282,9 +363,9 @@ void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
|
||||
{
|
||||
stbi_uc* p_source;
|
||||
|
||||
p_source = this->rgb_image + (this->channels * (y * this->width + x));
|
||||
p_source = rgb_image + (channels * (y * width + x));
|
||||
p_source[0] = r;
|
||||
if (this-> channels > 2)
|
||||
if ( channels > 2)
|
||||
{
|
||||
p_source[1] = g;
|
||||
p_source[2] = b;
|
||||
@@ -383,7 +464,61 @@ void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int t
|
||||
|
||||
CImageBasis::CImageBasis()
|
||||
{
|
||||
this->externalImage = false;
|
||||
externalImage = false;
|
||||
}
|
||||
|
||||
void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
|
||||
{
|
||||
bpp = _channels;
|
||||
width = _width;
|
||||
height = _height;
|
||||
channels = _channels;
|
||||
|
||||
int memsize = width * height * channels;
|
||||
rgb_image = (unsigned char*)GET_MEMORY(memsize);
|
||||
|
||||
|
||||
stbi_uc* p_source;
|
||||
|
||||
for (int x = 0; x < width; ++x)
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
p_source = rgb_image + (channels * (y * width + x));
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_source[_channels] = (uint8_t) 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
|
||||
{
|
||||
// if (rgb_image)
|
||||
// free(rgb_image);
|
||||
rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, 3);
|
||||
bpp = channels;
|
||||
// STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
|
||||
|
||||
}
|
||||
|
||||
CImageBasis::CImageBasis(CImageBasis *_copyfrom)
|
||||
{
|
||||
externalImage = false;
|
||||
channels = _copyfrom->channels;
|
||||
width = _copyfrom->width;
|
||||
height = _copyfrom->height;
|
||||
bpp = _copyfrom->bpp;
|
||||
|
||||
int memsize = width * height * channels;
|
||||
rgb_image = (unsigned char*)GET_MEMORY(memsize);
|
||||
if (!rgb_image)
|
||||
{
|
||||
printf(getESPHeapInfo().c_str());
|
||||
printf("\nKein freier Speicher mehr!!!! Benötigt: %d %d %d %d\n", width, height, channels, memsize);
|
||||
return;
|
||||
}
|
||||
|
||||
memCopy(_copyfrom->rgb_image, rgb_image, memsize);
|
||||
}
|
||||
|
||||
CImageBasis::CImageBasis(std::string _image)
|
||||
@@ -391,13 +526,19 @@ CImageBasis::CImageBasis(std::string _image)
|
||||
channels = 3;
|
||||
externalImage = false;
|
||||
filename = _image;
|
||||
// long freebefore = esp_get_free_heap_size();
|
||||
long zwld = esp_get_free_heap_size();
|
||||
printf("freeheapsize before: %ld\n", zwld);
|
||||
|
||||
rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
|
||||
// if (rgb_image == NULL)
|
||||
// LogFile.WriteToFile("Image Load failed:" + _image + " FreeHeapSize before: " + to_string(freebefore) + " after: " + to_string(esp_get_free_heap_size()));
|
||||
// printf("CImageBasis after load\n");
|
||||
// printf("w %d, h %d, b %d, c %d", this->width, this->height, this->bpp, this->channels);
|
||||
zwld = esp_get_free_heap_size();
|
||||
printf("freeheapsize after : %ld\n", zwld);
|
||||
|
||||
std::string zw = "Image Load failed:" + _image + "\n";
|
||||
if (rgb_image == NULL)
|
||||
printf(zw.c_str());
|
||||
zw = "CImageBasis after load " + _image + "\n";
|
||||
printf(zw.c_str());
|
||||
printf("w %d, h %d, b %d, c %d\n", width, height, bpp, channels);
|
||||
}
|
||||
|
||||
bool CImageBasis::ImageOkay(){
|
||||
@@ -406,12 +547,12 @@ bool CImageBasis::ImageOkay(){
|
||||
|
||||
CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
|
||||
{
|
||||
this->rgb_image = _rgb_image;
|
||||
this->channels = _channels;
|
||||
this->width = _width;
|
||||
this->height = _height;
|
||||
this->bpp = _bpp;
|
||||
this->externalImage = true;
|
||||
rgb_image = _rgb_image;
|
||||
channels = _channels;
|
||||
width = _width;
|
||||
height = _height;
|
||||
bpp = _bpp;
|
||||
externalImage = true;
|
||||
}
|
||||
|
||||
void CImageBasis::Contrast(float _contrast) //input range [-100..100]
|
||||
@@ -424,16 +565,16 @@ void CImageBasis::Contrast(float _contrast) //input range [-100..100]
|
||||
for (int x = 0; x < width; ++x)
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
p_source = this->rgb_image + (this->channels * (y * this->width + x));
|
||||
for (int channels = 0; channels < this->channels; ++channels)
|
||||
p_source[channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[channels] * contrast + intercept)));
|
||||
p_source = rgb_image + (channels * (y * width + x));
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
|
||||
}
|
||||
}
|
||||
|
||||
CImageBasis::~CImageBasis()
|
||||
{
|
||||
if (!this->externalImage)
|
||||
stbi_image_free(this->rgb_image);
|
||||
if (!externalImage)
|
||||
stbi_image_free(rgb_image);
|
||||
}
|
||||
|
||||
void CImageBasis::SaveToFile(std::string _imageout)
|
||||
@@ -442,25 +583,44 @@ void CImageBasis::SaveToFile(std::string _imageout)
|
||||
|
||||
if ((typ == "jpg") || (typ == "JPG")) // ACHTUNG PROBLEMATISCH IM ESP32
|
||||
{
|
||||
stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
|
||||
stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
|
||||
}
|
||||
|
||||
if ((typ == "bmp") || (typ == "BMP"))
|
||||
{
|
||||
stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
|
||||
stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
|
||||
}
|
||||
// stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
|
||||
// stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
|
||||
}
|
||||
|
||||
|
||||
|
||||
CAlignAndCutImage::CAlignAndCutImage(CImageBasis *_org, CImageBasis *_temp)
|
||||
{
|
||||
rgb_image = _org->rgb_image;
|
||||
channels = _org->channels;
|
||||
width = _org->width;
|
||||
height = _org->height;
|
||||
bpp = _org->bpp;
|
||||
externalImage = true;
|
||||
|
||||
ImageTMP = _temp;
|
||||
}
|
||||
|
||||
void CAlignAndCutImage::GetRefSize(int *ref_dx, int *ref_dy)
|
||||
{
|
||||
ref_dx[0] = t0_dx;
|
||||
ref_dy[0] = t0_dy;
|
||||
ref_dx[1] = t1_dx;
|
||||
ref_dy[1] = t1_dy;
|
||||
}
|
||||
|
||||
void CAlignAndCutImage::Align(std::string _template0, int ref0_x, int ref0_y, std::string _template1, int ref1_x, int ref1_y, int deltax, int deltay, std::string imageROI)
|
||||
{
|
||||
int dx, dy;
|
||||
int r0_x, r0_y, r1_x, r1_y;
|
||||
|
||||
CFindTemplate* ft = new CFindTemplate(this->filename);
|
||||
// CFindTemplate* ft = new CFindTemplate(filename);
|
||||
CFindTemplate* ft = new CFindTemplate(rgb_image, channels, width, height, bpp);
|
||||
|
||||
r0_x = ref0_x;
|
||||
r0_y = ref0_y;
|
||||
@@ -495,7 +655,7 @@ void CAlignAndCutImage::Align(std::string _template0, int ref0_x, int ref0_y, st
|
||||
|
||||
if (imageROI.length() > 0)
|
||||
{
|
||||
CImageBasis* imgzw = new CImageBasis(this->filename);
|
||||
CImageBasis* imgzw = new CImageBasis(this);
|
||||
imgzw->drawRect(r0_x, r0_y, t0_dx, t0_dy, 255, 0, 0, 2);
|
||||
imgzw->drawRect(r1_x, r1_y, t1_dx, t1_dy, 255, 0, 0, 2);
|
||||
imgzw->SaveToFile(imageROI);
|
||||
@@ -504,14 +664,16 @@ void CAlignAndCutImage::Align(std::string _template0, int ref0_x, int ref0_y, st
|
||||
}
|
||||
|
||||
string zw = "\tdx:\t" + to_string(dx) + "\tdy:\t" + to_string(dy) + "\td_winkel:\t" + to_string(d_winkel);
|
||||
LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
|
||||
// LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
|
||||
|
||||
CRotate rt(this->rgb_image, this->channels, this->width, this->height, this->bpp);
|
||||
CRotate rt(this, ImageTMP);
|
||||
rt.Translate(dx, dy);
|
||||
rt.Rotate(d_winkel, ref0_x, ref0_y);
|
||||
printf("Alignment: dx %d - dy %d - rot %f\n", dx, dy, d_winkel);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
|
||||
{
|
||||
|
||||
@@ -519,13 +681,13 @@ void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int d
|
||||
|
||||
x2 = x1 + dx;
|
||||
y2 = y1 + dy;
|
||||
x2 = min(x2, this->width - 1);
|
||||
y2 = min(y2, this->height - 1);
|
||||
x2 = min(x2, width - 1);
|
||||
y2 = min(y2, height - 1);
|
||||
|
||||
dx = x2 - x1;
|
||||
dy = y2 - y1;
|
||||
|
||||
int memsize = dx * dy * this->channels;
|
||||
int memsize = dx * dy * channels;
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
|
||||
stbi_uc* p_target;
|
||||
@@ -534,14 +696,46 @@ void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int d
|
||||
for (int x = x1; x < x2; ++x)
|
||||
for (int y = y1; y < y2; ++y)
|
||||
{
|
||||
p_target = odata + (this->channels * ((y - y1) * dx + (x - x1)));
|
||||
p_source = this->rgb_image + (this->channels * (y * this->width + x));
|
||||
for (int channels = 0; channels < this->channels; ++channels)
|
||||
p_target[channels] = p_source[channels];
|
||||
p_target = odata + (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];
|
||||
}
|
||||
|
||||
// stbi_write_jpg(_template1.c_str(), dx, dy, this->channels, odata, 0);
|
||||
stbi_write_bmp(_template1.c_str(), dx, dy, this->channels, odata);
|
||||
// stbi_write_jpg(_template1.c_str(), dx, dy, channels, odata, 0);
|
||||
stbi_write_bmp(_template1.c_str(), dx, dy, channels, odata);
|
||||
|
||||
stbi_image_free(odata);
|
||||
}
|
||||
|
||||
CResizeImage* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
|
||||
{
|
||||
int x2, y2;
|
||||
|
||||
x2 = x1 + dx;
|
||||
y2 = y1 + dy;
|
||||
x2 = min(x2, width - 1);
|
||||
y2 = min(y2, height - 1);
|
||||
|
||||
dx = x2 - x1;
|
||||
dy = y2 - y1;
|
||||
|
||||
int memsize = dx * dy * channels;
|
||||
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
|
||||
|
||||
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_source = rgb_image + (channels * (y * width + x));
|
||||
for (int _channels = 0; _channels < channels; ++_channels)
|
||||
p_target[_channels] = p_source[_channels];
|
||||
}
|
||||
|
||||
CResizeImage* rs = new CResizeImage(odata, channels, dx, dy, bpp);
|
||||
rs->SetIndepended();
|
||||
return rs;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user