Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
xwasco committed May 6, 2021
1 parent 20c1237 commit 80c3c54
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 126 deletions.
76 changes: 76 additions & 0 deletions DAISGram.cpp
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);
}
64 changes: 14 additions & 50 deletions DAISGram.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,16 @@ class DAISGram{

public:

DAISGram(){}
DAISGram();

~DAISGram(){}
~DAISGram();

/**
* Load a bitmap from file
*
* @param filename String containing the path of the file
*/
void 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);
}
}
}
void load_image(string filename);


/**
Expand All @@ -54,23 +37,7 @@ class DAISGram{
*
* @param filename String containing the path where to store the image.
*/
void 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);

}
void save_image(string filename);

/**
* Get rows
Expand Down Expand Up @@ -98,6 +65,8 @@ class DAISGram{
*
* It sums the bright variable to all the values in the image.
*
* Before returning the image, the corresponding tensor should be clamped in [0,255]
*
* @param bright the amount of bright to add (if negative the image gets darker)
* @return returns a new DAISGram containing the modified object
*/
Expand Down Expand Up @@ -138,7 +107,7 @@ class DAISGram{
* 0 -1 0
*
* Before returning the image, the corresponding tensor should be clamped in [0,255]
*
*
* @return returns a new DAISGram containing the modified object
*/
DAISGram sharpen();
Expand All @@ -153,7 +122,7 @@ class DAISGram{
* -2 -1 0
* -1 1 1
* 0 1 2
*
*
* Before returning the image, the corresponding tensor should be clamped in [0,255]
*
* @return returns a new DAISGram containing the modified object
Expand All @@ -176,7 +145,7 @@ class DAISGram{
* @param h the size of the filter
* @return returns a new DAISGram containing the modified object
*/
DAISGram smooth(int h=3);
DAISGram smooth(int h=3);

/**
* Edges of an image
Expand All @@ -190,13 +159,13 @@ class DAISGram{
* -1 8 -1
* -1 -1 -1
*
* Remember to convert the image to grayscale before running the convolution.
* Remeber to convert the image to grayscale before running the convolution.
*
* Before returning the image, the corresponding tensor should be clamped in [0,255]
*
* @return returns a new DAISGram containing the modified object
*/
DAISGram edge();
DAISGram edge();

/**
* Blend with anoter image
Expand All @@ -213,12 +182,12 @@ class DAISGram{
* @param alpha The parameter of the convex combination
* @return returns a new DAISGram containing the blending of the two images.
*/
DAISGram blend(DAISGram rhs, float alpha);
DAISGram blend(const DAISGram & rhs, float alpha=0.5);

/**
* Green Screen
*
* This function substitutes a pixe with the corresponding one in a background image
* This function substitutes a pixel with the corresponding one in a background image
* if its colors are in the surrounding (+- threshold) of a given color (rgb).
*
* (rgb - threshold) <= pixel <= (rgb + threshold)
Expand Down Expand Up @@ -253,12 +222,7 @@ class DAISGram{
* @param d number of channels
* @return returns a new DAISGram containing the generated image.
*/
void generate_random(int h, int w, int d){
data = Tensor(h,w,d,0.0);
data.init_random(128,30);
data.rescale(255);
}

void generate_random(int h, int w, int d);
};

#endif
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int main (int argc, char * argv[]) {
img = b.brighten(k_size); /* aumenta la luminosità */
}
else if (strcmp(operation, "blend") == 0) {
cout<<alpha<<endl;
c.load_image(fn_in_2);
img = b.blend(c, alpha); /* effettua il blending di due immagini */
}else if (strcmp(operation, "gray") == 0) {
Expand Down
69 changes: 69 additions & 0 deletions main_tensor.cpp
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!*/
}
42 changes: 42 additions & 0 deletions tensor.cpp
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());
}
}
Loading

0 comments on commit 80c3c54

Please sign in to comment.