Added Euclidean distance calculations

This commit is contained in:
William DURAND 2011-12-05 17:17:32 +01:00
parent 75a80803e6
commit 2a41e3b430
4 changed files with 121 additions and 15 deletions

11
Euclidean.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "Euclidean.hpp"
double Euclidean::computeDistanceFrom(const Euclidean & euclidian)
{
double diffx = _x - euclidian.getX();
double diffy = _y - euclidian.getY();
double diffx_sqr = pow(diffx, 2);
double diffy_sqr = pow(diffy, 2);
return sqrt(diffx_sqr + diffy_sqr);
}

39
Euclidean.hpp Normal file
View File

@ -0,0 +1,39 @@
#ifndef __EUCLIDIAN_HPP__
#define __EUCLIDIAN_HPP__
#include <math.h>
class Euclidean
{
public:
Euclidean(const int x, const int y) : _x(x), _y(y) {}
void setX(const int x) { _x = x; }
int getX() const { return _x; }
void setY(const int y) { _y = y; }
int getY() const { return _y; }
void setDistance(const double distance) { _distance = distance; }
double getDistance() const { return _distance; }
void setNearest(Euclidean & nearest) { _nearest = &nearest; }
Euclidean* getNearest() const { return _nearest; }
double computeDistanceFrom(const Euclidean & euclidian);
private:
int _x;
int _y;
double _distance;
Euclidean *_nearest;
};
#endif

View File

@ -2,7 +2,7 @@
.SUFFIXES:.cpp .o .SUFFIXES:.cpp .o
EXEC=emd EXEC=emd
SOURCES=main.cpp SOURCES=main.cpp Euclidean.cpp
OBJETS=$(SOURCES:%.cpp=%.o) OBJETS=$(SOURCES:%.cpp=%.o)
@ -16,9 +16,9 @@ $(EXEC):$(OBJETS) Makefile
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
clean: clean:
rm $(OBJETS) rm -f $(OBJETS)
clear: clear:
rm $(EXEC) rm -f $(EXEC)
depend: depend:
sed -e "/^#DEPENDANCIES/,$$ d" Makefile >dependances sed -e "/^#DEPENDANCIES/,$$ d" Makefile >dependances
echo "#DEPENDANCIES" >> dependances echo "#DEPENDANCIES" >> dependances
@ -26,4 +26,5 @@ depend:
cat dependances >Makefile cat dependances >Makefile
rm dependances rm dependances
main.o: main.cpp main.o: main.cpp Euclidean.hpp
Euclidean.o: Euclidean.cpp

View File

@ -7,6 +7,9 @@
#include "CImg.h" #include "CImg.h"
#include <math.h> #include <math.h>
#include <vector>
#include "Euclidean.hpp"
using namespace cimg_library; using namespace cimg_library;
@ -19,13 +22,15 @@ int main()
CImgDisplay dispBase(imgLena,"Image de base"); CImgDisplay dispBase(imgLena,"Image de base");
std::vector<Euclidean> vectEMax, vectEMin;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Part 1: Finding minimas and maximas // // Part 1: Finding minimas and maximas //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
CImg<unsigned char> imgMax = imgLena.channel(0); CImg<unsigned char> imgMax = imgLena.channel(0);
CImg<unsigned char> imgMin = imgLena.channel(0); CImg<unsigned char> imgMin = imgLena.channel(0);
imgMax.print(); imgMax.print();
for (int i = 0; i<imgLena.width() ; i+=3) { for (int i = 0; i<imgLena.width() ; i+=3) {
for (int j = 0; j<imgLena.height() ; j+=3) { for (int j = 0; j<imgLena.height() ; j+=3) {
@ -40,6 +45,9 @@ int main()
unsigned char max = imgMax(i,j); unsigned char max = imgMax(i,j);
unsigned char min = imgMin(i,j); unsigned char min = imgMin(i,j);
Euclidean eMax(i, j);
Euclidean eMin(i, j);
// 3x3 // 3x3
for (int k = i; k<i+3 ; k++) { for (int k = i; k<i+3 ; k++) {
for (int l = j; l<j+3 ; l++) { for (int l = j; l<j+3 ; l++) {
@ -52,6 +60,9 @@ int main()
imgMax(xmax,ymax) = 0; imgMax(xmax,ymax) = 0;
xmax = k; xmax = k;
ymax = l; ymax = l;
eMax.setX(k);
eMax.setY(l);
} }
// Min // Min
@ -59,15 +70,64 @@ int main()
imgMin(k,l) = 0; imgMin(k,l) = 0;
} else { } else {
min = imgMin(k,l); min = imgMin(k,l);
imgMin(xmin,ymin) = 0; imgMin(xmin,ymin) = 0;
xmin = k; xmin = k;
ymin = l; ymin = l;
eMin.setX(k);
eMin.setY(l);
} }
} }
} }
vectEMax.push_back(eMax);
vectEMin.push_back(eMin);
} }
} }
// Array of Euclidean distance to the nearest non zero element
std::vector<Euclidean>::iterator it1, it2;
for (it1 = vectEMax.begin(); it1 != vectEMax.end(); it1++) {
for (it2 = it1 + 1; it2 != vectEMax.end(); it2++) {
double dist = (*it1).computeDistanceFrom(*it2);
if (0 == (*it1).getDistance() || dist < (*it1).getDistance()) {
(*it1).setDistance(dist);
(*it1).setNearest(*it2);
}
if (0 == (*it2).getDistance() || dist < (*it2).getDistance()) {
(*it2).setDistance(dist);
(*it2).setNearest(*it1);
}
}
}
for (it1 = vectEMin.begin(); it1 != vectEMin.end(); it1++) {
for (it2 = it1 + 1; it2 != vectEMin.end(); it2++) {
double dist = (*it1).computeDistanceFrom(*it2);
if (0 == (*it1).getDistance() || dist < (*it1).getDistance()) {
(*it1).setDistance(dist);
(*it1).setNearest(*it2);
}
if (0 == (*it2).getDistance() || dist < (*it2).getDistance()) {
(*it2).setDistance(dist);
(*it2).setNearest(*it1);
}
}
}
// Calculate the window size
// Order filters with source image
// Calculate the upper envelope
// Calculate the lower envelope
// Display images for max and min // Display images for max and min
CImgDisplay dispMax(imgMax,"Image de Max"); CImgDisplay dispMax(imgMax,"Image de Max");
CImgDisplay dispMin(imgMin,"Image de Min"); CImgDisplay dispMin(imgMin,"Image de Min");
@ -76,12 +136,7 @@ int main()
// Part 2: Average // // Part 2: Average //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Calculate the upper envelope // Calculate the Average
// ??
// Calculate the lower envelope
// ??
// Calculate the Average
CImg<unsigned char> imgMoyenne = (imgMax+imgMin)/2; CImg<unsigned char> imgMoyenne = (imgMax+imgMin)/2;
CImgDisplay dispMoyenne(imgMoyenne,"Image Moyenne"); CImgDisplay dispMoyenne(imgMoyenne,"Image Moyenne");