-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
261 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include "dais_exc.h" | ||
#include "tensor.h" | ||
#include "libbmp.h" | ||
#include "DAISGram.h" | ||
|
||
using namespace std; | ||
|
||
/** | ||
* Load a bitmap from file | ||
* | ||
* @param filename String containing the path of the file | ||
*/ | ||
void DAISGram::load_image(string filename){ | ||
BmpImg img = BmpImg(); | ||
|
||
img.read(filename.c_str()); | ||
|
||
const int h = img.get_height(); | ||
const int w = img.get_width(); | ||
|
||
data = Tensor(h, w, 3, 0.0); | ||
|
||
for(int i=0;i<img.get_height();i++){ | ||
for(int j=0;j<img.get_width();j++){ | ||
data(i,j,0) = (float) img.red_at(j,i); | ||
data(i,j,1) = (float) img.green_at(j,i); | ||
data(i,j,2) = (float) img.blue_at(j,i); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Save a DAISGram object to a bitmap file. | ||
* | ||
* Data is clamped to 0,255 before saving it. | ||
* | ||
* @param filename String containing the path where to store the image. | ||
*/ | ||
void DAISGram::save_image(string filename){ | ||
|
||
data.clamp(0,255); | ||
|
||
BmpImg img = BmpImg(getCols(), getRows()); | ||
|
||
img.init(getCols(), getRows()); | ||
|
||
for(int i=0;i<getRows();i++){ | ||
for(int j=0;j<getCols();j++){ | ||
img.set_pixel(j,i,(unsigned char) data(i,j,0),(unsigned char) data(i,j,1),(unsigned char) data(i,j,2)); | ||
} | ||
} | ||
|
||
img.write(filename); | ||
|
||
} | ||
|
||
|
||
/** | ||
* Generate Random Image | ||
* | ||
* Generate a random image from nois | ||
* | ||
* @param h height of the image | ||
* @param w width of the image | ||
* @param d number of channels | ||
* @return returns a new DAISGram containing the generated image. | ||
*/ | ||
void DAISGram::generate_random(int h, int w, int d){ | ||
data = Tensor(h,w,d,0.0); | ||
data.init_random(128,50); | ||
data.rescale(255); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include "tensor.h" | ||
|
||
void show_help(){ | ||
printf("*** Tensors Operations ***\n"); | ||
printf("\targ 1: input file name (tensor1) \n"); | ||
printf("\targ 2: input file name (tensor2) \n"); | ||
printf("\targ 3: operazione da effettuare (+,-,/,*,convolve, concat)\n"); | ||
printf("\targ 4: output file name\n"); | ||
printf("\targ 5: Diversi significati in funzione dell'operazione (default 3):\n" | ||
"\t\t- [smooth]: kernel size \n" | ||
"\t\t- [brighten]: valore bright per aumentare la luminosità \n" | ||
"\t\t\n"); | ||
printf("\targ 6: Diversi significati in funzione dell'operazione (default 1.0):\n" | ||
"\t\t- [blend] parametro alpha per il blending di due immagini"); | ||
printf("\n"); | ||
} | ||
|
||
int main (int argc, char * argv[]) { | ||
|
||
char * fn_in_1; /* file 1 */ | ||
char * fn_in_2; /* file 2 */ | ||
char * operation; /* operazione da eseguire */ | ||
char * fn_out; /* output file */ | ||
|
||
int axis = 3; /* axis for concat */ | ||
|
||
/* variabili di appoggio per le computazioni */ | ||
Tensor a,b,out; | ||
|
||
if(argc<4){ | ||
show_help(); | ||
return 0; | ||
} | ||
|
||
fn_in_1 = argv[1]; /* file 1 */ | ||
fn_in_2 = argv[2]; /* file 2 */ | ||
operation = argv[3]; /* operazione da eseguire */ | ||
fn_out = argv[4]; /* output file */ | ||
|
||
if(argc>5) { | ||
axis = atoi(argv[5]); | ||
} | ||
|
||
a.read_file(fn_in_1); | ||
b.read_file(fn_in_2); | ||
|
||
if (strcmp(operation, "+") == 0) { | ||
out=a+b; /* aumenta la luminosità */ | ||
}else if(strcmp(operation, "-") == 0) { | ||
out=a-b; /* aumenta la luminosità */ | ||
}else if(strcmp(operation, "*") == 0) { | ||
out=a*b; /* aumenta la luminosità */ | ||
}else if(strcmp(operation, "/") == 0) { | ||
out=a/b; /* aumenta la luminosità */ | ||
}else if(strcmp(operation, "convolve") == 0) { | ||
out=a.convolve(b); /* aumenta la luminosità */ | ||
}else if(strcmp(operation, "concat") == 0) { | ||
out=a.concat(b,axis); /* aumenta la luminosità */ | ||
}else { | ||
throw(unknown_operation()); | ||
} | ||
|
||
out.write_file(fn_out); | ||
|
||
return 0; /* ciao a tutti!*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <random> | ||
#include <math.h> | ||
#include <fstream> | ||
|
||
#include "dais_exc.h" | ||
#include "tensor.h" | ||
|
||
#define PI 3.141592654 | ||
#define FLT_MAX 3.402823466e+38F /* max value */ | ||
#define FLT_MIN 1.175494351e-38F /* min positive value */ | ||
|
||
using namespace std; | ||
|
||
|
||
/** | ||
* Random Initialization | ||
* | ||
* Perform a random initialization of the tensor | ||
* | ||
* @param mean The mean | ||
* @param std Standard deviation | ||
*/ | ||
void Tensor::init_random(float mean, float std){ | ||
if(data){ | ||
|
||
std::default_random_engine generator; | ||
std::normal_distribution<float> distribution(mean,std); | ||
|
||
for(int i=0;i<r;i++){ | ||
for(int j=0;j<c;j++){ | ||
for(int k=0;k<d;k++){ | ||
this->operator()(i,j,k)= distribution(generator); | ||
} | ||
} | ||
} | ||
|
||
}else{ | ||
throw(tensor_not_initialized()); | ||
} | ||
} |
Oops, something went wrong.