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

Creation fichier pour Open mp

parent 7a705629
......@@ -4,10 +4,15 @@
#include <math.h>
#include <omp.h>
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "lib_stb_image/stb_image.h"
#endif
#ifndef STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "lib_stb_image/stb_image_write.h"
#endif
......
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "lib_stb_image/stb_image.h"
#endif
#ifndef STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "lib_stb_image/stb_image_write.h"
#endif
static int inputImgWidth = -1;
static int inputImgHeight = -1;
static int searchImgWidth = -1;
static int searchImgHeight = -1;
static int methode_one = 1;
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) {
double compute_diff = omp_get_wtime();
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% (%.0f seconds)\r",(float) ((y * (inputImgWidth-searchImgWidth) + x) *100) / ((inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight)), (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 )
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;
}
}
}
fflush(stdout);
printf("\rDone in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - compute_diff);
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* 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
img/output/final_m1.png

89.8 KB | W: | H:

img/output/final_m1.png

3.81 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
......@@ -3,14 +3,14 @@
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include "functions.h"
#include "func_CPU.h"
#include "func_OPEN.h"
static const int IS_INPUT = 1;
static const int IS_SEARCH = 0;
int main (int argc, char *argv[])
{
printf("Version : CPU\n");
{
double temps_initial = omp_get_wtime();
if (argc != 3)
......@@ -34,7 +34,10 @@ int main (int argc, char *argv[])
// ============================================================================================================
printf("\n============================================\n");
printf("\t\tCPU\n");
printf("============================================\n");
// ==================================== Convert input image to grey scale
......@@ -52,13 +55,7 @@ int main (int argc, char *argv[])
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "search");
printf("Done in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - search_to_grey);
// ============================================================================================================
// METHODE 1
// ============================================================================================================
// ==================================== Calculate compability
//printf("===================== Methode 1 =====================\n");
printf("Calculate difference\n");
// time execution managed in the function
unsigned char *diff = calculateDiff(inputGrey,searchGrey);
......@@ -79,27 +76,70 @@ int main (int argc, char *argv[])
printf("======================================================\n");
free(inputGrey);
free(searchGrey);
free(diff);
free(coord);
printf("The CPU took \033[0;31m %f \033[0m seconds to execute\n", omp_get_wtime() - temps_initial);
// ============================================================================================================
// METHODE 2
// ============================================================================================================
/*
printf("\n============================================\n");
printf("\t\tOPEN MP\n");
printf("============================================\n");
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
input_to_grey = omp_get_wtime();
inputGrey = rgb2grey(inputImg, IS_INPUT);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "input");
printf("Done in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - input_to_grey);
// ==================================== Convert search image to grey scale
printf("Convert search to grey\n");
search_to_grey = omp_get_wtime();
searchGrey = rgb2grey(searchImg, IS_SEARCH);
//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("===================== Methode 2 =====================\n");
methode_one = 0;
printf("Calculate difference\n");
diff = calculateDiff(inputGrey, searchGrey);
saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, "debug_m2");
// time execution managed in the function
diff = calculateDiff(inputGrey,searchGrey);
//saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, (char*)"debug_m1");
// ==================================== Search max compability
find_compa = omp_get_wtime();
coord = findBestCompability(diff);
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
unsigned char *final_m2 = generateRedBox(coord[0], coord[1], inputImg);
savePicture(inputImgHeight, inputImgWidth, final_m2, "final_m2");
free(final_m2);
printf("Save picture with red box\n");
box = omp_get_wtime();
final_m1 = generateRedBox(coord[0], coord[1], inputImg);
printf("Done in \033[0;31m %f \033[0m seconds\n",omp_get_wtime() - box);
savePicture(final_m1, (char*)"final_m1", 1);
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);
free_img(inputImg);
free_img(searchImg);
......
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
tp_projet_para: main.c ./lib_stb_image/*.h ./func_CPU.h ./func_OPEN.h
g++ -fopenmp -o tp_projet main_parra.c ./lib_stb_image/* ./func_CPU.c ./func_OPEN.c
tp_projet: main.c ./lib_stb_image/*.h ./func_CPU.h ./func_OPEN.h
g++ -fopenmp -o tp_projet main.c ./lib_stb_image/* ./func_CPU.c ./func_OPEN.c
File deleted
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