Commit 7af63f94 authored by Charron Maxime's avatar Charron Maxime
Browse files

Ajout de la partie GPU (simple)

parent d394589c
diff.png

313 KB

......@@ -2,7 +2,6 @@
unsigned char* CPU_rgb2grey (unsigned char *image, int imgWidth, int imgHeight);
void CPU_saveGreyPicture (unsigned char *image, char* filename, int imgWidth, int imgHeight);
unsigned char* CPU_generateRedBox(int x, int y, unsigned char *image, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
......
......@@ -3,39 +3,37 @@
#include <stdlib.h>
#include <math.h>
#include <omp.h>
static int inputImgWidth = -1;
static int inputImgHeight = -1;
#include "func_all.h"
static int searchImgWidth = -1;
static int searchImgHeight = -1;
static int methode_one = 1;
/*
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* GPU_rgb2grey (unsigned char *image, int imgWidth, int imgHeight) {
unsigned char *outputGrey = (unsigned char *) malloc(imgWidth * imgHeight * sizeof(unsigned char));
#pragma omp parallel for
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 GPU_saveGreyPicture (unsigned char *image, char* filename, int imgWidth, int imgHeight) {
unsigned char *imageFinal = (unsigned char *)malloc(imgWidth* imgHeight * 3 * sizeof(unsigned char));
#pragma omp parallel for
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);
}*/
savePicture(imageFinal, filename, imgWidth, imgHeight);
}
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) );
unsigned char* GPU_generateRedBox(int x, int y, unsigned char *image, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight){
unsigned char *ret = (unsigned char *)malloc(inputImgWidth * inputImgHeight * 3 * sizeof(unsigned char));
memcpy( ret, image, inputImgWidth * inputImgHeight * 3 * sizeof(unsigned char) );
#pragma omp parallel for
for ( int i = 0; i < searchImgWidth; i++ ){
ret[y*inputImgWidth*3 + (x+i)*3] = 255;
ret[y*inputImgWidth*3 + (x+i)*3 + 1] = 0;
......@@ -44,7 +42,8 @@ unsigned char* generateRedBox(int x, int y, unsigned char *image){
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;
}
}
#pragma omp parallel for
for ( int i = 0; i < searchImgHeight; i++ ){
ret[(y+i)*inputImgWidth*3 + x*3] = 255;
ret[(y+i)*inputImgWidth*3 + x*3 + 1] = 0;
......@@ -57,46 +56,49 @@ unsigned char* generateRedBox(int x, int y, unsigned char *image){
return ret;
}
unsigned char* calculateDiff(unsigned char* input, unsigned char* search) {
unsigned char* GPU_calculateDiff(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight) {
double compute_diff = omp_get_wtime();
unsigned char *diff = (unsigned char *) malloc( (inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight) * sizeof(unsigned char));
#pragma omp parallel for
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
// print the process
printf("%.2f%% (%.0f seconds)\r",(float) ((y * (inputImgWidth-searchImgWidth) + x) *100) / ((inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight)), (float) (omp_get_wtime() - compute_diff));
printf("(%.0f seconds)\r", (float) (omp_get_wtime() - compute_diff));
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 )
//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]);
//else
// sum += abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]);
//}
}
}
if ( methode_one ) {
//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{
//}else{
// Methode 2
diff[y * (inputImgWidth - searchImgWidth) + x] = sum / 5440;
}
//diff[y * (inputImgWidth - searchImgWidth) + x] = sum / 5440;
//}
}
}
fflush(stdout);
printf("\rDone in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - compute_diff);
int* time = convert((float) (omp_get_wtime() - compute_diff));
printf("\rDone in \033[0;31m %d minutes %d seconds \033[0m\n", time[0], time[1]);
return diff;
}
int* findBestCompability (unsigned char* diff){
int* GPU_findBestCompability (unsigned char* diff, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight){
int minY = 0;
int minX = 0;
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
......
unsigned char* rgb2grey (unsigned char *image, int);
void saveGreyPicture (unsigned char *image, char* filename, int isInput);
unsigned char* generateRedBox(int x, int y, unsigned char *image);
unsigned char* GPU_rgb2grey (unsigned char *image, int imgWidth, int imgHeight);
unsigned char* calculateDiff(unsigned char* input, unsigned char* search);
void GPU_saveGreyPicture (unsigned char *image, char* filename, int imgWidth, int imgHeight);
int* findBestCompability (unsigned char* diff);
\ No newline at end of file
unsigned char* GPU_generateRedBox(int x, int y, unsigned char *image, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
unsigned char* GPU_calculateDiff(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
int* GPU_findBestCompability (unsigned char* diff, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
\ No newline at end of file
img/output/final_m1.png

3.81 MB | W: | H:

img/output/final_m1.png

1.19 MB | W: | H:

img/output/final_m1.png
img/output/final_m1.png
img/output/final_m1.png
img/output/final_m1.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -5,7 +5,7 @@
#include <omp.h>
#include "functions/func_all.h"
#include "functions/func_CPU.h"
//#include "functions/func_OPEN.h"
#include "functions/func_OPEN.h"
static const int IS_INPUT = 1;
static const int IS_SEARCH = 0;
......@@ -47,6 +47,7 @@ int main (int argc, char *argv[])
printf("\n============================================\n");
printf("\t\tCPU\n");
printf("============================================\n");
double temps_cpu = omp_get_wtime();
// ==================================== Convert input image to grey scale
......@@ -85,24 +86,25 @@ int main (int argc, char *argv[])
printf("======================================================\n");
printf("The CPU took \033[0;31m %f \033[0m seconds to execute\n", omp_get_wtime() - temps_cpu);
free(inputGrey);
free(searchGrey);
free(coord);
printf("The CPU took \033[0;31m %f \033[0m seconds to execute\n", omp_get_wtime() - temps_initial);
/*
// ============================================================================================================
printf("\n============================================\n");
printf("\t\tOPEN MP\n");
printf("============================================\n");
double temps_open = omp_get_wtime();
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
input_to_grey = omp_get_wtime();
inputGrey = rgb2grey(inputImg, IS_INPUT);
input_to_grey = omp_get_wtime();
inputGrey = GPU_rgb2grey(inputImg, inputImgWidth, inputImgHeight);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "input");
printf("Done in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - input_to_grey);
......@@ -110,36 +112,33 @@ int main (int argc, char *argv[])
// ==================================== Convert search image to grey scale
printf("Convert search to grey\n");
search_to_grey = omp_get_wtime();
searchGrey = rgb2grey(searchImg, IS_SEARCH);
searchGrey = GPU_rgb2grey(searchImg, searchImgWidth, searchImgHeight);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "search");
printf("Done in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - search_to_grey);
// ==================================== Calculate compability
printf("Calculate difference\n");
// time execution managed in the function
diff = calculateDiff(inputGrey,searchGrey);
diff = GPU_calculateDiff(inputGrey,searchGrey, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
//saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, (char*)"debug_m1");
// ==================================== Search max compability
find_compa = omp_get_wtime();
coord = findBestCompability(diff);
coord = GPU_findBestCompability(diff, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
printf("Done in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - find_compa);
free(diff);
// ==================================== Save a copy of 'inputImg' with the red box
printf("Save picture with red box\n");
box = omp_get_wtime();
final_m1 = generateRedBox(coord[0], coord[1], inputImg);
final_m1 = GPU_generateRedBox(coord[0], coord[1], inputImg, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
printf("Done in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - box);
savePicture(final_m1, (char*)"final_m1", 1);
savePicture(final_m1, (char*)"final_m1", inputImgWidth, inputImgHeight);
free(final_m1);
printf("======================================================\n");
free(inputGrey);
free(searchGrey);
free(diff);
free(coord);
printf("The OPEN MP part took \033[0;31m %f \033[0m seconds to execute\n", omp_get_wtime() - temps_initial);
printf("The OPEN MP part took \033[0;31m %f \033[0m seconds to execute\n", omp_get_wtime() - temps_open);
......@@ -148,7 +147,6 @@ int main (int argc, char *argv[])
*/
free_img(inputImg);
free_img(searchImg);
int* time = convert((float)(omp_get_wtime() - temps_initial));
......
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
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