Commit 8221bba8 authored by Charron Maxime's avatar Charron Maxime
Browse files

Généralisation des méthodes

parent c0835c77
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
File added
......@@ -3,66 +3,13 @@
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include "func_all.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;
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* CPU_rgb2grey (unsigned char *image, int imgWidth, int imgHeight) {
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];
......@@ -70,49 +17,20 @@ unsigned char* rgb2grey (unsigned char *image, int isInput) {
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;
}
void CPU_saveGreyPicture (unsigned char *image, char* filename, int imgWidth, int imgHeight) {
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);
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* CPU_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) );
for ( int i = 0; i < searchImgWidth; i++ ){
ret[y*inputImgWidth*3 + (x+i)*3] = 255;
ret[y*inputImgWidth*3 + (x+i)*3 + 1] = 0;
......@@ -134,38 +52,38 @@ unsigned char* generateRedBox(int x, int y, unsigned char *image){
return ret;
}
unsigned char* calculateDiff(unsigned char* input, unsigned char* search) {
unsigned char* CPU_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));
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("%.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 )
//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);
......@@ -173,7 +91,7 @@ unsigned char* calculateDiff(unsigned char* input, unsigned char* search) {
return diff;
}
int* findBestCompability (unsigned char* diff){
int* CPU_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* 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);
unsigned char* CPU_calculateDiff(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
int* CPU_findBestCompability (unsigned char* diff, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
\ No newline at end of file
......@@ -3,19 +3,6 @@
#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;
......@@ -24,26 +11,7 @@ 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;
......@@ -61,7 +29,7 @@ void saveGreyPicture (unsigned char *image, char* filename, int isInput) {
imageFinal[index*3 + 2] = image[index];
}
savePicture(imageFinal, filename, isInput);
}
}*/
unsigned char* generateRedBox(int x, int y, unsigned char *image){
int imgHeight = inputImgHeight;
......@@ -95,7 +63,7 @@ unsigned char* calculateDiff(unsigned char* input, unsigned char* search) {
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("%.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++ ) {
......
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);
......
#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 methode_one = 1;
unsigned char* load_img(const char * imgPath, int* imgWidth, int* imgHeight) {
int dummyNbChannels; // number of channels forced to 3 in stb_load.
unsigned char *img = stbi_load(imgPath, imgWidth, imgHeight, &dummyNbChannels, 3);
if (img == NULL)
{
printf("Cannot load image %s", imgPath);
//return EXIT_FAILURE;
}
printf("Loaded : image %s: %dx%d\n", imgPath, *imgWidth, *imgHeight);
return img;
}
void free_img(unsigned char* img) {
stbi_image_free(img);
}
void savePicture (unsigned char *image, char* filename, int imgWidth, int imgHeight) {
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);
}
unsigned char* load_img(const char * imgPath, int* imgWidth, int* imgHeight);
void free_img(unsigned char*);
void savePicture (unsigned char *image, char* filename, int imgWidth, int imgHeight);
\ 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
......@@ -3,12 +3,21 @@
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include "func_CPU.h"
#include "func_OPEN.h"
#include "functions/func_all.h"
#include "functions/func_CPU.h"
//#include "functions/func_OPEN.h"
static const int IS_INPUT = 1;
static const int IS_SEARCH = 0;
int inputImgWidth = -1;
int inputImgHeight = -1;
int searchImgWidth = -1;
int searchImgHeight = -1;
int main (int argc, char *argv[])
{
double temps_initial = omp_get_wtime();
......@@ -20,15 +29,15 @@ int main (int argc, char *argv[])
}
// Get image paths from arguments.
const char *inputImgPath = argv[1];
const char *inputImgPath = argv[1];
const char *searchImgPath = argv[2];
// ==================================== Loading input image.
double init_input = omp_get_wtime();
unsigned char *inputImg = load_img(inputImgPath, IS_INPUT);
unsigned char *inputImg = load_img(inputImgPath, &inputImgWidth, &inputImgHeight);
printf("Done in \033[0;31m %f \033[0m seconds\n", omp_get_wtime() - init_input);
double init_search = omp_get_wtime();
unsigned char *searchImg = load_img(searchImgPath, IS_SEARCH);
unsigned char *searchImg = load_img(searchImgPath, &searchImgWidth, &searchImgHeight);
printf("Done in \033[0;31m %f \033[0m seconds\n", omp_get_wtime() - init_search);
......@@ -43,7 +52,7 @@ int main (int argc, char *argv[])
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
double input_to_grey = omp_get_wtime();
unsigned char *inputGrey = rgb2grey(inputImg, IS_INPUT);
unsigned char *inputGrey = CPU_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);
......@@ -51,39 +60,38 @@ int main (int argc, char *argv[])
// ==================================== Convert search image to grey scale
printf("Convert search to grey\n");
double search_to_grey = omp_get_wtime();
unsigned char *searchGrey = rgb2grey(searchImg, IS_SEARCH);
unsigned char *searchGrey = CPU_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
unsigned char *diff = calculateDiff(inputGrey,searchGrey);
unsigned char *diff = CPU_calculateDiff(inputGrey,searchGrey, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
//saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, (char*)"debug_m1");
// ==================================== Search max compability
double find_compa = omp_get_wtime();
int* coord = findBestCompability(diff);
int* coord = CPU_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");
double box = omp_get_wtime();
unsigned char *final_m1 = generateRedBox(coord[0], coord[1], inputImg);
unsigned char *final_m1 = CPU_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 CPU took \033[0;31m %f \033[0m seconds to execute\n", omp_get_wtime() - temps_initial);
/*
// ============================================================================================================
printf("\n============================================\n");
......@@ -140,7 +148,7 @@ int main (int argc, char *argv[])
*/
free_img(inputImg);
free_img(searchImg);
printf("The program took \033[0;31m %f \033[0m seconds to execute\n", omp_get_wtime() - temps_initial);
......
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
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,12 +12,12 @@ chmod 755 ./tp_projet
: '
Image espace
'
./tp_projet ./img/space.png ./img/goat.png
#./tp_projet ./img/space.png ./img/goat.png
: '
Image jungle
'
#./tp_projet ./img/jungle.png ./img/goat.png
./tp_projet ./img/jungle.png ./img/goat.png
: '
Image plage
......
File added
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