commit 80a0b2dd97c655d8c5135d25e328aef4edae1feb Author: Phyks Date: Sun Jul 28 15:24:49 2013 +0200 Initial commit diff --git a/29.bmp b/29.bmp new file mode 100644 index 0000000..12c628e Binary files /dev/null and b/29.bmp differ diff --git a/29.jpg b/29.jpg new file mode 100644 index 0000000..51dc367 Binary files /dev/null and b/29.jpg differ diff --git a/29_grey.jpg b/29_grey.jpg new file mode 100644 index 0000000..f960a2d Binary files /dev/null and b/29_grey.jpg differ diff --git a/30.jpg b/30.jpg new file mode 100644 index 0000000..b501436 Binary files /dev/null and b/30.jpg differ diff --git a/30_small.jpg b/30_small.jpg new file mode 100644 index 0000000..8b3d39e Binary files /dev/null and b/30_small.jpg differ diff --git a/31.jpg b/31.jpg new file mode 100644 index 0000000..fc02360 Binary files /dev/null and b/31.jpg differ diff --git a/Algorithm.pdf b/Algorithm.pdf new file mode 100644 index 0000000..c123c5f Binary files /dev/null and b/Algorithm.pdf differ diff --git a/IMG_0114b.JPG b/IMG_0114b.JPG new file mode 100644 index 0000000..e956a08 Binary files /dev/null and b/IMG_0114b.JPG differ diff --git a/IMG_0114s.JPG b/IMG_0114s.JPG new file mode 100644 index 0000000..c9cba94 Binary files /dev/null and b/IMG_0114s.JPG differ diff --git a/IMG_0114s.bmp b/IMG_0114s.bmp new file mode 100644 index 0000000..fc89c12 Binary files /dev/null and b/IMG_0114s.bmp differ diff --git a/IMG_0115b.JPG b/IMG_0115b.JPG new file mode 100644 index 0000000..166ce14 Binary files /dev/null and b/IMG_0115b.JPG differ diff --git a/IMG_0115s.JPG b/IMG_0115s.JPG new file mode 100644 index 0000000..24fe151 Binary files /dev/null and b/IMG_0115s.JPG differ diff --git a/Left.JPG b/Left.JPG new file mode 100644 index 0000000..7fff7dc Binary files /dev/null and b/Left.JPG differ diff --git a/Left2.JPG b/Left2.JPG new file mode 100644 index 0000000..ce80bcf Binary files /dev/null and b/Left2.JPG differ diff --git a/Notes valeurs de D, f et deltaX b/Notes valeurs de D, f et deltaX new file mode 100644 index 0000000..f781db7 --- /dev/null +++ b/Notes valeurs de D, f et deltaX @@ -0,0 +1,3 @@ +D = 36mm +f = 38mm +deltaX = 8cm diff --git a/RGB2grey.c b/RGB2grey.c new file mode 100644 index 0000000..5ee816e --- /dev/null +++ b/RGB2grey.c @@ -0,0 +1,30 @@ +unsigned char** RGB2grey(unsigned char*** image, int width, int height) +{ + unsigned char** grey; + int x,y; + + if((grey = malloc(sizeof(*grey) * height)) == NULL) + { + perror("malloc:"); + return NULL; + } + for(y = 0; y <= height; y++) + { + if((grey[y] = malloc(sizeof(**grey) * width)) == NULL) + { + perror("malloc:"); + return NULL; + } + } + + for(y = 0; y <= height; y++) + { + for(x = 0; x <= width; x++) + { + grey[y][x] = (unsigned char) (0.2125*image[y][x][0] + 0.7154*image[y][x][1] + 0.0721*image[y][x][2]); + //Formula found here : https://fr.wikipedia.org/wiki/Niveau_de_gris + } + } + + return grey; +} diff --git a/Rapport/IMG_0114s.JPG b/Rapport/IMG_0114s.JPG new file mode 100644 index 0000000..c9cba94 Binary files /dev/null and b/Rapport/IMG_0114s.JPG differ diff --git a/Rapport/IMG_0115s.JPG b/Rapport/IMG_0115s.JPG new file mode 100644 index 0000000..24fe151 Binary files /dev/null and b/Rapport/IMG_0115s.JPG differ diff --git a/Rapport/Left.JPG b/Rapport/Left.JPG new file mode 100644 index 0000000..7fff7dc Binary files /dev/null and b/Rapport/Left.JPG differ diff --git a/Rapport/Right.JPG b/Rapport/Right.JPG new file mode 100644 index 0000000..5d56da9 Binary files /dev/null and b/Rapport/Right.JPG differ diff --git a/Rapport/biblio.bib b/Rapport/biblio.bib new file mode 100644 index 0000000..2c810d2 --- /dev/null +++ b/Rapport/biblio.bib @@ -0,0 +1,27 @@ +@misc{article, +title = {Distance Estimation Algorithm for Stereo Pair Images}, +author = {TjandranegaraB, Edwin}, +howpublished="\url{http://docs.lib.purdue.edu/ecetr/64/}", +year = {article daté de 2005, dernière consultation en décembre 2012}, +} + +@misc{article2, +title = {Distance measuring based on stereoscopic pictures}, +author = {Mrovlje1, Jernej and Vranči, Damir}, +howpublished="\url{http://photon07.pd.infn.it:5210/users/dazzi/Thesis_doctorate/Info/Chapter_6/Stereoscopy_(Mrovlje).pdf}", +year = {article daté de 2008, dernière consultation en décembre 2012}, +} + +@misc{templateMatching, +title = {OpenCV wiki page about template matching}, +author = {}, +howpublished="\url{http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html}", +year = {dernière consultation en janvier 2013}, +} + +@misc{phaseCorrelation, +title = {Wikipedia page about phase correlation}, +author = {}, +howpublished="\url{http://en.wikipedia.org/wiki/Phase_correlation}", +year = {dernière consultation en janvier 2013}, +} \ No newline at end of file diff --git a/Rapport/rapport.aux b/Rapport/rapport.aux new file mode 100644 index 0000000..84ba627 --- /dev/null +++ b/Rapport/rapport.aux @@ -0,0 +1,42 @@ +\relax +\providecommand\hyper@newdestlabel[2]{} +\catcode`:\active +\catcode`;\active +\catcode`!\active +\catcode`?\active +\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument} +\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined +\global\let\oldcontentsline\contentsline +\gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}} +\global\let\oldnewlabel\newlabel +\gdef\newlabel#1#2{\newlabelxx{#1}#2} +\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}} +\AtEndDocument{\ifx\hyper@anchor\@undefined +\let\contentsline\oldcontentsline +\let\newlabel\oldnewlabel +\fi} +\fi} +\global\let\hyper@last\relax +\gdef\HyperFirstAtBeginDocument#1{#1} +\providecommand\HyField@AuxAddToFields[1]{} +\providecommand\HyField@AuxAddToCoFields[2]{} +\select@language{french} +\@writefile{toc}{\select@language{french}} +\@writefile{lof}{\select@language{french}} +\@writefile{lot}{\select@language{french}} +\@writefile{toc}{\contentsline {section}{\numberline {1}Objectif}{1}{section.1}} +\@writefile{toc}{\contentsline {section}{\numberline {2}M\IeC {\'e}thode choisie}{1}{section.2}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.1}Distance euclidienne dans l'espace RGB}{1}{subsection.2.1}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.2}Cross-correlation \emph {via} FFT}{1}{subsection.2.2}} +\citation{article} +\citation{article2} +\citation{templateMatching} +\citation{phaseCorrelation} +\bibstyle{plain-fr} +\bibdata{biblio} +\bibcite{templateMatching}{1} +\@writefile{toc}{\contentsline {section}{\numberline {3}R\IeC {\'e}sultats}{2}{section.3}} +\@writefile{toc}{\contentsline {section}{\numberline {4}Optimisations possibles}{2}{section.4}} +\bibcite{phaseCorrelation}{2} +\bibcite{article2}{3} +\bibcite{article}{4} diff --git a/Rapport/rapport.bbl b/Rapport/rapport.bbl new file mode 100644 index 0000000..970fcef --- /dev/null +++ b/Rapport/rapport.bbl @@ -0,0 +1,30 @@ +\begin{thebibliography}{1} +\expandafter\ifx\csname fonteauteurs\endcsname\relax +\def\fonteauteurs{\scshape}\fi + +\bibitem{templateMatching} +Opencv wiki page about template matching. +\newblock + \url{http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html}, + dernière consultation en janvier 2013. + +\bibitem{phaseCorrelation} +Wikipedia page about phase correlation. +\newblock \url{http://en.wikipedia.org/wiki/Phase_correlation}, dernière + consultation en janvier 2013. + +\bibitem{article2} +Jernej \bgroup\fonteauteurs\bgroup Mrovlje1\egroup\egroup{} et Damir + \bgroup\fonteauteurs\bgroup Vranči\egroup\egroup{} : +\newblock Distance measuring based on stereoscopic pictures. +\newblock + \url{http://photon07.pd.infn.it:5210/users/dazzi/Thesis_doctorate/Info/Chapter_6/Stereoscopy_(Mrovlje).pdf}, + article daté de 2008, dernière consultation en décembre 2012. + +\bibitem{article} +Edwin \bgroup\fonteauteurs\bgroup TjandranegaraB\egroup\egroup{} : +\newblock Distance estimation algorithm for stereo pair images. +\newblock \url{http://docs.lib.purdue.edu/ecetr/64/}, article daté de 2005, + dernière consultation en décembre 2012. + +\end{thebibliography} diff --git a/Rapport/rapport.blg b/Rapport/rapport.blg new file mode 100644 index 0000000..ae0814d --- /dev/null +++ b/Rapport/rapport.blg @@ -0,0 +1,49 @@ +This is BibTeX, Version 0.99d (TeX Live 2012/Arch Linux) +Capacity: max_strings=35307, hash_size=35307, hash_prime=30011 +The top-level auxiliary file: rapport.aux +The style file: plain-fr.bst +Database file #1: biblio.bib +Warning--to sort, need author or key in templateMatching +Warning--to sort, need author or key in phaseCorrelation +You've used 4 entries, + 2238 wiz_defined-function locations, + 554 strings with 5349 characters, +and the built_in function-call counts, 936 in all, are: += -- 99 +> -- 21 +< -- 0 ++ -- 10 +- -- 6 +* -- 34 +:= -- 174 +add.period$ -- 8 +call.type$ -- 4 +change.case$ -- 15 +chr.to.int$ -- 0 +cite$ -- 6 +duplicate$ -- 28 +empty$ -- 93 +format.name$ -- 6 +if$ -- 213 +int.to.chr$ -- 0 +int.to.str$ -- 4 +missing$ -- 0 +newline$ -- 23 +num.names$ -- 4 +pop$ -- 33 +preamble$ -- 1 +purify$ -- 11 +quote$ -- 0 +skip$ -- 28 +stack$ -- 0 +substring$ -- 44 +swap$ -- 4 +text.length$ -- 0 +text.prefix$ -- 0 +top$ -- 0 +type$ -- 16 +warning$ -- 2 +while$ -- 4 +width$ -- 5 +write$ -- 40 +(There were 2 warnings) diff --git a/Rapport/rapport.out b/Rapport/rapport.out new file mode 100644 index 0000000..7a092dc --- /dev/null +++ b/Rapport/rapport.out @@ -0,0 +1,6 @@ +\BOOKMARK [1][-]{section.1}{Objectif}{}% 1 +\BOOKMARK [1][-]{section.2}{M\351thode choisie}{}% 2 +\BOOKMARK [2][-]{subsection.2.1}{Distance euclidienne dans l'espace RGB}{section.2}% 3 +\BOOKMARK [2][-]{subsection.2.2}{Cross-correlation via FFT}{section.2}% 4 +\BOOKMARK [1][-]{section.3}{R\351sultats}{}% 5 +\BOOKMARK [1][-]{section.4}{Optimisations possibles}{}% 6 diff --git a/Rapport/rapport.pdf b/Rapport/rapport.pdf new file mode 100644 index 0000000..6bc08b7 Binary files /dev/null and b/Rapport/rapport.pdf differ diff --git a/Rapport/rapport.tex b/Rapport/rapport.tex new file mode 100644 index 0000000..4e00af9 --- /dev/null +++ b/Rapport/rapport.tex @@ -0,0 +1,101 @@ +\documentclass[10pt,a4paper]{article} +\usepackage{ifpdf} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage[francais]{babel} +\usepackage{lmodern} +\usepackage{graphicx} +\usepackage{amsmath} +\usepackage{amsfonts} +\usepackage{amssymb} +\usepackage{enumitem} +\usepackage[margin=1.5cm]{geometry} +\usepackage{hyperref} +\usepackage{url} + +\title{Projet de Physique Numérique : Mesure de distances par stéréoscopie} +\author{Lucas Verney} +\date{2012/2013} +\ifpdf +\pdfinfo { + /Author (Lucas Verney) + /Title (Projet de Physique Numérique : Mesure de distances par stéréoscopie) +} +\fi +\begin{document} + \maketitle + + \section{Objectif} + Le but de ce projet était de déterminer des distances séparant un observateur d'un objet à partir de deux photos de cet objet prises en déplaçant horizontalement l'appareil entre les prises de vue. En utilisant la stéréoscopie, on peut en effet déterminer la distance nous séparant de l'objet, tout comme notre cerveau est capable d'estimer les distances nous séparant des objets à partir des ``images'' captées par nos yeux. + + \bigskip + + \underline{\emph{Exemple :}} + \medskip + + \begin{center} + \begin{tabular}{cc} + \includegraphics[scale=0.25]{IMG_0114s.JPG} & \includegraphics[scale=0.25]{IMG_0115s.JPG} \\ + \emph{Image de gauche} & \emph{Image de droite} \\ + \end{tabular} + \end{center} + + \section{Méthode choisie} + Il faut, dans un premier temps, déterminer le déplacement en pixels de l'objet entre les deux images. Dans un deuxième temps, il faut traduire ce déplacement en distance réelle nous séparant de l'objet. + + \subsection{Distance euclidienne dans l'espace RGB} + Pour déterminer le déplacement en pixels, on considère une grille de $n\times n$ pixels, qu'on superpose à notre image de gauche (par défaut, $n = 50$ dans le programme). Pour chaque carrés de $n\times n$ pixels, on cherche le carré qui lui correspond le plus dans l'image de droite. Pour ce faire, on va parcourir chaque pixel de l'image de droite et comparer le carré de l'image de gauche et le carré dont le coin supérieur gauche est le pixel courant dans l'image de droite. + + Les images étant chargées en couleur (RGB), on est dans un espace à trois dimensions (R, G et B) dont les coordonnées des pixels sont entre $0$ et $255$ (profondeur de couleur de 8 bits). On peut donc estimer la ``distance'' séparant les deux carrés par la moyenne sur le carré des distances pixel à pixel où la distance pixel à pixel est la distance euclidienne standard ($=\sqrt{(R_2 - R_1)^2 + (G_2 - G_1)^2 + (B_2 - B_1)^2}$). + + \subsection{Cross-correlation \emph{via} FFT} + Une autre possibilité est d'utiliser la cross-correlation (ou la phase correlation) afin de déterminer la position de notre objet. + + On commence par charger les images en niveaux de gris et on récupère le carré qu'on considère dans une nouvelle image, de la même taille que l'image de droite. On complète cette image par du noir (des 0) qui n'interviendront donc pas dans la transformation de Fourier. + + On effectue ensuite les transformations de fourier de chaque image et on multiplie le spectre de l'image de droite par le conjugué du spectre du carré dont on cherche la position. On normalise ensuite la transformation de Fourier (en divisant chaque la valeur de chaque point par son module) et on repasse en espace réel (\emph{via} une transformation de Fourier inverse. + + On cherche ensuite la valeur de la plus grande amplitude sur cette image, qui correspond à la position du carré qu'on cherche. + + \bigskip + + Une fois cette distance déterminée, on peut en déduire la distance nous séparant de l'objet en utilisant les formules explicitées dans \cite{article}. Connaissant les caractéristiques de l'appareil, il est facile de traduire le déplacement en pixels précédent en distance réelle. + + \section{Résultats} + Le programme obtenu a été testée sur le jeu d'images suivant (les tests ont été effectués en considérant le coin inférieur gauche du carré noir) : + + \begin{center} + \begin{tabular}{cc} + \includegraphics[scale=0.05]{Left.JPG} & \includegraphics[scale=0.05]{Right.JPG} \\ + \emph{Image de gauche} & \emph{Image de droite} \\ + \end{tabular} + \end{center} + + Le programme décrit précédemment permet de déterminer très précisément le déplacement du coin inférieur gauche et on ne peut obtenir une meilleure précision manuellement. Ce résultat a été obtenu en une vingtaine de secondes en utilisant les $4$ c{\oe}urs du processeur, ce qui est relativement long. En effet, pour parcourir toute l'image avec la méthode précédente et des carrés de $50$ pixels de large, il faudrait environ $30h$. + + Après conversion du déplacement en pixels en distance réelle, on trouve une distance réelle de $15cm$ contre $35cm$ en réalité (en utilisant des caractéristiques ``moyennes'' pour l'appareil photo). Cependant, le déplacement étant convenablement détecté, cet écart provient d'une méconnaissance des caractéristiques précises de l'appareil photo utilisé (notamment sa distance focale). + + En conclusion, cette technique nous permet de déterminer des distances très précisément mais est très coûteuse en ressources et il est difficilement envisageable de couvrir une photo entière avec cette méthode. + + La technique utilisant la transformée de fourier rapide est nettement plus rapide (une vingtaine de secondes par image) et donne des résultats légèrement moins précis mais largement comparables. + + \medskip + \emph{Remarque :} Il faut également noter qu'on ne détermine pas la distance nous séparant d'un objet (ou d'un point) précis mais une moyenne de cette distance sur un carré de $n$ pixels de large. Il faut donc choisir la taille de ce carré petite devant l'échelle caractéristique de variation des distances (liée à la taille transversale des objets). + + \emph{Remarque :} On peut également utiliser un algorithme de détection de contours (type filtre de Sobel qui calcule le ``gradient'' de l'image) pour travailler sur une image en niveau de gris et déterminer les distances nous séparant des contours des objets, ceci afin d'obtenir une meilleure précision sur des images chargées. + + \section{Optimisations possibles} + Comme on l'a vu précédemment, la méthode employée est très coûteuse en ressources. On peut envisager diverses optimisations : + \begin{itemize} + \item On peut envisager d'appliquer un filtre de détection de contours (type filtre de Sobel) avant de travailler sur l'image. On peut alors se restreindre aux zones d'intérêt, c'est-à-dire aux contours représentés par des pixels blancs. + \item Pour accélérer le calcul des distances, on peut envisager de paralléliser au maximum les opérations. Dans le code présenté, on utilise jusqu'à $4$ c{\oe}urs du processeur mais l'utilisation de la puce graphique pourrait accélérer le calcul, celle-ci travaillant naturellement avec des calculs parallèles. + \item Jusqu'ici, on n'a envisagé qu'un déplacement horizontal (ou vertical en travaillant sur une image retournée). On pourrait envisager le cas d'un déplacement quelconque dans l'espace (translation) en adaptant les formules. + \end{itemize} + + \nocite{article2} + \nocite{templateMatching} + \nocite{phaseCorrelation} + + \bibliographystyle{plain-fr} + \bibliography{biblio} +\end{document} \ No newline at end of file diff --git a/Right.JPG b/Right.JPG new file mode 100644 index 0000000..5d56da9 Binary files /dev/null and b/Right.JPG differ diff --git a/Right2.JPG b/Right2.JPG new file mode 100644 index 0000000..ebaadd8 Binary files /dev/null and b/Right2.JPG differ diff --git a/Stereoscopy_(Mrovlje).pdf b/Stereoscopy_(Mrovlje).pdf new file mode 100644 index 0000000..d1c051a Binary files /dev/null and b/Stereoscopy_(Mrovlje).pdf differ diff --git a/Test.gif b/Test.gif new file mode 100644 index 0000000..61042c8 Binary files /dev/null and b/Test.gif differ diff --git a/TestL.jpg b/TestL.jpg new file mode 100644 index 0000000..68782b6 Binary files /dev/null and b/TestL.jpg differ diff --git a/TestR.jpg b/TestR.jpg new file mode 100644 index 0000000..3dbd276 Binary files /dev/null and b/TestR.jpg differ diff --git a/bmp b/bmp new file mode 100755 index 0000000..a9aa1ce Binary files /dev/null and b/bmp differ diff --git a/bmp.c b/bmp.c new file mode 100644 index 0000000..26d9033 --- /dev/null +++ b/bmp.c @@ -0,0 +1,165 @@ +#include +#include +#include + +//Note : BMP files are written from left to right but from bottom to top ! + +struct file_header +{ + char signature[3]; + int size; + int reserved; + int offset; +}; + +struct img_header +{ + int headerSize; + int width; + int height; + int planes; + int depth; + int compress; + int imgSize; + int resX; + int resY; + int colors; + int importantColors; +}; + +struct file_header get_file_header(FILE* file) +{ + struct file_header fh={"", 0, 0, 0}; + + fread(&fh.signature, 2, 1, file); + fread(&fh.size, 4, 1, file); + fread(&fh.reserved, 4, 1, file); + fread(&fh.offset, 4, 1, file); + + return fh; +} + +struct img_header get_img_header(FILE* file) +{ + struct img_header ih={0,0,0,0,0,0,0,0,0,0,0}; + + fread(&ih.headerSize, 4, 1, file); + fread(&ih.width, 4, 1, file); + fread(&ih.height, 4, 1, file); + fread(&ih.planes, 2, 1, file); + fread(&ih.depth, 2, 1, file); + fread(&ih.compress, 4, 1, file); + fread(&ih.imgSize, 4, 1, file); + fread(&ih.resX, 4, 1, file); + fread(&ih.resY, 4, 1, file); + fread(&ih.colors, 4, 1, file); + fread(&ih.importantColors, 4, 1, file); + + return ih; +} + +int main (int argc, char* argv[]) +{ + if(argc < 2) + { + fprintf(stderr, "Error : you must specify a bmp image to open.\n"); + return EXIT_FAILURE; + } + + //All the variables we'll use + FILE *file; + const char* filename; + filename = argv[1]; + struct file_header fileHeader; + struct img_header imageHeader; + int x, y, yImage; + unsigned char ***image; + + file = fopen(filename, "rb"); //First, open the file in binary mode + + if(file == NULL) + { + fprintf(stderr, "Error : can't open the bmp image.\n"); + return EXIT_FAILURE; + } + + fileHeader = get_file_header(file); //Get the file header and check it is a bmp file + + if(strcmp(fileHeader.signature, "BM") != 0 && strcmp(fileHeader.signature, "BA") != 0 && strcmp(fileHeader.signature, "CI") != 0 && strcmp(fileHeader.signature, "CP") != 0 && strcmp(fileHeader.signature, "IC") != 0 && strcmp(fileHeader.signature, "PT") != 0) + { + fprintf(stderr, "Error : This file is not a valid BMP image.\n"); + return EXIT_FAILURE; + } + + imageHeader = get_img_header(file); //Get the image header + + if(imageHeader.compress != 0) + { + fprintf(stderr, "Error : The BMP file is compressed. This program can't open such files.\n"); + return EXIT_FAILURE; + } + + if(imageHeader.depth != 24) //If it is not a "true-color" RGB bmp file (ie 24 bits per pixel) + { + fprintf(stderr, "Error : This BMP is not a standard true-color BMP file. It may be a 256 colors BMP file for example.\n"); + return EXIT_FAILURE; + } + + //Allocation dynamique pour l'image + if((image = malloc(sizeof(*image) * imageHeader.height)) == NULL) + { + perror("malloc:"); + return EXIT_FAILURE; + } + for(y = 0; y <= imageHeader.height; y++) + { + if((image[y] = malloc(sizeof(**image) * imageHeader.width)) == NULL) + { + perror("malloc:"); + return EXIT_FAILURE; + } + } + + for(y = 0; y <= imageHeader.height; y++) + { + for(x = 0; x <= imageHeader.width; x++) + { + if((image[y][x] = malloc(sizeof(***image) * 3)) == NULL) //image[y][x][R,G,B] + { + perror("malloc:"); + return EXIT_FAILURE; + } + image[y][x][0] = 0; + image[y][x][1] = 0; + image[y][x][2] = 0; + } + } + + fseek(file, fileHeader.offset, SEEK_SET); //We don't get all the possible headers, so go to the start of the image informations + + for(y = 0; y <= imageHeader.height; y++) //Get all the values for all the pixels in the image + { + for(x = 0; x <= imageHeader.width; x++) + { + yImage = imageHeader.height - y; //Due to the fact that BMP file are written from bottom to top + fread(&image[yImage][x][2], 1, 1, file); + fread(&image[yImage][x][1], 1, 1, file); + fread(&image[yImage][x][0], 1, 1, file); + } + } + + + printf("Output is : (Coordinates of the pixel) : value of each channel"); + for(y = 0; y <= imageHeader.height; y++) + { + for(x = 0; x <= imageHeader.width; x++) + { + printf("(%d,%d) : R = %d,G = %d,B = %d\n", x, y, image[y][x][0], image[y][x][1], image[y][x][2]); + } + } + + free(image); + fclose(file); + + return EXIT_SUCCESS; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..147d9d1 --- /dev/null +++ b/makefile @@ -0,0 +1,10 @@ +all: stereo bmp + +stereo : stereo.o + gcc stereo.o -o stereo `pkg-config opencv --libs` -lm -lpthread -lfftw3 -lfftw3_threads + +stereo.o : stereo.c + gcc -c stereo.c -Wall `pkg-config opencv --cflags` -lm -lpthread -lfftw3 -lfftw3_threads + +bmp : bmp.c + gcc -o bmp bmp.c diff --git a/sobel.c b/sobel.c new file mode 100644 index 0000000..704219b --- /dev/null +++ b/sobel.c @@ -0,0 +1,75 @@ +unsigned char** Sobel(unsigned char*** image, int width, int height) +//cf. http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html#sobel-derivatives and https://fr.wikipedia.org/wiki/Algorithme_de_Sobel +{ + unsigned char** sobel; + int x,y,i; + unsigned char tempx, tempy; + + if((sobel = malloc(sizeof(*sobel) * height)) == NULL) + { + perror("malloc:"); + return NULL; + } + for(y = 0; y <= height; y++) + { + if((sobel[y] = malloc(sizeof(**sobel) * width)) == NULL) + { + perror("malloc:"); + return NULL; + } + for(x = 0; x <= width; x++) + { + sobel[y][x] = 0; + } + } + + for(y = 1; y < height; y++) + { + for(x = 1; x < width; x++) + { + tempx = -image[y-1][x-1]+image[y-1][x+1]-2*image[y][x-1]+2*image[y][x+1]-image[y+1][x-1]+image[y+1][x+1]; + tempy = -image[y-1][x-1]-2*image[y-1][x]-image[y-1][x+1]+image[y+1][x-1]+2*image[y+1][x]+image[y+1][x+1]; + sobel[y][x] = (int) floor(sqrt(tempx^2 + tempy^2)); + } + } + + return sobel; +} + +unsigned char*** add2images(unsigned char*** image1, unsigned char*** image2, int width, int height, int nChannels) +{ + unsigned char*** sum; + int x,y,i; + + if((sum = malloc(sizeof(*sum) * height)) == NULL) + { + perror("malloc:"); + return NULL; + } + for(y = 0; y <= height; y++) + { + if((sum[y] = malloc(sizeof(**sum) * width)) == NULL) + { + perror("malloc:"); + return NULL; + } + } + + for(y = 0; y <= height; y++) + { + for(x = 0; x <= width; x++) + { + if((sum[y][x] = malloc(sizeof(***sum) * nChannels)) == NULL) //sum[y][x][R,G,B] + { + perror("malloc:"); + return NULL; + } + for(i = 0; i < nChannels; i++) + { + sum[y][x] = (unsigned char) ((image1[y][x][i] + image2[y][x][i])/2); + } + } + } + + return sum; +} diff --git a/stereo b/stereo new file mode 100755 index 0000000..32ec34f Binary files /dev/null and b/stereo differ diff --git a/stereo.c b/stereo.c new file mode 100644 index 0000000..223ac1d --- /dev/null +++ b/stereo.c @@ -0,0 +1,1337 @@ +/* +-------------------------------------------------------------------------------------------------------------------- +------- STEREOSCOPY DISTANCE MEASUREMENT --------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- +This program has been written by Phyks (http://phyks.me - webmaster@phyks.me). It is licensed under the +zlib/libpng OSI licence. + +Please, send me an email if you use or modify this program, just to let me know if this program is useful to anybody or how did you improve it :) You can also send me an email to tell me how lame it is ! :) + +Licence : http://www.opensource.org/licenses/zlib-license.php + +-------------------------------------------------------------------------------------------------------------------- + +This program computes the mean distance between a stereoscopic camera and an object. +We assume both cameras have the same specs (which are the diameter of the camera's field stop and the focal length). + +The code is commented but for more details about the algorithm, please refer to the following article : +http://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1064&context=ecetr + +The distance estimation is just an application of the algorithm described in the previous article. To compute the +displacement vector between the two images, we study the distances between subsets of the images. This is just like if +we had a mask that we could place over the left image and then, we could search the best matching part of the right image. + +We are in a three dimensional space (RGB) so we just compute the euclidian distances between each corresponding pixels of +the subsets and then, we compute the mean distance between the two subsets. + +Another option is to use FFT transform and phase correlation algorithm. More information cn be found here : +http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html and http://en.wikipedia.org/wiki/Phase_correlation + +The sobel argument makes the program perform a Sobel edge detection first. + +Notes : +- We use the openCV library to easily load and go through the images. OpenCV is a C library for computer vision licensed +under a BSD License (http://opensource.org/licenses/bsd-license.php) +- We use FFTW library to perform FFT. FFTW is a C library to compute FFT licensed under the GNU General Public License (GPL, see http://www.fftw.org/doc/License-and-Copyright.html) +- We compute both X and Y displacements but we'll focus on X displacements to determine the mean distance of the object +as the cameras are supposed to be on the same horizontal plane (Y displacements = vertical displacements are negligible) +- X corresponds to the abscissa and Y to the ordinate of the pixel in the image. They both goes from 0 to ... + +Pixels are numeroted this way : +(0,0) (1,0) (2,0) ... (Width,0) +(1,0) (1,1) (2,1) ... (Width,1) + ... ... ... ... ... +(0,Height) ... (Width, Height) +-------------------------------------------------------------------------------------------------------------------- +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef M_PI + #define M_PI 3.14159265358979323846 +#endif + +// Structure to store the coordinates of the best matching subsets +struct minXY_struct +{ + float min; + int X; + int Y; +}; + +struct data_find_common +{ + const IplImage* img2; + const uchar* p1; + int widthStep1; + int X_1; + int Y_1; + int subset_size; + int X_start; + int Y_start; + int X_end; + int Y_end; + struct minXY_struct* temp; +}; + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Find common ------------------------------------------------------------------------------------------------ +-------------------------------------------------------------------------------------------------------------------- +This function takes 10 parameters : +- The image to search in +- The pointer to the pixel in the left-hand corner of the subset we are working on (and its human-readable +coordinates X_1 and Y_1) +- The size of the subsets we consider +- The coordinates of the part of the image to search in + +This function will search the best matching subset in the right image to the current subset in the left image. + +It will go through the image img2 (aka the right image) and for each pixel, will compute the mean RGB distance between : +- The subset we are working on in the left image +- A subset of the same size in img2 with the current pixel as the left-hand corner + +As the images have been opened in color mode, we are working in a three dimensional space (RGB) and we just compute the +euclidian distance pixel to pixel using the colors as components. Then, we compute the mean of this distance over the subset. + +This function returns a structure with the X and Y coordinates of the left-hand corner of the best matching subset. +-------------------------------------------------------------------------------------------------------------------- +*/ + +static void *find_common(void *data) +{ + struct data_find_common *data_args = data; + + //Variables to explore the right image + uchar *p2, *line2; + //And the subsets + int i = 0, j = 0, k = 0; + + //To store the distance between the two subsets (distance) and the distances pixel to pixel (distance_temp) + float distance = 0, distance_temp = 0; + //Human readables coordinates corresponding to p2 + int X_temp = data_args->X_start, Y_temp = data_args->Y_start; + + //The structure it will return. We start with a maximal distance in the left-hand corner of the image + struct minXY_struct minXY={255, 0, 0}; + + //We explore the image line by line from left to right + //Note : we only explore an area with a height equal to the quarter of th total height of the image because the vertical displacement is supposed to be close to 0 + for (line2 = (uchar*) data_args->img2->imageData + data_args->Y_start*data_args->img2->widthStep; + line2 <= (uchar*) data_args->img2->imageData + data_args->Y_end*data_args->img2->widthStep; + line2 += data_args->img2->widthStep) + { + for (p2 = line2 + data_args->X_start*data_args->img2->nChannels; p2 <= line2 + data_args->X_end*data_args->img2->nChannels; p2 += data_args->img2->nChannels) + { + distance = 0; + //For each pixels, we calculate the mean distance for the subset_size pixel box around it + for(i = 0; isubset_size; i++) //i = iterate through the pixels in a line + { + for(j = 0; jsubset_size; j++) //j = iterate through the lines + { + distance_temp = 0; + + for(k = 0; k < data_args->img2->nChannels; k++) //k = iterate through the channels + { + distance_temp += pow(*(p2 + i*data_args->img2->nChannels + k + j*data_args->img2->widthStep) - *(data_args->p1 + i*data_args->img2->nChannels + k + j*data_args->widthStep1),2); //Works because img1 and img2 have the same number of channels + } + distance += sqrt(distance_temp); + } + } + distance /= pow(data_args->subset_size,2); //Distance is the mean distance over the subset + + //And if it is better than the previous min, we store the new coordinates + //We prefer a smaller distance + // But at equal distance, we prefer the closer pixels and the minimal vertical displacement (as it is supposed to be 0 because cameras are on a same horizontal plane) + if(distance < minXY.min || (distance == minXY.min && (sqrt(pow(X_temp-data_args->X_1, 2) + pow(Y_temp-data_args->Y_1, 2)) < sqrt(pow(minXY.X-data_args->X_1, 2) + pow(minXY.Y-data_args->Y_1, 2)) || abs(Y_temp - data_args->Y_1) < abs(minXY.Y - data_args->Y_1)))) + { + minXY.X = X_temp; + minXY.Y = Y_temp; + minXY.min = distance; + } + + X_temp++; + } + X_temp = data_args->X_start; + Y_temp++; + } + + //Store the informations (coordinates and minimum) in a structure + *data_args->temp = minXY; + + return NULL; +} + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Compute mean distance -------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- + +This function takes 6 parameters : +- The abscissas of the pixels in the left and in the right image +- The width of the two images +- The distance between the cameras and the angle theta computed previously + +We compute the mean distance of the given object by the algorithm described in the article above and return it or +return -1 if there was an error. +-------------------------------------------------------------------------------------------------------------------- +*/ + +float compute_mean_distance(int X_1, int X_2, int widthL, int widthR, float theta, float deltaX) +{ + float alpha_1, alpha_2; + + //If the object is located between two cameras + if(X_1 > widthL / 2 && X_2 < widthR / 2) + { + alpha_1 = atan((X_1 - (widthL / 2))*tan(theta)/(widthL / 2)); + alpha_2 = atan(((widthR / 2) - X_2)*tan(theta)/(widthR / 2)); + + return tan(M_PI/2 - alpha_1)*tan(M_PI/2 - alpha_2)*deltaX / (tan(M_PI/2 - alpha_1) + tan(M_PI/2 - alpha_2)); + } + + //If the object is on the left side of both cameras + if(X_1 < widthL / 2 && X_2 < widthR / 2) + { + alpha_1 = atan(((widthL / 2) - X_1)*tan(theta)/(widthL / 2)); + alpha_2 = atan(((widthR / 2) - X_2)*tan(theta)/(widthR / 2)); + + return sin(M_PI/2 - alpha_1)*sin(M_PI/2 - alpha_2)*deltaX / sin(alpha_2 - alpha_1); + } + + //If the object is on the right side of both cameras + if(X_1 > widthL / 2 && X_2 > widthR / 2) + { + alpha_1 = atan((X_1 - (widthL / 2))*tan(theta)/(widthL / 2)); + alpha_2 = atan((X_2 - (widthR / 2))*tan(theta)/(widthR / 2)); + + return sin(M_PI/2 - alpha_1)*sin(M_PI/2 - alpha_2)*deltaX / sin(alpha_1 - alpha_2); + } + + //If the object is exactly in front of the left camera + if(X_1 == widthL / 2) + { + alpha_2 = atan(((widthR / 2) - X_2)*tan(theta)/(widthR / 2)); + + return tan(M_PI/2 - alpha_2)*deltaX; + } + + //If the object is exactly in front of the right camera + if(X_2 == widthR / 2) + { + alpha_1 = atan((X_1 - (widthL / 2))*tan(theta)/(widthL / 2)); + + return tan(M_PI/2 - alpha_1)*deltaX; + } + + return -1; +} + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Print help message ----------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- +*/ + +void print_help(FILE* stream) +{ + fprintf(stream, "\nThis program computes the mean distance between a stereoscopic camera and an object. We assume both cameras have the same specs (which are the diameter of the camera's field stop and the focal length).\n"); + fprintf(stream, "\nFor more details about the algorithm, please refer to the following article : http://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1064&context=ecetr\n"); + fprintf(stream, "\nThe distance estimation is just an application of the algorithm described in the previous article. To compute the displacement vector between the two images, we study the distances between subsets of the images. This is just like ifwe had a mask that we could place over the left image and then, we could search the best matching part of the right image.\n"); + fprintf(stream, "\nNotes : \n"); + fprintf(stream, "- We use the openCV library to easily load and go through the images. OpenCV is a C library for computer vision licensed under a BSD License (http://opensource.org/licenses/bsd-license.php)\n"); + fprintf(stream, "- We compute both X and Y displacements but we'll focus on X displacements to determine the mean distance of the object as the cameras are supposed to be on the same horizontal plane (Y displacements = vertical displacements are negligible)\n"); + fprintf(stream, "- X corresponds to the abscissa and Y to the ordinate of the pixel in the image. They both goes from 0 to ...\n"); + fprintf(stream, "\nPixels are numeroted this way :\n"); + fprintf(stream, "(0,0)\t(1,0)\t(2,0)\t...\t(Width,0)\n"); + fprintf(stream, "(1,0)\t(1,1)\t(2,1)\t...\t(Width,1)\n"); + fprintf(stream, " ...\t ...\t ...\t...\t ...\n"); + fprintf(stream, "(0,Height)\t ...\t\t(Width, Height)\n"); + fprintf(stream, "\n======================================\n"); + fprintf(stream, "\n Usage : \n"); + fprintf(stream, "\t -l (--left) \t Path to the left image\n"); + fprintf(stream, "\t -r (--right) \t Path to the right image\n"); + fprintf(stream, "\t -D (--diameter) (float) Diameter of the camera's field stop\n"); + fprintf(stream, "\t -f (--focal) \t (float) Focal length of the camera\n"); + fprintf(stream, "\t -d (--delta) \t (float) Distance between the two cameras\n"); + fprintf(stream, "\t -s (--size) \t [Optional] (int) Size of the subsets used, default is 50\n"); + fprintf(stream, "\t -x (--abscissa) [Optional] (int) Abscissa of the pixel to compute the mean distance\n"); + fprintf(stream, "\t -y (--ordinate) [Optional] (int) Ordinate of the pixel to compute the mean distance\n"); + fprintf(stream, "\t -c (--choose) \t [Optional] To display the image and select which pixel you want to work with (same as -x and -y but useful if you don't know the coordinates of the pixel)\n"); + fprintf(stream, "\t -o (--output) \t [Optional] Path to a file to store the output\n"); + fprintf(stream, "\t -S (--sobel) \t [Optional] Run a Sobel edges detection algorithm on both left and right images first to work only on edges. This option won't have any effect if working in single pixel mode.\n"); + fprintf(stream, "\t -F (--fft) \t [Optional] Use cross-correlation and fft based method (faster but a little bit less accurate).\n"); + fprintf(stream, "\t -H (--hamming) \t [Optional] Use an hamming window to improve FFT (but slightly slower).\n"); + fprintf(stream, "\t -h (--help) \t Display this error message\n"); +} + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Min ----------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- +Return the min of a and b +*/ + +int minimum(int a, int b) +{ + if(a < b) + return a; + else + return b; +} + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Max -------------------------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- +Return the max of a and b +*/ + +int maximum(int a, int b) +{ + if(a > b) + return a; + else + return b; +} + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Mouse Handler ---------------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- +Handle the left button click and get the coordinates of the current pixel +*/ + +void mouseHandler(int event, int x, int y, int flags, void* param) +{ + if(event == CV_EVENT_LBUTTONDOWN) //If left click + { + int** input = param; + + *input[0] = x; //Get the current coordinates + *input[1] = y; + cvDestroyAllWindows(); //And close the window + } +} + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Find_FFT --------------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- +Find a template (tpl) in an image (src) + +Note : template and src must be the same size +*/ + +struct Peak +{ + CvPoint pt; + double maxval; +}; + +/* + Old version using OpenCV +--------------------------------- +struct Peak Find_FFT(IplImage* src, IplImage* tpl) +{ + CvSize imgSize = cvSize(src->width, src->height); //Size of the src image + CvMat tmp; + + //src and tpl must be the same size + assert(src->width == tpl->width); + assert(src->height == tpl->height); + + // Allocate floating point frames used for DFT (real, imaginary and complex) + IplImage* realInput = cvCreateImage( imgSize, IPL_DEPTH_64F, 1 ); + IplImage* imaginaryInput = cvCreateImage( imgSize, IPL_DEPTH_64F, 1 ); + IplImage* complexInput = cvCreateImage( imgSize, IPL_DEPTH_64F, 2 ); + + //Find best size for DFT + int nDFTHeight= cvGetOptimalDFTSize( imgSize.height ); + int nDFTWidth= cvGetOptimalDFTSize( imgSize.width ); + CvSize dftSize = cvSize(nDFTWidth, nDFTHeight); + + //Images that will store DFT + CvMat* src_DFT = cvCreateMat( nDFTHeight, nDFTWidth, CV_64FC2 ); + CvMat* tpl_DFT = cvCreateMat( nDFTHeight, nDFTWidth, CV_64FC2 ); + + //Images used to compute modulus + IplImage* imageRe = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 ); + IplImage* imageIm = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 ); + IplImage* imageImMag = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 ); + IplImage* imageMag = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 ); + + // Processing of src + cvScale(src,realInput,1.0,0); //Convert it to CV_32F (float) + cvZero(imaginaryInput); + cvMerge(realInput,imaginaryInput,NULL,NULL,complexInput); + cvGetSubRect(src_DFT,&tmp,cvRect(0,0,src->width,src->height)); + cvCopy(complexInput,&tmp,NULL); + if (src_DFT->cols>src->width) + { + cvGetSubRect(src_DFT,&tmp,cvRect(src->width,0,src_DFT->cols-src->width,src->height)); + cvZero(&tmp); + } + cvDFT(src_DFT,src_DFT,CV_DXT_FORWARD,complexInput->height); //Process DFT + + // Processing of tpl + cvScale(tpl,realInput,1.0,0); + cvMerge(realInput,imaginaryInput,NULL,NULL,complexInput); + cvGetSubRect(tpl_DFT,&tmp,cvRect(0,0,tpl->width,tpl->height)); + cvCopy(complexInput,&tmp,NULL); + if (tpl_DFT->cols>tpl->width) + { + cvGetSubRect(tpl_DFT,&tmp,cvRect(tpl->width,0,tpl_DFT->cols-tpl->width,tpl->height)); + cvZero( &tmp ); + } + cvDFT(tpl_DFT,tpl_DFT,CV_DXT_FORWARD,complexInput->height); + + // Multiply spectrums of the scene and the model (use CV_DXT_MUL_CONJ to get correlation instead of convolution) + cvMulSpectrums(src_DFT,tpl_DFT,src_DFT,CV_DXT_MUL_CONJ); + + // Split Fourier in real and imaginary parts + cvSplit(src_DFT,imageRe,imageIm,0,0); + + // Compute the magnitude of the spectrum components: Mag = sqrt(Re^2 + Im^2) + cvPow( imageRe, imageMag, 2.0 ); + cvPow( imageIm, imageImMag, 2.0 ); + cvAdd( imageMag, imageImMag, imageMag, NULL ); + cvPow( imageMag, imageMag, 0.5 ); + + // Normalize correlation (Divide real and imaginary components by magnitude) + cvDiv(imageRe,imageMag,imageRe,1.0); + cvDiv(imageIm,imageMag,imageIm,1.0); + cvMerge(imageRe,imageIm,NULL,NULL,src_DFT); + + //Inverse dft + cvDFT( src_DFT, src_DFT, CV_DXT_INVERSE_SCALE, complexInput->height ); + cvSplit( src_DFT, imageRe, imageIm, 0, 0 ); + + //Find the peak (greatest magnitude) + double minval = 0.0; + double maxval = 0.0; + CvPoint minloc; + CvPoint maxloc; + cvMinMaxLoc(imageRe,&minval,&maxval,&minloc,&maxloc,NULL); + + int x=maxloc.x; // log range + //if (x>(imageRe->width/2)) + // x = x-imageRe->width; // positive or negative values + int y=maxloc.y; // angle + //if (y>(imageRe->height/2)) + // y = y-imageRe->height; // positive or negative values + + struct Peak pk; + pk.maxval= maxval; + pk.pt=cvPoint(x,y); + + cvReleaseImage(&realInput); + cvReleaseImage(&imaginaryInput); + cvReleaseImage(&complexInput); + cvReleaseImage(&imageRe); + cvReleaseImage(&imageIm); + cvReleaseImage(&imageImMag); + cvReleaseImage(&imageMag); + + cvReleaseMat(&src_DFT); + cvReleaseMat(&tpl_DFT); + + return pk; +} +-----------------------------------------*/ + +/* New version, using FFTW (multithreaded) +------------------------------------------ +*/ + +struct Peak Find_FFT(IplImage* src, IplImage* tpl, int hamming) +{ + int i, j, k = 0; + double tmp; //To store the modulus temporarily + + //src and tpl must be the same size + assert(src->width == tpl->width); + assert(src->height == tpl->height); + + // Get image properties + int width = src->width; + int height = src->height; + int step = src->widthStep; + int fft_size = width * height; + + fftw_init_threads(); //Initialize FFTW for multithreading with a max number of 2 threads (more is not efficient) + fftw_plan_with_nthreads(2); + + //Allocate arrays for FFT of src and tpl + fftw_complex *src_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); + fftw_complex *src_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); + + fftw_complex *tpl_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); + fftw_complex *tpl_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); + + fftw_complex *res_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); //Result = Cross correlation + fftw_complex *res_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); + + // Setup pointers to images + uchar *src_data = (uchar*) src->imageData; + uchar *tpl_data = (uchar*) tpl->imageData; + + // Fill the structure that will be used by fftw + for(i = 0; i < height; i++) + { + for(j = 0 ; j < width ; j++, k++) + { + src_spatial[k][0] = (double) src_data[i * step + j]; + src_spatial[k][1] = 0.0; + + tpl_spatial[k][0] = (double) tpl_data[i * step + j]; + tpl_spatial[k][1] = 0.0; + } + } + + // Hamming window to improve FFT (but slightly slower to compute) + if(hamming == 1) + { + double omega = 2.0*M_PI/(fft_size-1); + double A= 0.54; + double B= 0.46; + for(i=0,k=0;iwidth,tpl->height ), IPL_DEPTH_64F, 1); + double *peak_find_data = (double*) peak_find->imageData; + + for( i = 0 ; i < fft_size ; i++ ) + { + peak_find_data[i] = res_spatial[i][0] / (double) fft_size; + } + + CvPoint minloc, maxloc; + double minval, maxval; + + cvMinMaxLoc(peak_find, &minval, &maxval, &minloc, &maxloc, 0); + + pk.pt = maxloc; + pk.maxval = maxval; + + // Clear memory + fftw_destroy_plan(plan_src); + fftw_destroy_plan(plan_tpl); + fftw_destroy_plan(plan_res); + fftw_free(src_spatial); + fftw_free(tpl_spatial); + fftw_free(src_freq); + fftw_free(tpl_freq); + fftw_free(res_spatial); + fftw_free(res_freq); + cvReleaseImage(&peak_find); + + fftw_cleanup_threads(); //Cleanup everything else related to FFTW + + return pk; +} + + +/* +-------------------------------------------------------------------------------------------------------------------- +------- Main function ---------------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------------------------- +*/ + +int main (int argc, char* argv[]) +{ + //--------------------------- + //Initialization of variables + //--------------------------- + + //To handle the options + const char* const short_options = "hl:r:D:f:d:s:x:y:o:cSFH"; + const struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "left", 1, NULL, 'l' }, + { "right", 1, NULL, 'r' }, + { "diameter", 1, NULL, 'D' }, + { "focal", 1, NULL, 'f' }, + { "delta", 1, NULL, 'd' }, + { "size", 1, NULL, 's' }, + { "abscissa", 1, NULL, 'x' }, + { "ordinate", 1, NULL, 'y' }, + { "output", 1, NULL, 'o' }, + { "choose", 0, NULL, 'c' }, + { "sobel", 0, NULL, 'S' }, + {"fft", 0, NULL, 'F'}, + {"hamming", 0, NULL, 'H'}, + { NULL, 0, NULL, 0 } + }; + int next_option = 0; + + //Threads + pthread_t thread1, thread2, thread3, thread4; + struct data_find_common data1, data2, data3, data4; + + //To store the images + IplImage* imgL = NULL; + IplImage* imgR = NULL; + uchar *p1, *line1; + + //Max X and Y for the images (to avoid computing it many times) + int X_maxL, Y_maxL, X_maxR, Y_maxR; + + //Path to images + const char* src_pathL = NULL; //Const ? + const char* src_pathR = NULL; + + //Matrices + int array_size = 0; //Size of the matrices we'll create + int **displacements; //To store the displacements for every subset + float *mean_distance; + + //To store the parameters + float D = 0, f = 0, deltaX = 0; + int X_input = -1, Y_input = -1, c = 0, sobel = 0, fft = 0, hamming = 0; + int CVLOAD = CV_LOAD_IMAGE_COLOR; + FILE *output_file = stdout; //By default, the output will be stored in stdout + const char* output = NULL; + //The size of the subset, 50 pixels by default. + int subset_size = 50; + + //theta = half angle of view of the cameras, alphas cf. article + float theta; + + //To iterate and compute the mean distances + int X_1, Y_1, X_2; + int j=0, key, char_temp; + float min; + CvMat tmp, tmp2; + //Temporary structures to store what find_common returns + struct minXY_struct temp1={255, 0, 0}; + struct minXY_struct temp2={255, 0, 0}; + struct minXY_struct temp3={255, 0, 0}; + struct minXY_struct temp4={255, 0, 0}; + + //Images for sobel + IplImage* imgL_gray = NULL; + IplImage* imgL_Sobelx = NULL; + IplImage* imgL_Sobely = NULL; + IplImage* imgL_Sobel = NULL; + IplImage* imgR_gray = NULL; + IplImage* imgR_Sobelx = NULL; + IplImage* imgR_Sobely = NULL; + IplImage* imgR_Sobel = NULL; + + //Image for FFT + IplImage* template = NULL; + + //Time + clock_t start=clock(); + + //---------------------------------------------------------- + //Store the options and the parameters in corresponding vars + //---------------------------------------------------------- + + do { + next_option = getopt_long(argc, argv, short_options, long_options, NULL); + + switch(next_option) + { + case 'h': + print_help(stdout); + return EXIT_SUCCESS; + + case 'l': + src_pathL = optarg; + break; + + case 'r': + src_pathR = optarg; + break; + + case 'D': + D = atof(optarg); + break; + + case 'f': + f = atof(optarg); + break; + + case 'd': + deltaX = atof(optarg); + break; + + case 's': + subset_size = atoi(optarg); + break; + + case 'x': + X_input = atoi(optarg); + break; + + case 'y': + Y_input = atoi(optarg); + break; + + case 'o': + output = optarg; + break; + + case 'c': + c = 1; + break; + + case 'S': + sobel = 1; + break; + + case 'F': + fft = 1; + CVLOAD = CV_LOAD_IMAGE_GRAYSCALE; + break; + + case 'H': + hamming = 1; + break; + + case -1: // End of arguments list + break; + + default: // Unexpected behavior + return EXIT_FAILURE; + } + + } while(next_option != -1); + + + //If not enough arguments -> error + help message + if(src_pathL == NULL || src_pathR == NULL || D == 0 || f == 0 || deltaX == 0) + { + fprintf(stderr, "Not enough arguments. You must provide the paths to the two images, the diameter, the focal length and the distance between the two cameras. Please refer to the help message below for more details.\n"); + fprintf(stderr, "======================================\n"); + fprintf(stderr, "Help :\n"); + print_help(stderr); + return EXIT_FAILURE; + } + + //If the output must be written in a file, open it + if(output != NULL) + { + output_file = fopen(output, "w"); + } + + //------------------------------------------------ + //Load the images and define the needed parameters + //------------------------------------------------ + + //We load the images in color mode even if they are greyscale to avoid problems with images with different color modes for the two images + if (!(imgL = cvLoadImage (src_pathL, CVLOAD))) + { + fprintf (stderr, "couldn't open image file: %s\n",src_pathL); + return EXIT_FAILURE; + } + + if (!(imgR = cvLoadImage (src_pathR, CVLOAD))) + { + fprintf (stderr, "couldn't open image file: %s\n", src_pathR); + return EXIT_FAILURE; + } + + //Check that the images have 3 channels and 8 bits depth (to store values in char) + if(CVLOAD == CV_LOAD_IMAGE_COLOR) + { + assert (imgL->depth == IPL_DEPTH_8U && imgL->nChannels == 3); + assert (imgR->depth == IPL_DEPTH_8U && imgR->nChannels == 3); + } + else + { + assert (imgL->depth == IPL_DEPTH_8U && imgL->nChannels == 1); + assert (imgR->depth == IPL_DEPTH_8U && imgR->nChannels == 1); + } + + fprintf(output_file, "Now working with %d*%d subset. \n", subset_size, subset_size); + + theta = atan(D/(2*f)); + + //Last coordinates we can study (due to the size of the subset) + X_maxL = imgL->width - subset_size; + Y_maxL = imgL->imageSize/imgL->widthStep - subset_size; + X_maxR = imgR->width - subset_size; + Y_maxR = imgR->imageSize/imgR->widthStep - subset_size; + + //------------------------------------------------------------------------------------------- + //Test that there's something (interesting) to do, ie that subset_size < imgSize / 2 and same for Y + //------------------------------------------------------------------------------------------- + if(floor(imgL->width/2) < subset_size || floor((imgL->imageSize / imgL->widthStep) / 2) < subset_size) + { + fprintf(stderr, "Error : subset is greater than the half of the image. Please choose a smaller subset size.\n"); + + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + + return EXIT_FAILURE; + } + + //--------------------------------------------------------- + //Get the coordinates of the pixels if argument "-c" passed + //--------------------------------------------------------- + + while(c != 0) + { + int *input[2] = {&X_input, &Y_input}; + + cvNamedWindow("Select the pixel to work with", 1); //Create a window, display the image and add the mouse handler + cvSetMouseCallback("Select the pixel to work with", mouseHandler, (void*) &input); + cvShowImage("Select the pixel to work with", imgL); + cvWaitKey(0); + + printf("Now working with the pixel : (%d,%d).\n", X_input, Y_input); + + printf("Is it ok ? [y/N] \n"); + + do + { + key = fgetc(stdin); + + while (char_temp != '\n' && char_temp != EOF) + { + char_temp = getchar(); + } + }while(key != 89 && key != 121 && key != 110 && key != 78 && key != 10); + + if(key == 89 || key == 121 || key == 10) + { + c = 0; + } + } + + //----------------------------------------------- + //Applicate an edge detection algorithm if needed + //----------------------------------------------- + + if(sobel != 0 && X_input < 0 && Y_input < 0) + { + //First, apply a gaussian blur + cvSmooth(imgL, imgL, CV_GAUSSIAN, 3, 3, 0, 0); + cvSmooth(imgR, imgR, CV_GAUSSIAN, 3, 3, 0, 0); + + //Convert images to greyscale + imgL_gray = cvCreateImage(cvGetSize(imgL),imgL->depth,1); + imgL_Sobelx = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1); + imgL_Sobely = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1); + imgL_Sobel = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1); + imgR_gray = cvCreateImage(cvGetSize(imgL),imgL->depth,1); + imgR_Sobelx = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1); + imgR_Sobely = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1); + imgR_Sobel = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1); + + cvCvtColor(imgL, imgL_gray, CV_RGB2GRAY); + cvCvtColor(imgR, imgR_gray, CV_RGB2GRAY); + + //Gradient X + cvSobel(imgL_gray, imgL_Sobelx, 1, 0, 3); + cvSobel(imgR_gray, imgR_Sobelx, 1, 0, 3); + + //Gradient Y + cvSobel(imgL_gray, imgL_Sobely, 0, 1, 3); + cvSobel(imgR_gray, imgR_Sobely, 0, 1, 3); + + //Add the two images + cvAdd(imgL_Sobelx, imgL_Sobely, imgL_Sobel, NULL); + cvAdd(imgR_Sobelx, imgR_Sobely, imgR_Sobel, NULL); + + //Show the images + printf("The images after edge detection will be shown in a new window.\n"); + printf("Press ENTER to continue"); + + while(getchar() != '\n'){} + cvStartWindowThread(); + + cvNamedWindow("Edge detection of the left image", CV_WINDOW_AUTOSIZE ); + cvShowImage("Edge detection of the left image", imgL_Sobel); + cvWaitKey(0); + cvDestroyWindow("Edge detection of the left image"); + + cvNamedWindow("Edge detection of the right image", CV_WINDOW_AUTOSIZE ); + cvShowImage("Edge detection of the right image", imgR_Sobel); + cvWaitKey(0); + cvDestroyWindow("Edge detection of the right image"); + + cvReleaseImage(&imgL_Sobelx); + cvReleaseImage(&imgL_Sobely); + cvReleaseImage(&imgR_Sobelx); + cvReleaseImage(&imgR_Sobely); + + assert (imgL_Sobel->depth == IPL_DEPTH_8U && imgL_Sobel->nChannels == 1); + assert (imgR_Sobel->depth == IPL_DEPTH_8U && imgR_Sobel->nChannels == 1); + } + + //------------------------------------------------------------------------------------------- + //Definition of 2 vectors X and Y to store the coordinates of the displacement between images + //------------------------------------------------------------------------------------------- + if(X_input >= 0 && Y_input >= 0) + { + if(X_input <= X_maxL && Y_input <= Y_maxL) + { + fprintf(output_file, "Working with the %dx%d pixel\n", X_input, Y_input); + array_size = 1; //If we enter a specific pixel, matrices become float + } + else //If x or y is too large, inform the user + { + fprintf(stderr, "Error : x or y is out of the image. Please note that x and y must be such as x<%d and y <%d\n", X_maxL, Y_maxL); + + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + + return EXIT_FAILURE; + } + } + else + { + array_size = floor(imgL->imageSize / imgL->widthStep / subset_size) * floor(imgL->width / subset_size); + } + + //Allocate memory for all the matrices (and initialize them) + if((displacements = malloc(sizeof(*displacements) * 2)) == NULL) + { + perror("malloc:"); + + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + + if(sobel != 0) + { + cvReleaseImage(&imgL_Sobel); + cvReleaseImage(&imgR_Sobel); + } + + return EXIT_FAILURE; + } + if((displacements[0] = malloc(sizeof(**displacements) * array_size)) == NULL) + { + perror("malloc:"); + + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + + if(sobel != 0) + { + cvReleaseImage(&imgL_Sobel); + cvReleaseImage(&imgR_Sobel); + } + + return EXIT_FAILURE; + } + if((displacements[1] = malloc(sizeof(**displacements) * array_size)) == NULL) + { + perror("malloc:"); + + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + + if(sobel != 0) + { + cvReleaseImage(&imgL_Sobel); + cvReleaseImage(&imgR_Sobel); + } + + return EXIT_FAILURE; + } + + if((mean_distance = malloc(sizeof(*mean_distance) * array_size)) == NULL) + { + perror("malloc:"); + + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + free(displacements); + + if(sobel != 0) + { + cvReleaseImage(&imgL_Sobel); + cvReleaseImage(&imgR_Sobel); + } + + return EXIT_FAILURE; + } + + for(j = 0; j < array_size; j++) + { + displacements[0][j] = 0; + displacements[1][j] = 0; + mean_distance[j] = 0; + } + + //-------------------------------------------------------------------- + //Call find_common to fill the matrices and compute the mean distances + //-------------------------------------------------------------------- + fprintf(output_file, "Output is : (X-axis, Y-axis) : (Displacement on X-axis, Displacement on Y-axis) -> Mean Distance\n\n"); + //If we work with a specific pixel + if(X_input >= 0 && Y_input >= 0 && X_input <= X_maxL && Y_input <= Y_maxL) + { + //Get the pointer to the pixel + p1 = (uchar*) imgL->imageData + Y_input*imgL->widthStep + X_input*imgL->nChannels; + + if(fft == 1) //If using FFT + { + template = cvCreateImage(cvSize(imgR->width, imgR->height), IPL_DEPTH_8U, 1); + + cvZero(template); + cvGetSubRect(template,&tmp,cvRect(0,0,subset_size,subset_size)); + cvGetSubRect(imgL,&tmp2,cvRect(X_input,Y_input,subset_size,subset_size)); + cvCopy(&tmp2,&tmp,NULL); + + struct Peak pk = Find_FFT(imgR, template, hamming); + + X_2 = pk.pt.x; + displacements[0][0] = pk.pt.x - X_input; //(= DeltaX) + displacements[1][0] = pk.pt.y - Y_input; //(=DeltaY) + + cvReleaseImage(&template); + } + else + { + //Compute the displacement + // 1 2 + // 3 4 + //4 threads + + data1.img2 = imgR; + data1.p1 = p1; + data1.widthStep1 = imgL->widthStep; + data1.X_1 = X_input; + data1.Y_1 = Y_input; + data1.subset_size = subset_size; + data1.X_start = 0; + data1.Y_start = maximum(0, Y_input - (int) floor(imgR->imageSize/8/imgR->widthStep)); + data1.X_end = floor(X_maxR/2); + data1.Y_end = Y_input; + data1.temp = &temp1; + pthread_create (&thread1, NULL, find_common, &data1); + + data2.img2 = imgR; + data2.p1 = p1; + data2.widthStep1 = imgL->widthStep; + data2.X_1 = X_input; + data2.Y_1 = Y_input; + data2.subset_size = subset_size; + data2.X_start = (int) floor(X_maxR/2) + 1; + data2.Y_start = maximum(0, Y_input - (int) floor(imgR->imageSize/8/imgR->widthStep)); + data2.X_end = X_maxR; + data2.Y_end = Y_input; + data2.temp = &temp2; + pthread_create (&thread2, NULL, find_common, &data2); + + data3.img2 = imgR; + data3.p1 = p1; + data3.widthStep1 = imgL->widthStep; + data3.X_1 = X_input; + data3.Y_1 = Y_input; + data3.subset_size = subset_size; + data3.X_start = 0; + data3.Y_start = Y_input + 1; + data3.X_end = floor(X_maxR/2); + data3.Y_end = minimum(Y_maxR, Y_input + (int) floor(imgR->imageSize/8/imgR->widthStep)); + data3.temp = &temp3; + pthread_create (&thread3, NULL, find_common, &data3); + + data4.img2 = imgR; + data4.p1 = p1; + data4.widthStep1 = imgL->widthStep; + data4.X_1 = X_input; + data4.Y_1 = Y_input; + data4.subset_size = subset_size; + data4.X_start = floor(X_maxR/2) + 1; + data4.Y_start = Y_input + 1; + data4.X_end = X_maxR; + data4.Y_end = minimum(Y_maxR, Y_input + (int) floor(imgR->imageSize/8/imgR->widthStep)); + data4.temp = &temp4; + pthread_create (&thread4, NULL, find_common, &data4); + + //Wait until threads finish + pthread_join(thread1, NULL); + + //Compute all the results together + min = temp1.min; + displacements[0][0] = temp1.X - X_input; //(= DeltaX) + displacements[1][0] = temp1.Y - Y_input; //(=DeltaY) + X_2 = temp1.X; + + pthread_join(thread2, NULL); + if(temp2.min < min) + { + min = temp2.min; + displacements[0][0] = temp2.X - X_input; //(= DeltaX) + displacements[1][0] = temp2.Y - Y_input; //(=DeltaY) + X_2 = temp2.X; + } + + pthread_join(thread3, NULL); + if(temp3.min < min) + { + min = temp3.min; + displacements[0][0] = temp3.X - X_input; //(= DeltaX) + displacements[1][0] = temp3.Y - Y_input; //(=DeltaY) + X_2 = temp3.X; + } + + pthread_join(thread4, NULL); + if(temp4.min < min) + { + min = temp4.min; + displacements[0][0] = temp4.X - X_input; //(= DeltaX) + displacements[1][0] = temp4.Y - Y_input; //(=DeltaY) + X_2 = temp4.X; + } + } + + //Compute mean distance + mean_distance[0] = compute_mean_distance(X_input, X_2, imgL->width, imgR->width, theta, deltaX); + + + if(mean_distance[0] < 0) //If there's an error (mean_distance < 0 is absurd) + { + fprintf(stderr, "An error occurred, negative mean_distance found. Dump :\nCoordinates = (%d, %d) ; Displacement = (%d, %d); Computed value of mean distance : %f\n", X_input, Y_input, displacements[0][0], displacements[1][0], mean_distance[0]); + + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + free(displacements); + free(mean_distance); + + if(output != NULL) + fclose(output_file); + + return EXIT_FAILURE; + } + else //Else, print the result + { + fprintf(output_file, "(%d, %d) : (%d, %d) -> %f\n", X_input, Y_input, displacements[0][0], displacements[1][0], mean_distance[0]); + } + } + //If we work with the entire image + else + { + j = 0; //We use j to go through the matrices + + //Explore the image line by line + for (line1 = (uchar*) imgL->imageData; + line1 <= (uchar*) imgL->imageData + Y_maxL*imgL->widthStep; + line1 += imgL->widthStep*subset_size) + { + for (p1 = line1; p1 <= line1 + X_maxL*imgL->nChannels; p1 += imgL->nChannels*subset_size) + { + //Get the coordinates corresponding to p1 + //(p1 = imageData + X_1 + iChannel + Y_1*widthStep) where iChannel is in {0,1,..nChannels} + X_1 = floor(((p1 - (uchar*) imgL->imageData) % imgL->widthStep) / imgL->nChannels); + Y_1 = floor((p1 - (uchar*) imgL->imageData ) / imgL->widthStep); + + if(fft == 1) //If using FFT + { + template = cvCreateImage(cvSize(imgR->width, imgR->height), IPL_DEPTH_8U, 1); + + cvZero(template); + cvGetSubRect(template,&tmp,cvRect(0,0,subset_size,subset_size)); + cvGetSubRect(imgL,&tmp2,cvRect(X_1,Y_1,subset_size,subset_size)); + cvCopy(&tmp2,&tmp,NULL); + + struct Peak pk = Find_FFT(imgR, template, hamming); + + X_2 = pk.pt.x; + displacements[0][0] = pk.pt.x - X_1; //(= DeltaX) + displacements[1][0] = pk.pt.y - Y_1; //(=DeltaY) + + cvReleaseImage(&template); + } + else + { + //Compute the displacement + // 1 2 + // 3 4 + //4 threads + + data1.img2 = imgR; + data1.p1 = p1; + data1.widthStep1 = imgL->widthStep; + data1.X_1 = X_1; + data1.Y_1 = Y_1; + data1.subset_size = subset_size; + data1.X_start = 0; + data1.Y_start = maximum(0, Y_1 - (int) floor(imgR->imageSize/8/imgR->widthStep)); + data1.X_end = floor(X_maxR/2); + data1.Y_end = Y_1; + data1.temp = &temp1; + pthread_create (&thread1, NULL, find_common, &data1); + + data2.img2 = imgR; + data2.p1 = p1; + data2.widthStep1 = imgL->widthStep; + data2.X_1 = X_1; + data2.Y_1 = Y_1; + data2.subset_size = subset_size; + data2.X_start = (int) floor(X_maxR/2) + 1; + data2.Y_start = maximum(0, Y_1 - (int) floor(imgR->imageSize/8/imgR->widthStep)); + data2.X_end = X_maxR; + data2.Y_end = Y_1; + data2.temp = &temp2; + pthread_create (&thread2, NULL, find_common, &data2); + + data3.img2 = imgR; + data3.p1 = p1; + data3.widthStep1 = imgL->widthStep; + data3.X_1 = X_1; + data3.Y_1 = Y_1; + data3.subset_size = subset_size; + data3.X_start = 0; + data3.Y_start = Y_1 + 1; + data3.X_end = floor(X_maxR/2); + data3.Y_end = minimum(Y_maxR, Y_1 + (int) floor(imgR->imageSize/8/imgR->widthStep)); + data3.temp = &temp3; + pthread_create (&thread3, NULL, find_common, &data3); + + data4.img2 = imgR; + data4.p1 = p1; + data4.widthStep1 = imgL->widthStep; + data4.X_1 = X_1; + data4.Y_1 = Y_1; + data4.subset_size = subset_size; + data4.X_start = floor(X_maxR/2) + 1; + data4.Y_start = Y_1 + 1; + data4.X_end = X_maxR; + data4.Y_end = minimum(Y_maxR, Y_1 + (int) floor(imgR->imageSize/8/imgR->widthStep)); + data4.temp = &temp4; + pthread_create (&thread4, NULL, find_common, &data4); + + //Wait until threads finish + pthread_join(thread1, NULL); + + //Compute all the results together + min = temp1.min; + displacements[0][j] = temp1.X - X_1; //(= DeltaX) + displacements[1][j] = temp1.Y - Y_1; //(=DeltaY) + X_2 = temp1.X; + + pthread_join(thread2, NULL); + if(temp2.min < min) + { + min = temp2.min; + displacements[0][j] = temp2.X - X_1; //(= DeltaX) + displacements[1][j] = temp2.Y - Y_1; //(=DeltaY) + X_2 = temp2.X; + } + + pthread_join(thread3, NULL); + if(temp3.min < min) + { + min = temp3.min; + displacements[0][j] = temp3.X - X_1; //(= DeltaX) + displacements[1][j] = temp3.Y - Y_1; //(=DeltaY) + X_2 = temp3.X; + } + + pthread_join(thread4, NULL); + if(temp4.min < min) + { + min = temp4.min; + displacements[0][j] = temp4.X - X_1; //(= DeltaX) + displacements[1][j] = temp4.Y - Y_1; //(=DeltaY) + X_2 = temp4.X; + } + } + + //Compute mean distance + mean_distance[j] = compute_mean_distance(X_1, X_2, imgL->width, imgR->width, theta, deltaX); + + + if(mean_distance[j] < 0) //If there's an error (mean_distance < 0 is absurd) + { + fprintf(stderr, "An error occurred, negative mean_distance found. Dump :\nCoordinates = (%d, %d) ; Displacement = (%d, %d); Computed value of mean distance : %f\n", X_1, Y_1, displacements[0][j], displacements[1][j], mean_distance[j]); + fprintf(output_file, "Error, absurd mean distance : (%d, %d) : (%d, %d) -> %f\n", X_1, Y_1, displacements[0][j], displacements[1][j], mean_distance[j]); + } + else //Else, print the result + { + fprintf(output_file, "(%d, %d) : (%d, %d) -> %f\n", X_1, Y_1, displacements[0][j], displacements[1][j], mean_distance[j]); + } + + j++; + } + } + } + + + //----------- + //Free memory + //----------- + if(sobel != 0) + { + cvReleaseImage(&imgL_Sobel); + cvReleaseImage(&imgR_Sobel); + } + cvReleaseImage(&imgL); + cvReleaseImage(&imgR); + free(displacements); + free(mean_distance); + + clock_t end=clock(); + + fprintf(output_file, "\nCalcul des distances terminé en %d microsecondes.\n", (int) (end - start)); + + if(output != NULL) + { + fprintf(stdout, "\nCalcul des distances terminé en %d microsecondes.\n", (int) (end - start)); + fclose(output_file); + } + + + return EXIT_SUCCESS; //And exit ;) +} + +//Almost there + +//Just some comments to make it ... + +//What ? You'll see soon... + + + + +// Yeah ! Here we go ! 1337 lines of code !