Commit 81c472fc authored by Charron Maxime's avatar Charron Maxime
Browse files

Init the repo

parent b69bf1bc
inputImgWidth
inputImgHeight
inputImgPath
inputImgGrey
searchImgWidth
searchImgHeight
searchImgPath
searchImgGrey
float D = 1;
int coordx = 0;
int coordy = 0;
while (D != 0) {
// parcours de l'inputImg
for (int x = 0; x < ((inputImgWidth - searchImgWidth) / 3); x++) {
for (int y = 0; y < ((inputImgHeight - searchImgHeight) / 3); y++) {
// parcours de l'inputImg de la taille de la searchImg
for (int i = x; i < ((x + searchImgWidth) / 3); i++) {
for (int j = y; j < ((y + searchImgHeight) / 3; j++) {
D = (inputImgGrey[width * y + x] - searchImgGrey[width * y + x]) * (inputImgGrey[width * y + x] - searchImgGrey[width * y + x])
return (x, y)
}
}
}
}
}
///recuperer les coocrdonnees x et y pour peindre en rouge le contours ??
for (int i = x; i < ((x + searchImgWidth)); i++) {
mettre en rouge(i, y);
mettre en rouge(i, y + searchImgHeight);
}
for (int j = y; j < ((y + searchImgHeight); j++) {
mettre en rouge(x, j);
mettre en rouge(x + searchImgWidth, j);
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#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* 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;
}
int main (int argc, char *argv[])
{
printf("CPU\n");
double temps_initial = omp_get_wtime();
if (argc != 3)
{
printf("Invalid arguments !\n");
return EXIT_FAILURE;
}
// Get image paths from arguments.
const char *inputImgPath = argv[1];
const char *searchImgPath = argv[2];
// ==================================== Loading input image.
int dummyNbChannels; // number of channels forced to 3 in stb_load.
unsigned char *inputImg = stbi_load(inputImgPath, &inputImgWidth, &inputImgHeight, &dummyNbChannels, 3);
if (inputImg == NULL)
{
printf("Cannot load image %s", inputImgPath);
return EXIT_FAILURE;
}
printf("Input image %s: %dx%d\n", inputImgPath, inputImgWidth, inputImgHeight);
// ==================================== Loading search image.
unsigned char *searchImg = stbi_load(searchImgPath, &searchImgWidth, &searchImgHeight, &dummyNbChannels, 3);
if (searchImg == NULL)
{
printf("Cannot load image %s", searchImgPath);
return EXIT_FAILURE;
}
printf("Search image %s: %dx%d\n", searchImgPath, searchImgWidth, searchImgHeight);
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
unsigned char *inputGrey = rgb2grey(inputImgHeight, inputImgWidth, inputImg);
//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);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "search");
printf("Done\n");
// ============================================================================================================
// METHODE 1
// ============================================================================================================
// ==================================== 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");
// ==================================== 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);
printf("======================================================\n");
// ============================================================================================================
// METHODE 2
// ============================================================================================================
/*
// ==================================== 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");
// ==================================== Search max compability
coord = findBestCompability(diff);
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("======================================================\n");
*/
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");
return EXIT_SUCCESS;
}
#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* 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));
# pragma omp parallel for schedule(static)
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;
}
int main (int argc, char *argv[])
{
printf("Version PARRA\n");
double temps_initial = omp_get_wtime();
;
if (argc != 3)
{
printf("Invalid arguments !\n");
return EXIT_FAILURE;
}
// Get image paths from arguments.
const char *inputImgPath = argv[1];
const char *searchImgPath = argv[2];
// ==================================== Loading input image.
int dummyNbChannels; // number of channels forced to 3 in stb_load.
unsigned char *inputImg = stbi_load(inputImgPath, &inputImgWidth, &inputImgHeight, &dummyNbChannels, 3);
if (inputImg == NULL)
{
printf("Cannot load image %s", inputImgPath);
return EXIT_FAILURE;
}
printf("Input image %s: %dx%d\n", inputImgPath, inputImgWidth, inputImgHeight);
// ==================================== Loading search image.
unsigned char *searchImg = stbi_load(searchImgPath, &searchImgWidth, &searchImgHeight, &dummyNbChannels, 3);
if (searchImg == NULL)
{
printf("Cannot load image %s", searchImgPath);
return EXIT_FAILURE;
}
printf("Search image %s: %dx%d\n", searchImgPath, searchImgWidth, searchImgHeight);
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
unsigned char *inputGrey = rgb2grey(inputImgHeight, inputImgWidth, inputImg);
//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);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "search");
printf("Done\n");
// ============================================================================================================
// METHODE 1
// ============================================================================================================
// ==================================== 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");
// ==================================== 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);
printf("======================================================\n");
// ============================================================================================================
// METHODE 2
// ============================================================================================================
/*
// ==================================== 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");
// ==================================== Search max compability
coord = findBestCompability(diff);
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("======================================================\n");
*/
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");
return EXIT_SUCCESS;
}
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/*
Pour le calcul des diff :
ne pas prendre en compte les 0 rajoute moins de diff au pixels (diff plus homogène)
\ No newline at end of file
clear
echo "=============================================================================="
echo " COMPILATION"
echo "=============================================================================="
make tp_projet
echo ""
echo "=============================================================================="
echo " RUN"
echo "=============================================================================="
chmod 755 ./tp_projet
: '
Image espace
'
./tp_projet ./img/space.png ./img/goat.png
: '
Image jungle
'
#./tp_projet ./img/jungle.png ./img/goat.png
: '
Image plage
'
#./tp_projet ./img/beach.png ./img/goat.png
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