You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1538 lines
38 KiB

#error "This source file seems to be unused. 2026-02-20"
//----------------------------------------------------------------------------
// Dr.Frucht Systems Ltd.
// Copyright © 2000-2017. All Rights Reserved.
// Version 2.0 9/14/2017
// Last modification: 9/14/2017
// PROJECT:
// SUBSYSTEM:
// FILE: basicImage.cpp
// AUTHOR: gagua
// OVERVIEW
// ~~~~~~~~
// Header file of basicImage.cpp
//----------------------------------------------------------------------------
//
#include "basicImage.h"
//#include "utils.h"
//#include "blas.h"
//#include "dn_cuda.h"
#define STB_IMAGE_IMPLEMENTATION
#include "../../media/pictures/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../../media/pictures/stb_image_write.h"
const uint16_t cortexCOLORS[6][3] = {
{ 1, 0, 1 },
{ 0, 0, 1 },
{ 0, 1, 1 },
{ 0, 1, 0 },
{ 1, 1, 0 },
{ 1, 0, 0 }
};
using namespace cv;
cortexImage::cortexImage(){
h = 0;
w = 0;
c = 0;
step = 0;
data = NULL;
};
cortexImage::cortexImage(IplImage* src){
if( src !=NULL ) {
uchar *ipldata = (uchar *)src->imageData;
h = src->height;
w = src->width;
c = src->nChannels;
step = src->widthStep;
data = (float*)calloc(h*w*c, sizeof(float));
size = h*w*c;
if( data != NULL ) {
int i, j, k;
for(i = 0; i < h; ++i){
for(k= 0; k < c; ++k){
for(j = 0; j < w; ++j){
data[k*w*h + i*w + j] = ((float)ipldata[i*step + j*c + k]) / 255.f;
};
};
};
} else {
h = 0;
w = 0;
c = 0;
size = 0;
step = 0;
};
} else {
h = 0;
w = 0;
c = 0;
size = 0;
step = 0;
};
};
cortexImage::cortexImage(cv::Mat* src){
if( src!=NULL ) {
h= src->rows;
w= src->cols;
c= src->channels();
step = w*c;
size = h*w*c;
data = (float*)calloc(h*w*c, sizeof(float));
if( data != NULL ) {
int i, j, k;
for(i = 0; i < h; ++i){
for(k= 0; k < c; ++k){
for(j = 0; j < w; ++j){
data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k])/255.f;
};
};
};
} else {
h=0;
w=0;
c=0;
size = 0;
step = 0;
};
} else {
h=0;
w=0;
c=0;
size = 0;
step = 0;
};
};
bool cortexImage::fillFromMat(cv::Mat* src){
if (src != NULL && src!= NULL) {
if ( h == src->rows &&
w == src->cols &&
c == src->channels()){
int i, j, k;
for (i = 0; i < h; ++i){
for (k = 0; k < c; ++k){
for (j = 0; j < w; ++j){
data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k]) / 255.f;
};
};
};
};
};
return false;
};
cortexImage::cortexImage( const cortexImage &CC ){
h=CC.h;
w=CC.w;
c=CC.c;
step=CC.step;
size = CC.size;
data = (float*)calloc(h*w*c, sizeof(float));
if( data != NULL ) {
memcpy( data, CC.data, h*w*c*sizeof(float));
} else {
h=0;
w=0;
c=0;
size = 0;
step = 0;
};
};
cortexImage::cortexImage( const cortexImage *CC ){
h = CC->h;
w = CC->w;
c = CC->c;
step = CC->step;
size = CC->size;
data = (float*)calloc(h*w*c, sizeof(float));
if (data != NULL) {
memcpy(data, CC->data, h*w*c*sizeof(float));
} else {
h = 0;
w = 0;
c = 0;
step = 0;
size = 0;
};
};
/*
cortexImage::cortexImage( cortexImage &CC ){
h = CC.h;
w = CC.w;
c = CC.c;
step = CC.step;
data = (float*)calloc(h*w*c, sizeof(float));
if (data != NULL) {
memcpy(data, CC.data, h*w*c*sizeof(float));
} else {
h = 0;
w = 0;
c = 0;
step = 0;
};
};
cortexImage::cortexImage( cortexImage *CC ){
h = CC->h;
w = CC->w;
c = CC->c;
step = CC->step;
data = (float*)calloc(h*w*c, sizeof(float));
if (data != NULL) {
memcpy(data, CC->data, h*w*c*sizeof(float));
} else {
h = 0;
w = 0;
c = 0;
step = 0;
};
};
*/
cortexImage::~cortexImage(){
freeData();
};
float cortexImage::getColor(uint32_t c, uint32_t x, uint32_t max){
float ratio = ((float)x/max)*5;
int i = (int)floor(ratio);
int j = (int)ceil(ratio);
ratio -= i;
float r = ((1.f - ratio)* cortexCOLORS[i][c])+ ( ratio * cortexCOLORS[j][c]);
return r;
};
void cortexImage::toMat(cv::Mat &dst){
int mW, mH;
mW = (w >= dst.cols) ? dst.cols : w;
mH = (h >= dst.rows) ? dst.rows : h;
int index = 0;
for (int y = 0; y < mW; y++){
for (int x = 0; x < mW*dst.channels(); x++){
index = x + y*w*c;
if(dst.channels()==1){
uchar& BGR = dst.at<uchar>(y, x);
BGR = (uchar)(((uint32_t)(255.f*data[index])) & 0xFF);
} else if (dst.channels() == 3){
Vec3b& BGR = dst.at<Vec3b>(y, x);
BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF);
BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF);
BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF);
} else if (dst.channels() == 4){
Vec4b& BGR = dst.at<Vec4b>(y, x);
BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF);
BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF);
BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF);
BGR[3] = 0xFF;
} else return;
};
};
};
void cortexImage::toMat(cv::Mat *dst){
if (dst != NULL)toMat(*dst);
};
cv::Mat *cortexImage::toMat(void){
cv::Mat *RetVal;
RetVal = new cv::Mat(h, w, CV_8UC3);
if (RetVal != NULL)toMat(*RetVal);
return RetVal;
};
/*
cv::Mat *mask_to_rgb( cortexImage *mask ){
int n = mask->c;
cv::Mat *im;
im = new cv::Mat( mask->h, mask->w, CV_32FC3 );
int i, j;
for (j = 0; j < n; ++j){
int offset = j * 123457 % n;
float red = getColor(2, offset, n);
float green = getColor(1, offset, n);
float blue = getColor(0, offset, n);
for (i = 0; i < im.cols*im.rows; ++i){
im.data[i + 0 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * red;
im.data[i + 1 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * green;
im.data[i + 2 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * blue;
}
}
return im;
};
*/
bool cortexImage::composite(cortexImage *source, uint32_t dx, uint32_t dy){
uint32_t x,y,k;
for(k = 0; k < source->c; ++k){
for(y = 0; y < source->h; ++y){
for(x = 0; x < source->w; ++x){
float val = source->getPixel( x, y, k );
float val2 = getPixelEX( dx+x, dy+y, k );
setPixel( dx+x, dy+y, k, val*val2 );
};
};
};
return true;
};
void cortexImage::border(uint32_t border){
//image b = make_image(a.w + 2*border, a.h + 2*border, a.c);
uint32_t x, y, k;
for(k = 0; k < c; ++k){
for(y = 0; y < h; ++y){
for(x = 0; x < w; ++x){
float val = getPixelEX(x - border, y - border, k);
if(x - border < 0 || x - border >= w || y - border < 0 || y - border >= h) val = 1;
setPixel( x, y, k, val );
};
};
};
};
void cortexImage::drawLabel(uint32_t r, uint32_t c, cortexImage *label, const float *rgb){
uint32_t tW = label->w;
uint32_t tH = label->h;
if (r - tH >= 0) r = r - tH;
uint32_t i, j, k;
for(j = 0; j < tH && ( j + r )< h; ++j){
for(i = 0; i < tW && (i + c )< w; ++i){
for(k = 0; k < label->c; ++k){
float val = label->getPixel( i, j, k);
setPixel( i+c, j+r, k, rgb[k]*val );
};
};
};
};
void cortexImage::drawBoxEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, float r, float g, float b){
//normalize_image(a);
uint32_t i, a;
if(x1 < 0) x1 = 0;
if(x1 >= w) x1 = w-1;
if(x2 < 0) x2 = 0;
if(x2 >= w) x2 = w-1;
if(y1 < 0) y1 = 0;
if(y1 >= h) y1 = h-1;
if(y2 < 0) y2 = 0;
if(y2 >= h) y2 = h-1;
a = w*h;
for(i = x1; i <= x2; ++i){
data[i + y1*w] = r;
data[i + y2*w] = r;
data[i + y1*w + a] = g;
data[i + y2*w + a] = g;
data[i + y1*w + (a<<1)] = b;
data[i + y2*w + (a<<1)] = b;
};
for(i = y1; i <= y2; ++i){
data[x1 + i*w] = r;
data[x2 + i*w] = r;
data[x1 + i*w + a] = g;
data[x2 + i*w + a] = g;
data[x1 + i*w + (a<<1)] = b;
data[x2 + i*w + (a<<1)] = b;
};
};
void cortexImage::drawBoxWEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t w, float r, float g, float b){
uint32_t i;
for(i = 0; i < w; ++i){
drawBoxEX( x1+i, y1+i, x2-i, y2-i, r, g, b);
};
};
void cortexImage::drawBox(cortexBOX *bbox, uint32_t w, float r, float g, float b){
uint32_t left = (int)((bbox->x - bbox->w) / 2)*w;
uint32_t right = (int)((bbox->x + bbox->w) / 2)*w;
uint32_t top = (int)((bbox->y - bbox->h) / 2)*h;
uint32_t bot = (int)((bbox->y + bbox->h) / 2)*h;
drawBoxWEX( left, top, right, bot, 2, r, g, b);
};
/*
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes){
int i;
for(i = 0; i < num; ++i){
int cls = max_index(probs[i], classes);
float prob = probs[i][cls];
if(prob > thresh){
int width = (int)(im.h * .006);
if(0){
width = (int)( pow(prob, (float)(1./2.))*10+1);
alphabet = 0;
}
//printf("%d %s: %.0f%%\n", i, names[class], prob*100);
printf("%s: %.0f%%\n", names[cls], prob*100);
int offset = cls*123457 % classes;
float red = get_color(2,offset,classes);
float green = get_color(1,offset,classes);
float blue = get_color(0,offset,classes);
float rgb[3];
//width = prob*20+2;
rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
box b = boxes[i];
int left = (int)(b.x - b.w / 2.)*im.w;
int right = (int)(b.x + b.w / 2.)*im.w;
int top = (int)(b.y - b.h / 2.)*im.h;
int bot = (int)(b.y + b.h / 2.)*im.h;
if(left < 0) left = 0;
if(right > im.w-1) right = im.w-1;
if(top < 0) top = 0;
if(bot > im.h-1) bot = im.h-1;
draw_box_width(im, left, top, right, bot, width, red, green, blue);
if (alphabet) {
image label = get_label(alphabet, names[cls], (int)((im.h*.03) / 10));
draw_label(im, top + width, left, label, rgb);
free_image(label);
}
if (masks){
image mask = float_to_image(14, 14, 1, masks[i]);
image resized_mask = resize_image(mask, (int)(b.w*im.w), (int)(b.h*im.h));
image tmask = threshold_image(resized_mask, .5);
embed_image(tmask, im, left, top);
free_image(mask);
free_image(resized_mask);
free_image(tmask);
}
}
}
}
void drawMAT_detections(cv::Mat &im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, int classes){
char Buffer[220];
sprintf(Buffer, "Detected %d objects", num );
rectangle( im, Point(im.cols - 120, 5), Point(im.cols - 10, 15), Scalar(0, 0, 255), CV_FILLED);
putText( im, Buffer, Point(im.cols - 115, 13), FONT_HERSHEY_SIMPLEX,.3, Scalar(255, 255, 0), 1);
for (int i = 0; i < num; ++i){
int cls = max_index(probs[i], classes);
float prob = probs[i][cls];
if (prob > thresh){
//int width = (int)(im.rows * .006);
//printf("%d %s: %.0f%%\n", i, names[class], prob*100);
sprintf(Buffer, "%s: %.0f%%", names[cls], prob * 100);
int offset = cls * 123457 % classes;
float red = get_color(2, offset, classes);
float green = get_color(1, offset, classes);
float blue = get_color(0, offset, classes);
//float rgb[3];
//width = prob*20+2;
//rgb[0] = red;
//rgb[1] = green;
//rgb[2] = blue;
box b = boxes[i];
int left = (int)((b.x - (b.w / 2.f))*im.cols);
int right = (int)((b.x + (b.w / 2.f))*im.cols);
int top = (int)((b.y - (b.h / 2.f))*im.rows);
int bot = (int)((b.y + (b.h / 2.f))*im.rows);
if (left < 0) left = 0;
if (right > im.cols - 1) right = im.cols - 1;
if (top < 0) top = 0;
if (bot > im.rows - 1) bot = im.rows - 1;
putText(im, Buffer, Point(b.x*im.cols, b.y*im.rows), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 0), 2);
rectangle(im, Point(left, top), Point(right, bot), Scalar(0, 255, 0), 2);
//draw_box_width(im, left, top, right, bot, width, red, green, blue);
};
};
}
*/
void cortexImage::transpose(void){
assert(w == h);
uint16_t i, j, k;
for(i = 0; i < c; ++i){
for(j = 0; j < w-1; ++j){
for(k = j + 1; k < w; ++k){
float swap = data[k + w*(j + h*i)];
data[k + w*(j + h*i)] = data[j + w*(k + h*i)];
data[j + w*(k + h*i)] = swap;
};
};
};
};
void cortexImage::rotateCW(uint32_t times){
assert(w == h);
times = (times + 400) % 4;
uint32_t i, x, y, l;
uint32_t n = w;
for(i = 0; i < times; ++i){
for(l = 0; l < c; ++l){
for(x = 0; x < n/2; ++x){
for(y = 0; y < (n-1)/2 + 1; ++y){
float temp = data[y + w*(x + h*l)];
data[y + w*(x + h*l)] = data[n-1-x + w*(y + h*l)];
data[n-1-x + w*(y + h*l)] = data[n-1-y + w*(n-1-x + h*l)];
data[n-1-y + w*(n-1-x + h*l)] = data[x + w*(n-1-y + h*l)];
data[x + w*(n-1-y + h*l)] = temp;
};
};
};
};
};
void cortexImage::flip(void){
uint16_t i, j, k;
for(k = 0; k < c; ++k){
for(i = 0; i < h; ++i){
for(j = 0; j < w/2; ++j){
int index = j + w*(i + h*(k));
int flip = (w - j - 1) + w*(i + h*(k));
float swap = data[flip];
data[flip] = data[index];
data[index] = swap;
};
};
};
};
void cortexImage::distance(cortexImage *b){
uint16_t i, j;
for(i = 0; i < c; ++i){
for(j = 0; j < h*w; ++j){
data[j] += pow( data[i*h*w+j]-data[i*h*w+j], 2 );
};
};
for(j = 0; j < h*w; ++j){
data[j] = sqrt(data[j]);
};
};
void cortexImage::ghost( cortexImage *source, uint32_t dx, uint32_t dy){
uint16_t x, y, k;
float max_dist = (float)sqrt((-source->w/2.f + .5)*(-source->w/2.f + .5));
for(k = 0; k < source->c; ++k){
for(y = 0; y < source->h; ++y){
for(x = 0; x < source->w; ++x){
float dist = (float)sqrt((x - source->w/2.f + .5f)*(x - source->w/2.f + .5f) + (y - source->h/2.f + .5f)*(y - source->h/2.f + .5f));
float alpha = (1 - dist/max_dist);
if(alpha < 0) alpha = 0;
float v1 = source->getPixel(x,y,k);
float v2 = getPixel(dx+x,dy+y,k);
float val = alpha*v1 + (1-alpha)*v2;
setPixel(dx+x, dy+y, k, val);
};
};
};
};
void cortexImage::embed(cortexImage *source, uint32_t dx, uint32_t dy){
uint32_t x, y, k;
for(k = 0; k < source->c; ++k){
for(y = 0; y < source->h; ++y){
for(x = 0; x < source->w; ++x){
float val = source->getPixel(x,y,k);
setPixel(dx+x, dy+y, k, val);
};
};
};
};
/*
cortexImage *collapse_image_layers(cortexImage *source, uint32_t border){
uint32_t h = source.h;
h = (h+border)*source.c - border;
image dest = make_image(source.w, h, 1);
uint32_t i;
for(i = 0; i < source.c; ++i){
image layer = get_image_layer(source, i);
uint32_t h_offset = i*(source.h + border);
embed_image(layer, dest, 0, h_offset);
free_image(layer);
}
return dest;
};
*/
cortexImage *cortexImage::getLayer(uint32_t l){
cortexImage *out;
out = new cortexImage();
out->makeWithData(w, h, 1);// make_image(m.w, m.h, 1);
int i;
for (i = 0; i < h*w; ++i){
out->data[i] = data[i + l*h*w];
};
return out;
};
void cortexImage::constrain(void){
uint16_t i;
for(i = 0; i < w*h*c; ++i){
if(data[i] < 0) data[i] = 0;
if(data[i] > 1) data[i] = 1;
};
};
void cortexImage::normalize(void){
uint16_t i;
float min = 9999999.f;
float max = -999999.f;
for(i = 0; i < h*w*c; ++i){
float v = data[i];
if(v < min) min = v;
if(v > max) max = v;
}
if(max - min < .000000001f){
min = 0.f;
max = 1.f;
}
for(i = 0; i < c*w*h; ++i){
data[i] = (data[i] - min)/(max-min);
};
};
void cortexImage::normalize2( void ){
float *min = (float*)calloc(c, sizeof(float));
float *max = (float*)calloc(c, sizeof(float));
uint16_t i, j;
for(i = 0; i < c; ++i) min[i] = max[i] = data[i*h*w];
for(j = 0; j < c; ++j){
for(i = 0; i < h*w; ++i){
float v = data[i+j*h*w];
if(v < min[j]) min[j] = v;
if(v > max[j]) max[j] = v;
};
};
for(i = 0; i < c; ++i){
if(max[i] - min[i] < .000000001f){
min[i] = 0;
max[i] = 1;
};
};
for(j = 0; j < c; ++j){
for(i = 0; i < w*h; ++i){
data[i+j*h*w] = (data[i+j*h*w] - min[j])/(max[j]-min[j]);
};
};
free(min);
free(max);
};
void cortexImage::copyInto(cortexImage *dest){
if (dest->data != NULL) free(dest->data);
dest->data = (float*)calloc(h*w*c, sizeof(float));
dest->size = h*w*c;
dest->step = w*c;
if (dest->data != NULL){
dest->h = h;
dest->w = w;
dest->c = c;
memcpy(dest->data, data, h*w*c*sizeof(float));
} else {
dest->h = 0;
dest->w = 0;
dest->c = 0;
dest->size = 0;
dest->step = 0;
};
};
void cortexImage::rgb_to_bgr(void){
uint16_t i;
for(i = 0; i < w*h; ++i){
float swap = data[i];
data[i] = data[i+w*h*2];
data[i+w*h*2] = swap;
};
};
bool cortexImage::toIpl(IplImage* dst){
unsigned char *idata = (unsigned char *)dst->imageData;
uint32_t hh = dst->height;
uint32_t ww = dst->width;
uint32_t cc = dst->nChannels;
uint32_t sstep = dst->widthStep;
uint32_t i, j, k;
for (i = 0; i < hh; ++i){
for (k = 0; k < cc; ++k){
for (j = 0; j < ww; ++j){
idata[i*sstep + j*cc + k] = ((int)( 255.f * data[k*w*h + i*w + j])) & 0xFF;
};
};
};
return true;
};
/*
void flush_stream_buffer(CvCapture *cap, int n)
{
int i;
for(i = 0; i < n; ++i) {
cvQueryFrame(cap);
}
}
image get_image_from_stream(CvCapture *cap)
{
IplImage* src = cvQueryFrame(cap);
if (!src) return make_empty_image(0,0,0);
image im = ipl_to_image(src);
rgbgr_image(im);
return im;
}
int fill_image_from_stream(CvCapture *cap, image im)
{
IplImage* src = cvQueryFrame(cap);
if (!src) return 0;
ipl_into_image(src, im);
rgbgr_image(im);
return 1
}
*/
/*
void cortexImage::saveJPG(const char *name){
char buff[256];
uint32_t x, y, k;
cortexImage *copy;
copy = new cortexImage( this );
if (c == 3) copy->rgb_to_bgr();
sprintf(buff, "%s.jpg", name);
IplImage *disp = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, c);
int step = disp->widthStep;
for(y = 0; y < h; ++y){
for(x = 0; x < w; ++x){
for(k= 0; k < c; ++k){
disp->imageData[y*step + x*c + k] = (unsigned char)(copy->getPixel(x, y, k) * 255);
};
};
};
cvSaveImage( buff, disp, 0 );
cvReleaseImage(&disp);
delete(copy);
};
*/
void cortexImage::saveJPG(const char *name, int Quality ){
uint16_t i, k;
char buff[256];
sprintf(buff, "%s.png", name);
unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char));
for (k = 0; k < c; ++k){
for (i = 0; i < w*h; ++i){
Zdata[i*c + k] = (unsigned char)(255 * data[i + k*w*h]);
};
};
int success = stbi_write_jpg(buff, w, h, c, Zdata, Quality );
free(Zdata);
if (!success) fprintf(stderr, "Failed to write image %s\n", buff);
};
void cortexImage::savePNG(const char *name){
uint16_t i, k;
char buff[256];
sprintf(buff, "%s.png", name);
unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char));
for(k = 0; k < c; ++k){
for(i = 0; i < w*h; ++i){
Zdata[i*c+k] = (unsigned char) (255*data[i + k*w*h]);
};
};
int success = stbi_write_png( buff, w, h, c, Zdata, w*c );
free(Zdata);
if(!success) fprintf(stderr, "Failed to write image %s\n", buff );
};
void cortexImage::makeEmpty(uint32_t ww, uint32_t hh, uint32_t cc){
if (data != NULL) free(data);
h = hh;
w = ww;
c = cc;
step = 0;
size = 0;
};
void cortexImage::freeData(void){
if (data != NULL) free(data);
};
void cortexImage::makeWithData(uint32_t ww, uint32_t hh, uint32_t cc){
makeEmpty( ww, hh, cc );
size = h*w*c;
data = (float*)calloc(size, sizeof(float));
};
void cortexImage::fillRandom(void){
freeData();
data = (float*)calloc(size, sizeof(float));
for (uint16_t i = 0; i < size; ++i){
data[i] = (float)((rand_normal() * .25) + .5);
};
};
void cortexImage::setData(uint32_t ww, uint32_t hh, uint32_t cc, float *ddata){
freeData();
w = ww;
h = hh;
c = cc;
data = ddata;
};
void cortexImage::place(uint32_t ww, uint32_t hh, uint32_t dx, uint32_t dy, cortexImage *canvas){
uint32_t x, y, cc;
for(cc = 0; cc < c; ++cc){
for(y = 0; y < hh; ++y){
for(x = 0; x < ww; ++x){
uint32_t rx = (uint32_t)((float)x / ww) * w;
uint32_t ry = (uint32_t)((float)y / hh) * h;
float val = bilinear_interpolate( (float)rx, (float)ry, cc );
canvas->setPixel( x + dx, y + dy, cc, val);
};
};
};
};
void cortexImage::centerCrop(uint32_t ww, uint32_t hh){
int m = (w < h) ? w : h;
crop((w - m) / 2, (h - m) / 2, m, m);
resize(ww, hh);
};
void cortexImage::rotateCrop( float rad, float s, uint32_t ww, uint32_t hh, float dx, float dy, float aspect){
uint32_t x, y, cc;
float cx = (float)(w/2.);
float cy = (float)(h/2.);
cortexImage *rot;
rot = new cortexImage();
rot->makeWithData(ww, hh, c);
for(cc = 0; cc < c; ++cc){
for(y = 0; y < hh; ++y){
for(x = 0; x < ww; ++x){
float rx = (float)(cos(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) - sin(rad)*((y - hh/2.)/s + dy/s) + cx);
float ry = (float)(sin(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) + cos(rad)*((y - hh/2.)/s + dy/s) + cy);
float val = bilinear_interpolate( rx, ry, cc);
rot->setPixel(x, y, cc, val);
};
};
};
setData(rot->w, rot->h, rot->c, rot->data);
delete(rot);
};
void cortexImage::rotate(float rad){
uint32_t x, y, cc;
float cx = (float)(w / 2.f);
float cy = (float)(h / 2.f);
cortexImage *rot;
rot = new cortexImage();
rot->makeWithData(w, h, c );
for (cc = 0; cc < c; ++c){
for (y = 0; y < h; ++y){
for (x = 0; x < w; ++x){
float rx = (float)(cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx);
float ry = (float)(sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy);
float val = bilinear_interpolate( rx, ry, cc);
rot->setPixel( x, y, cc, val);
};
};
};
setData(rot->w, rot->h, rot->c, rot->data);
delete(rot);
};
void cortexImage::fill(float s){
for (uint16_t i = 0; i < h*w*c; ++i) data[i] = s;
};
void cortexImage::translate(float s){
for (uint16_t i = 0; i < h*w*c; ++i) data[i] += s;
};
void cortexImage::scale(float s){
for (uint16_t i = 0; i < h*w*c; ++i) data[i] *= s;
};
void cortexImage::crop(uint32_t dx, uint32_t dy, uint32_t ww, uint32_t hh){
cortexImage *cropped;
cropped = new cortexImage();
cropped->makeWithData(ww, hh, c );
uint32_t i, j, k;
for(k = 0; k < c; ++k){
for(j = 0; j < hh; ++j){
for(i = 0; i < ww; ++i){
uint32_t r = j + dy;
uint32_t cc = i + dx;
float val = 0;
r = constrain_int(r, 0, h-1);
cc = constrain_int(cc, 0, w-1);
val = getPixel( cc, r, k);
cropped->setPixel(i, j, k, val);
};
};
};
setData(cropped->w, cropped->h, cropped->c, cropped->data);
delete(cropped);
};
int best3dShiftR(cortexImage *a, cortexImage *b, int min, int max){
if(min == max) return min;
int mid = (int)floor((min + max) / 2.);
cortexImage *c1;
cortexImage *c2;
c1 = new cortexImage(b);
c2 = new cortexImage(b);
c1->crop(0, mid, b->w, b->h);
c2->crop(0, mid + 1, b->w, b->h);
float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 10);
float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 10);
c1->freeData();
c2->freeData();
delete(c1);
delete(c2);
if( d1 < d2) return best3dShiftR(a, b, min, mid);
else return best3dShiftR(a, b, mid+1, max);
};
int best3dShift(cortexImage *a, cortexImage *b, int min, int max){
uint16_t i;
int best = 0;
float best_distance = FLT_MAX;
for(i = min; i <= max; i += 2){
cortexImage *c;
c = new cortexImage(b);
c->crop(0, i, b->w, b->h);
float d = dist_array(c->data, a->data, a->w*a->h*a->c, 100);
if(d < best_distance){
best_distance = d;
best = i;
}
printf("%d %f\n", i, d);
c->freeData();
delete(c);
};
return best;
};
void composite3d(char *f1, char *f2, char *out, int delta){
if(!out) out = "out";
cortexImage *a;
a = new cortexImage();
a->fillFromFile(f1, 0, 0, 3);
cortexImage *b;
b = new cortexImage();
b->fillFromFile(f2, 0, 0, 3);
int shift = best3dShiftR(a, b, -a->h/100, a->h/100);
cortexImage *c1;
c1 = new cortexImage(b);
c1->crop( 10, shift, b->w, b->h);
float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 100);
cortexImage *c2;
c2 = new cortexImage(b);
c2->crop(-10, shift, b->w, b->h);
float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 100);
if(d2 < d1 && 0){
cortexImage *swap = a;
a = b;
b = swap;
shift = -shift;
printf("swapped, %d\n", shift);
} else{
printf("%d\n", shift);
}
cortexImage *c;
c = new cortexImage(b);
c->crop( delta, shift, a->w, a->h);
int i;
for(i = 0; i < c->w*c->h; ++i){
c->data[i] = a->data[i];
};
c->saveJPG(out);
c->freeData();
c1->freeData();
c2->freeData();
a->freeData();
b->freeData();
delete(c);
delete(c1);
delete(c2);
delete(a);
delete(b);
};
void cortexImage::letterboxInto(cortexImage *boxed){
int new_w = w;
int new_h = h;
if (((float)boxed->w / w) < ((float)boxed->h / h)) {
new_w = boxed->w;
new_h = (h * boxed->w) / w;
} else {
new_h = boxed->h;
new_w = (w * boxed->h) / h;
};
cortexImage *resized;
resized = new cortexImage(this);
resized->resize(new_w, new_h);
boxed->embed(resized, (boxed->w-new_w)/2, (boxed->h-new_h)/2);
resized->freeData();
delete(resized);
};
void cortexImage::letterbox(uint32_t ww, uint32_t hh){
int new_w = w;
int new_h = h;
if (((float)ww/w) < ((float)hh/h)) {
new_w = ww;
new_h = (h * ww)/w;
} else {
new_h = hh;
new_w = (w * hh)/h;
}
resize( new_w, new_h);
cortexImage *boxed;
boxed = new cortexImage();
boxed->makeWithData(ww, hh, c);
boxed->fill(.5f);
//int i;
//for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0;
boxed->embed(this, (ww-new_w)/2, (hh-new_h)/2);
setData(ww, hh, c, boxed->data);
delete(boxed);
};
void cortexImage::resizeMax(uint32_t max){
int ww = w;
int hh = h;
if(ww > hh){
hh = (hh * max) / ww;
ww = max;
} else {
ww = (ww * max) / hh;
hh = max;
}
if(ww == w && hh == h) return;
resize( ww, hh );
};
void cortexImage::resizeMin(uint32_t min){
int ww = w;
int hh = h;
if(ww < hh){
hh = (hh * min) / ww;
ww = min;
} else {
ww = (ww * min) / hh;
hh = min;
};
if(ww == w && hh == h) return;
resize(ww, hh);
};
void cortexImage::randomCrop(uint32_t ww, uint32_t hh){
int dx = rand_int(0, w - ww);
int dy = rand_int(0, h - hh);
crop(dx, dy, ww, hh);
};
cortexARGS cortexImage::randomAugmentArgs(float angle, float aspect, uint32_t low, uint32_t high, uint32_t ww, uint32_t hh){
cortexARGS a = { 0 };
aspect = rand_scale(aspect);
uint32_t r = rand_int(low, high);
uint32_t min = (int)((h < w*aspect) ? h : w*aspect);
float scale = (float)r / min;
float rad = (float)(rand_uniform(-angle, angle) * TWO_PI / 360.f);
float dx = (float)((w*scale/aspect - ww) / 2.);
float dy = (float)((h*scale - ww) / 2.);
//if(dx < 0) dx = 0;
//if(dy < 0) dy = 0;
dx = rand_uniform(-dx, dx);
dy = rand_uniform(-dy, dy);
a.rad = rad;
a.scale = scale;
a.w = ww;
a.h = hh;
a.dx = dx;
a.dy = dy;
a.aspect = aspect;
return a;
}
void cortexImage::randomAugment(float angle, float aspect, uint32_t low, uint32_t high, uint32_t w, uint32_t h){
cortexARGS a = randomAugmentArgs(angle, aspect, low, high, w, h);
rotateCrop( a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);
};
float three_way_max(float a, float b, float c){
return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ;
};
float three_way_min(float a, float b, float c){
return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ;
};
void cortexImage::yuv_to_rgb(void){
assert( c == 3);
float r, g, b;
float y, u, v;
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
y = getPixel( i , j, 0);
u = getPixel( i , j, 1);
v = getPixel( i , j, 2);
r = (float)(y + 1.13983*v);
g = (float)(y + -.39465*u + -.58060*v);
b = (float)(y + 2.03211*u);
setPixel( i, j, 0, r);
setPixel( i, j, 1, g);
setPixel( i, j, 2, b);
};
};
};
void cortexImage::rgb_to_yuv(void){
assert(c == 3);
float r, g, b;
float y, u, v;
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
r = getPixel( i , j, 0);
g = getPixel( i , j, 1);
b = getPixel( i , j, 2);
y = (float)(.299*r + .587*g + .114*b);
u = (float)(-.14713*r + -.28886*g + .436*b);
v = (float)(.615*r + -.51499*g + -.10001*b);
setPixel( i, j, 0, y);
setPixel( i, j, 1, u);
setPixel( i, j, 2, v);
};
};
};
// http://www.cs.rit.edu/~ncs/color/t_convert.html
void cortexImage::rgb_to_hsv(void){
assert( c == 3);
float r, g, b;
float hh, s, v;
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
r = getPixel( i , j, 0);
g = getPixel( i , j, 1);
b = getPixel( i , j, 2);
float max = three_way_max(r,g,b);
float min = three_way_min(r,g,b);
float delta = max - min;
v = max;
if(max == 0){
s = 0;
hh = 0;
}else{
s = delta/max;
if(r == max){
hh = (g - b) / delta;
} else if (g == max) {
hh = 2 + (b - r) / delta;
} else {
hh = 4 + (r - g) / delta;
}
if (hh < 0) hh += 6;
hh = (float)(hh/6.);
}
setPixel( i, j, 0, hh);
setPixel( i, j, 1, s);
setPixel( i, j, 2, v);
};
};
};
void cortexImage::hsv_to_rgb( void ){
assert(c == 3);
float r, g, b;
float hh, s, v;
float f, p, q, t;
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
hh = 6 * getPixel( i , j, 0);
s = getPixel( i , j, 1);
v = getPixel( i , j, 2);
if (s == 0) {
r = g = b = v;
} else {
int index = (int)floor(hh);
f = hh - index;
p = v*(1-s);
q = v*(1-s*f);
t = v*(1-s*(1-f));
if(index == 0){
r = v; g = t; b = p;
} else if(index == 1){
r = q; g = v; b = p;
} else if(index == 2){
r = p; g = v; b = t;
} else if(index == 3){
r = p; g = q; b = v;
} else if(index == 4){
r = t; g = p; b = v;
} else {
r = v; g = p; b = q;
};
};
setPixel( i, j, 0, r);
setPixel( i, j, 1, g);
setPixel( i, j, 2, b);
};
};
};
void cortexImage::grayscale_3C(void){
assert( c == 3);
float scale[] = {(float)0.299, (float)0.587, 0.114f};
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
float val = 0;
for (uint8_t k = 0; k < 3; ++k){
val += scale[k]*getPixel( i, j, k);
}
data[0*h*w + w*j + i] = val;
data[1*h*w + w*j + i] = val;
data[2*h*w + w*j + i] = val;
};
};
};
void cortexImage::grayscale(void){
assert(c == 3);
//image gray = make_image(im.w, im.h, 1);
float scale[] = {0.299f, 0.587f, 0.114f};
for (uint16_t k = 0; k < c; ++k){
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
data[i+w*j] += scale[k]*getPixel( i, j, k);
};
};
};
};
void cortexImage::threshold(float thresh){
for (uint16_t i = 0; i < w*h*c; ++i){
data[i] = (float)(data[i]>thresh ? 1 : 0);
};
};
void cortexImage::blendAsFront(cortexImage *back, float alpha){
assert(w == back->w && h == back->h && c == back->c);
//image blend = make_image(fore.w, fore.h, fore.c);
for (uint16_t k = 0; k < c; ++k){
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
float val = alpha * getPixel( i, j, k) +
(1 - alpha)* back->getPixel(i, j, k);
setPixel( i, j, k, val );
};
};
};
};
void cortexImage::blendAsBack(cortexImage *fore, float alpha){
assert(fore->w == w && fore->h == h && fore->c == c);
//image blend = make_image(fore.w, fore.h, fore.c);
for (uint16_t k = 0; k < fore->c; ++k){
for (uint16_t j = 0; j < fore->h; ++j){
for (uint16_t i = 0; i < fore->w; ++i){
float val = alpha * fore->getPixel(i, j, k) +
(1 - alpha)* getPixel(i, j, k);
setPixel( i, j, k, val );
};
};
};
};
void cortexImage::scale_channel(uint32_t cc, float v){
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
float pix = getPixel( i, j, cc);
pix = pix*v;
setPixel( i, j, cc, pix);
};
};
};
void cortexImage::translate_channel(uint32_t cc, float v){
for (uint16_t j = 0; j < h; ++j){
for (uint16_t i = 0; i < w; ++i){
float pix = getPixel( i, j, cc);
pix = pix+v;
setPixel( i, j, cc, pix);
};
};
};
void cortexImage::binarize(void){
for (uint16_t i = 0; i < w * h * c; ++i){
if(data[i] > .5) data[i] = 1; else data[i] = 0;
};
};
void cortexImage::saturate(float sat){
rgb_to_hsv();
scale_channel(1, sat);
hsv_to_rgb();
constrain();
};
void cortexImage::hue(float hue){
rgb_to_hsv();
for (uint16_t i = 0; i < w*h; ++i){
data[i] = data[i] + hue;
if (data[i] > 1) data[i] -= 1;
if (data[i] < 0) data[i] += 1;
}
hsv_to_rgb();
constrain();
};
void cortexImage::exposure(float sat){
rgb_to_hsv();
scale_channel( 2, sat);
hsv_to_rgb();
constrain();
};
void cortexImage::distort( float hue, float sat, float val ){
rgb_to_hsv();
scale_channel( 1, sat);
scale_channel( 2, val);
for (uint16_t i = 0; i < w*h; ++i){
data[i] = data[i] + hue;
if (data[i] > 1) data[i] -= 1;
if (data[i] < 0) data[i] += 1;
}
hsv_to_rgb();
constrain();
};
void cortexImage::randomDistort(float hue, float saturation, float exposure){
float dhue = rand_uniform(-hue, hue);
float dsat = rand_scale(saturation);
float dexp = rand_scale(exposure);
distort(dhue, dsat, dexp);
};
void cortexImage::saturateExposure(float sat, float exposure){
rgb_to_hsv();
scale_channel(1, sat);
scale_channel(2, exposure);
hsv_to_rgb();
constrain();
};
float cortexImage::bilinear_interpolate(float x, float y, uint32_t cc){
int ix = (int) floorf(x);
int iy = (int) floorf(y);
float dx = x - ix;
float dy = y - iy;
float val = (1-dy) * (1-dx) * getPixelEX(ix, iy, cc) +
dy * (1-dx) * getPixelEX(ix, iy+1, cc) +
(1-dy) * dx * getPixelEX(ix+1, iy, cc) +
dy * dx * getPixelEX(ix+1, iy+1, cc);
return val;
};
void cortexImage::resize(uint32_t ww, uint32_t hh){
cortexImage resized;
cortexImage part;
resized.makeEmpty(ww, hh, c);
part.makeEmpty(ww, h, c);
uint32_t r, cc, k;
float w_scale = (float)(w - 1) / (ww - 1);
float h_scale = (float)(h - 1) / (hh - 1);
for(k = 0; k < c; ++k){
for(r = 0; r < h; ++r){
for(cc = 0; cc < ww; ++cc){
float val = 0;
if(cc == ww-1 || w == 1){
val = getPixel(w-1, r, k);
} else {
float sx = cc*w_scale;
int ix = (int) sx;
float dx = sx - ix;
val = (1 - dx) * getPixel(ix, r, k) + dx * getPixel(ix+1, r, k);
};
part.setPixel(cc, r, k, val);
};
};
};
for(k = 0; k < c; ++k){
for(r = 0; r < hh; ++r){
float sy = r*h_scale;
int iy = (int) sy;
float dy = sy - iy;
for(cc = 0; cc < ww; ++cc){
float val = (1 - dy) * part.getPixel(cc, iy, k);
resized.setPixel(cc, r, k, val);
};
if(r == hh-1 || h == 1) continue;
for(cc = 0; cc < ww; ++cc){
float val = dy * part.getPixel(cc, iy + 1, k);
resized.addPixel(cc, r, k, val);
};
};
};
part.freeData();
setData( ww, hh, resized.c, resized.data );
};
bool cortexImage::fillFromFile(char* path, uint32_t x, uint32_t y, uint32_t channels){
int ww, hh, cc;
unsigned char *Zdata = stbi_load(path, &ww, &hh, &cc, channels);
if (!data) {
fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", path, stbi_failure_reason());
return false;
};
if(channels) cc = channels;
int i,j,k;
makeWithData(ww, hh, cc);
for(k = 0; k < cc; ++k){
for(j = 0; j < hh; ++j){
for(i = 0; i < ww; ++i){
int dst_index = i + ww*j + ww*hh*k;
int src_index = k + cc*i + cc*ww*j;
data[dst_index] = (float)(Zdata[src_index]/255.);
};
};
};
free(Zdata);
return true;
};
float cortexImage::getPixel(uint32_t x, uint32_t y, uint32_t cc){
assert(x < w && y < h && cc < c);
return data[cc*h*w + y*w + x];
}
float cortexImage::getPixelEX(uint32_t x, uint32_t y, uint32_t cc){
if(x < 0 || x >= w || y < 0 || y >= h) return 0;
/*
if(x < 0) x = 0;
if(x >= m.w) x = m.w-1;
if(y < 0) y = 0;
if(y >= m.h) y = m.h-1;
*/
if(cc < 0 || cc >= c) return 0;
return getPixel( x, y, cc);
};
void cortexImage::setPixel(uint32_t x, uint32_t y, uint32_t cc, float val){
if (x < 0 || y < 0 || c < 0 || x >= w || y >= h || cc >= c) return;
assert(x < w && y < h && cc < c);
data[cc*h*w + y*w + x] = val;
};
void cortexImage::addPixel(uint32_t x, uint32_t y, uint32_t cc, float val){
assert(x < w && y < h && cc < c);
data[cc*h*w + y*w + x] += val;
};
void cortexImage::collapseImagesVert(cortexImageList *imgList){
int color = 1;
int border = 1;
int hh,ww,cc,ch;
ww = (*imgList->begin())->w;
ch = ((*imgList->begin())->h + border);
hh = ch * imgList->size() - border;
cc = (*imgList->begin())->c;
if(cc != 3 || !color){
ww = (ww+border)*cc - border;
cc = 1;
};
makeWithData(ww, hh, cc);
int i,j;
i = 0;
for ( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){
int h_offset = i*ch;
//(*index)->normalize();
if( cc == 3 && color){
embed( (*index), 0, h_offset);
} else{
for (j = 0; j < (*index)->c; ++j){
int w_offset = j*((*index)->w + border);
cortexImage *layer;
layer = (*index)->getLayer(j);
embed(layer, w_offset, h_offset);
layer->freeData();
delete(layer);
};
};
};
};
void cortexImage::collapseImagesHorz(cortexImageList *imgList){
int color = 1;
int border = 1;
int hh,cw,ww,cc;
int size = (*imgList->begin())->h;
hh = size;
cw = ((*imgList->begin())->w + border);
ww = cw * imgList->size() - border;
cc = (*imgList->begin())->c;
if(cc != 3 || !color){
hh = (hh+border)*cc - border;
cc = 1;
};
makeWithData(ww, hh, cc);
int i,j;
i = 0;
for( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){
int w_offset = i*cw;
//(*index)->normalize();
if(cc == 3 && color){
embed((*index), w_offset, 0);
} else {
for (j = 0; j < (*index)->c; ++j){
int h_offset = j*(size+border);
cortexImage *layer;
layer = (*index)->getLayer(j);
embed(layer, w_offset, h_offset);
layer->freeData();
delete(layer);
};
};
};
};