Commit ba20db3f authored by Charron Maxime's avatar Charron Maxime
Browse files

CPU : fonctions dans un fichier à part

parent 81c472fc
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#define STB_IMAGE_IMPLEMENTATION
#include "lib_stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "lib_stb_image/stb_image_write.h"
static int inputImgWidth = -1;
static int inputImgHeight = -1;
static int searchImgWidth = -1;
static int searchImgHeight = -1;
static int methode_one = 1;
unsigned char* load_img(const char * imgPath, int isInput) {
int dummyNbChannels; // number of channels forced to 3 in stb_load.
unsigned char *img;
if ( isInput )
img = stbi_load(imgPath, &inputImgWidth, &inputImgHeight, &dummyNbChannels, 3);
else
img = stbi_load(imgPath, &searchImgWidth, &searchImgHeight, &dummyNbChannels, 3);
if (img == NULL)
{
printf("Cannot load image %s", imgPath);
//return EXIT_FAILURE;
}
if ( isInput )
printf("Loaded : image %s: %dx%d\n", imgPath, inputImgWidth, inputImgHeight);
else
printf("Loaded : image %s: %dx%d\n", imgPath, searchImgWidth, searchImgHeight);
return img;
}
void free_img(unsigned char* img) {
stbi_image_free(img);
}
unsigned char* rgb2grey (unsigned char *image, int isInput) {
int imgHeight;
int imgWidth;
if ( isInput ) {
imgHeight = inputImgHeight;
imgWidth = inputImgWidth;
}else{
imgHeight = searchImgHeight;
imgWidth = searchImgWidth;
}
unsigned char *outputGrey = (unsigned char *) malloc(imgWidth * imgHeight * sizeof(unsigned char));
for ( int index = 0; index < imgHeight*imgWidth; index++ ){
outputGrey[index] = 0.299*image[index*3] + 0.587*image[index*3 + 1] + 0.114*image[index*3 + 2];
}
return outputGrey;
}
void savePicture (unsigned char *image, char* filename, int isInput) {
int imgHeight;
int imgWidth;
if ( isInput ) {
imgHeight = inputImgHeight;
imgWidth = inputImgWidth;
}else{
imgHeight = searchImgHeight;
imgWidth = searchImgWidth;
}
unsigned char *save_img = (unsigned char *)malloc(imgWidth* imgHeight * 3 * sizeof(unsigned char));
memcpy( save_img, image, imgWidth* imgHeight * 3 * sizeof(unsigned char) );
char str[60] = "img/output/";
strcat(str, filename);
strcat(str, ".png");
stbi_write_png(str, imgWidth, imgHeight, 3, save_img, imgWidth* 3);
printf("Picture saved : %s\n",str);
}
void saveGreyPicture (unsigned char *image, char* filename, int isInput) {
int imgHeight;
int imgWidth;
if ( isInput ) {
imgHeight = inputImgHeight;
imgWidth = inputImgWidth;
}else{
imgHeight = searchImgHeight;
imgWidth = searchImgWidth;
}
unsigned char *imageFinal = (unsigned char *)malloc(imgWidth* imgHeight * 3 * sizeof(unsigned char));
for ( int index = 0; index < imgHeight*imgWidth; index++ ){
imageFinal[index*3] = image[index];
imageFinal[index*3 + 1] = image[index];
imageFinal[index*3 + 2] = image[index];
}
savePicture(imageFinal, filename, isInput);
}
unsigned char* generateRedBox(int x, int y, unsigned char *image){
int imgHeight = inputImgHeight;
int imgWidth = inputImgWidth;
unsigned char *ret = (unsigned char *)malloc(imgWidth * imgHeight * 3 * sizeof(unsigned char));
memcpy( ret, image, imgWidth * imgHeight * 3 * sizeof(unsigned char) );
for ( int i = 0; i < searchImgWidth; i++ ){
ret[y*inputImgWidth*3 + (x+i)*3] = 255;
ret[y*inputImgWidth*3 + (x+i)*3 + 1] = 0;
ret[y*inputImgWidth*3 + (x+i)*3 + 2] = 0;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3] = 255;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3 + 1] = 0;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3 + 2] = 0;
}
for ( int i = 0; i < searchImgHeight; i++ ){
ret[(y+i)*inputImgWidth*3 + x*3] = 255;
ret[(y+i)*inputImgWidth*3 + x*3 + 1] = 0;
ret[(y+i)*inputImgWidth*3 + x*3 + 2] = 0;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3] = 255;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3 + 1] = 0;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3 + 2] = 0;
}
return ret;
}
unsigned char* calculateDiff(unsigned char* input, unsigned char* search) {
unsigned char *diff = (unsigned char *) malloc( (inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight) * sizeof(unsigned char));
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
// print the process
printf("%.2f %\r",(float) ((y * (inputImgWidth-searchImgWidth) + x) *100) / ((inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight)));
fflush(stdout);
int sum = 0;
for ( int yBox = 0; yBox < searchImgHeight; yBox++ ) {
for ( int xBox = 0; xBox < searchImgWidth; xBox++ ) {
//if ( search[ (yBox) * inputImgWidth + xBox] > 0 ) {
// Methode 1 : Avec le carré des différences
if ( methode_one )
sum += pow(abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]),2);
// Methode 2 : Sans le carré des différences
else
sum += abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]);
//}
}
}
if ( methode_one ) {
// Methode 1
// rapporter la somme entre 0 et 255 ( 255^2 * largeur_goat * hauteur_goat / 255)
double som_moy = sum / 1387200;
diff[y * (inputImgWidth - searchImgWidth) + x] = round(som_moy);
}else{
// Methode 2
diff[y * (inputImgWidth - searchImgWidth) + x] = sum / 5440;
}
}
}
printf("\n");
return diff;
}
int* findBestCompability (unsigned char* diff){
int minY = 0;
int minX = 0;
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
if (diff[y * (inputImgWidth - searchImgWidth) + x] < diff[minY * (inputImgWidth - searchImgWidth) + minX]){
minY = y;
minX = x;
}
}
}
int* ret = (int*) malloc( 2 * sizeof(int));
ret[0] = minX;
ret[1] = minY;
printf("Best compatibily find at x=%d and y=%d\n",minX, minY);
return ret;
}
\ No newline at end of file
unsigned char* load_img(const char *, int);
void free_img(unsigned char*);
unsigned char* rgb2grey (unsigned char *image, int);
void savePicture (unsigned char *image, char* filename, int isInput);
void saveGreyPicture (unsigned char *image, char* filename, int isInput);
unsigned char* generateRedBox(int x, int y, unsigned char *image);
unsigned char* calculateDiff(unsigned char* input, unsigned char* search);
int* findBestCompability (unsigned char* diff);
\ No newline at end of file
......@@ -3,136 +3,7 @@
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#define STB_IMAGE_IMPLEMENTATION
#include "lib_stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "lib_stb_image/stb_image_write.h"
static int inputImgWidth = -1;
static int inputImgHeight = -1;
static int searchImgWidth = -1;
static int searchImgHeight = -1;
static int methode_one = 1;
unsigned char* rgb2grey (int imgHeight, int imgWidth, unsigned char *image) {
unsigned char *outputGrey = (unsigned char *) malloc(imgWidth * imgHeight * sizeof(unsigned char));
for ( int index = 0; index < imgHeight*imgWidth; index++ ){
outputGrey[index] = 0.299*image[index*3] + 0.587*image[index*3 + 1] + 0.114*image[index*3 + 2];
}
return outputGrey;
}
void savePicture (int imgHeight, int imgWidth, unsigned char *image, char* filename) {
unsigned char *save_img = (unsigned char *)malloc(imgWidth* imgHeight * 3 * sizeof(unsigned char));
memcpy( save_img, image, imgWidth* imgHeight * 3 * sizeof(unsigned char) );
char str[60] = "img/output/";
strcat(str, filename);
strcat(str, ".png");
stbi_write_png(str, imgWidth, imgHeight, 3, save_img, imgWidth* 3);
printf("Picture saved : %s\n",str);
}
void saveGreyPicture (int imgHeight, int imgWidth, unsigned char *image, char* filename) {
unsigned char *imageFinal = (unsigned char *)malloc(imgWidth* imgHeight * 3 * sizeof(unsigned char));
for ( int index = 0; index < imgHeight*imgWidth; index++ ){
imageFinal[index*3] = image[index];
imageFinal[index*3 + 1] = image[index];
imageFinal[index*3 + 2] = image[index];
}
savePicture(imgHeight, imgWidth, imageFinal, filename);
}
unsigned char* generateRedBox(int x, int y, unsigned char *image){
int imgHeight = inputImgHeight;
int imgWidth = inputImgWidth;
unsigned char *ret = (unsigned char *)malloc(imgWidth * imgHeight * 3 * sizeof(unsigned char));
memcpy( ret, image, imgWidth * imgHeight * 3 * sizeof(unsigned char) );
for ( int i = 0; i < searchImgWidth; i++ ){
ret[y*inputImgWidth*3 + (x+i)*3] = 255;
ret[y*inputImgWidth*3 + (x+i)*3 + 1] = 0;
ret[y*inputImgWidth*3 + (x+i)*3 + 2] = 0;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3] = 255;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3 + 1] = 0;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3 + 2] = 0;
}
for ( int i = 0; i < searchImgHeight; i++ ){
ret[(y+i)*inputImgWidth*3 + x*3] = 255;
ret[(y+i)*inputImgWidth*3 + x*3 + 1] = 0;
ret[(y+i)*inputImgWidth*3 + x*3 + 2] = 0;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3] = 255;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3 + 1] = 0;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3 + 2] = 0;
}
return ret;
}
unsigned char* calculateDiff(unsigned char* input, unsigned char* search) {
unsigned char *diff = (unsigned char *) malloc( (inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight) * sizeof(unsigned char));
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
// print the process
printf("%.2f %\r",(float) ((y * (inputImgWidth-searchImgWidth) + x) *100) / ((inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight)));
fflush(stdout);
int sum = 0;
for ( int yBox = 0; yBox < searchImgHeight; yBox++ ) {
for ( int xBox = 0; xBox < searchImgWidth; xBox++ ) {
//if ( search[ (yBox) * inputImgWidth + xBox] > 0 ) {
// Methode 1 : Avec le carré des différences
if ( methode_one )
sum += pow(abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]),2);
// Methode 2 : Sans le carré des différences
else
sum += abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]);
//}
}
}
if ( methode_one ) {
// Methode 1
// rapporter la somme entre 0 et 255 ( 255^2 * largeur_goat * hauteur_goat / 255)
double som_moy = sum / 1387200;
diff[y * (inputImgWidth - searchImgWidth) + x] = round(som_moy);
}else{
// Methode 2
diff[y * (inputImgWidth - searchImgWidth) + x] = sum / 5440;
}
}
}
printf("\n");
return diff;
}
int* findBestCompability (unsigned char* diff){
int minY = 0;
int minX = 0;
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
if (diff[y * (inputImgWidth - searchImgWidth) + x] < diff[minY * (inputImgWidth - searchImgWidth) + minX]){
minY = y;
minX = x;
}
}
}
int* ret = (int*) malloc( 2 * sizeof(int));
ret[0] = minX;
ret[1] = minY;
printf("Best compatibily find at x=%d and y=%d\n",minX, minY);
return ret;
}
#include "functions.h"
int main (int argc, char *argv[])
......@@ -151,6 +22,9 @@ int main (int argc, char *argv[])
const char *searchImgPath = argv[2];
// ==================================== Loading input image.
unsigned char *inputImg = load_img(inputImgPath, 1);
unsigned char *searchImg = load_img(searchImgPath, 0);
/*
int dummyNbChannels; // number of channels forced to 3 in stb_load.
unsigned char *inputImg = stbi_load(inputImgPath, &inputImgWidth, &inputImgHeight, &dummyNbChannels, 3);
if (inputImg == NULL)
......@@ -168,7 +42,7 @@ int main (int argc, char *argv[])
return EXIT_FAILURE;
}
printf("Search image %s: %dx%d\n", searchImgPath, searchImgWidth, searchImgHeight);
*/
......@@ -178,14 +52,14 @@ int main (int argc, char *argv[])
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
unsigned char *inputGrey = rgb2grey(inputImgHeight, inputImgWidth, inputImg);
unsigned char *inputGrey = rgb2grey(inputImg, 1);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "input");
printf("Done\n");
// ==================================== Convert search image to grey scale
printf("Convert search to grey\n");
unsigned char *searchGrey = rgb2grey(searchImgHeight, searchImgWidth, searchImg);
unsigned char *searchGrey = rgb2grey(searchImg, 0);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "search");
printf("Done\n");
......@@ -197,16 +71,16 @@ int main (int argc, char *argv[])
// ==================================== Calculate compability
printf("===================== Methode 1 =====================\n");
printf("Calculate difference\n");
unsigned char *diff = calculateDiff(inputGrey, searchGrey);
saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, (char*)"debug_m1");
unsigned char *diff = calculateDiff(inputGrey,searchGrey);
//saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, (char*)"debug_m1");
// ==================================== Search max compability
int* coord = findBestCompability(diff);
free(diff);
// ==================================== Save a copy of 'inputImg' with the red box
unsigned char *final_m1 = generateRedBox(coord[0], coord[1], inputImg);
savePicture(inputImgHeight, inputImgWidth, final_m1, (char*)"final_m1");
free(final_m1);
savePicture(final_m1, (char*)"final_m1", 1);
free(final_m1);
printf("======================================================\n");
......@@ -232,9 +106,10 @@ int main (int argc, char *argv[])
printf("======================================================\n");
*/
stbi_image_free(inputImg);
stbi_image_free(searchImg);
free_img(inputImg);
free_img(searchImg);
//stbi_image_free(inputImg);
//stbi_image_free(searchImg);
printf("The program took %f seconds to execute\n", omp_get_wtime() - temps_initial);
printf("Good bye!\n");
......
tp_projet_para: main.c ./lib_stb_image/*.h
g++ -fopenmp -o tp_projet main_parra.c ./lib_stb_image/*
tp_projet: main.c ./lib_stb_image/*.h
g++ -fopenmp -o tp_projet main.c ./lib_stb_image/*
tp_projet_para: main.c ./lib_stb_image/*.h ./functions.h
g++ -fopenmp -o tp_projet main_parra.c ./lib_stb_image/* ./functions.c
tp_projet: main.c ./lib_stb_image/*.h ./functions.h
g++ -fopenmp -o tp_projet main.c ./lib_stb_image/* ./functions.c
......@@ -12,7 +12,7 @@ chmod 755 ./tp_projet
: '
Image espace
'
./tp_projet ./img/space.png ./img/goat.png
#./tp_projet ./img/space.png ./img/goat.png
: '
Image jungle
......@@ -22,5 +22,5 @@ Image jungle
: '
Image plage
'
#./tp_projet ./img/beach.png ./img/goat.png
./tp_projet ./img/beach.png ./img/goat.png
No preview for this file type
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment