Commentçamarche.net
CodeS-SourceS
Rechercher un code, un tuto, une réponse

Transformée de hough: détection de droites

0/5 (15 avis)

Vue 18 684 fois - Téléchargée 1 121 fois

Description

Ce programme permet de mieux comprendre le fonctionnement de la transformée de Hough.
Cette transformée permet de changer d'espace de représentation. Un point dans l'espace (X,Y) sera transformée en sinusoïde dans l'espace (R,Theta) et inversement un point dans le plan (R, Theta) sera une droite dans le plan (X,Y).

Ce qui est intéressant, c'est que la succession de point formant une droite dans le plan (X,Y) formera des sinusoïdes qui seront concurrente en un point.

Cette transformé permet donc de détecter les droites dans une image.

Source / Exemple :


//*****************************************************************************************
//Author	MORARD Vincent 
//Date :	28/06/07 
//Contact:	vincent.morard@cpe.fr	
//WebSite:	pistol.petesampras.free.fr
//*****************************************************************************************

//Includes----------------------------------------------------------------------------------
#include "windows.h"
#include "cmugraphics.h"
#include "math.h"
#include <vector>
using namespace std;

//Define------------------------------------------------------------------------------------
#define SPACE_X1 100
#define SPACE_Y1 10
#define SPACE_X2 300
#define SPACE_Y2 210

#define SPACE_CENTER_X 200
#define SPACE_CENTER_Y 110

#define HOUGH_X1 50
#define HOUGH_Y1 230
#define HOUGH_X2 350
#define HOUGH_Y2 510

#define HOUGH_CENTER_X 50
#define HOUGH_CENTER_Y 370
const double Pi = 3.141592653589793238462643383279;

//Struct---------------------------------------------------------------------------------
struct Transform
{
	double X,Y,Theta,R;
	bool Inv_Transform;
};

//Header's function-----------------------------------------------------------------------
void DrawAxis(window &F,int Graph=-1);
void DrawCosinus(window &F,double R,double Theta);
void DrawLine(window &F,double X,double Y,double Theta);
void UpdateHough(window &F,vector<Transform> Vect);
void UpdateSpace(window &F,vector<Transform> Vect);
void ButtonDownAction(window &F,vector<Transform> *Vect,int MouseX,int MouseY,bool Final=0);
void UpdateCoord(window &F,vector<Transform> *Vect);
void DrawInterface(window &F,bool *Reset,bool *Exit,bool *Click);

//*******************************************************************************************

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)

{
	bool Exit=false;
	bool Reset=true;
	bool Click=false;
	int Cpt=0;
	window F(390,600,0,0);			//create a 390 width and 600 Height window 
									//at the position (0,0)
	F.SetWaitClose(false);			//Close the window when we exit
	F.SetBuffering(true);			//Allow double buffering
	F.ChangeTitle("Hough tranform");//Change title
	
	vector<Transform>Vect;			//Vector to stock all the parameters
	Vect.resize(0);					//init vector

	do
	{
		if(++Cpt==100)
		{
			MSG msg;
			if(!GetMessage(&msg, NULL, 0, 0))//On recoit le message de fin =>on a appuyé sur la croix
				exit(1);
			Cpt=0;
		}
		if(Reset)					//if Reset : we redraw axis
		{	
			Reset=false;
			DrawAxis(F);
			Vect.resize(0);			//And we resize to 0 the vector
		}
		UpdateCoord(F,&Vect);		//To update the points and theres transforms
		DrawInterface(F,&Reset,&Exit,&Click);		//Button
		F.UpdateBuffer();			
		Sleep(20);					//The process gives back the hand to an another process
									//during 20 ms
	}
	while(!Exit);
	exit(0);
	return 0;
}

//This function checks the mouse's event and calls the appropriate function
void UpdateCoord(window &F,vector<Transform>*Vect)
{
	int MouseX,MouseY;
	bool Click=false;
	
	F.GetMouseCoord(MouseX,MouseY);
	if(MouseY>540)
		return;
	//If the user clicks on the screen, he will see the result of Hough tranform in real time
	while(F.GetButtonState(LEFT_BUTTON,MouseX,MouseY)==BUTTON_DOWN)
	{	
		
		ButtonDownAction(F,Vect,MouseX,MouseY);		//temporary action
		F.UpdateBuffer();
	
		Sleep(30);
		Click=true;
	}
	if(Click)											//if we click the result keeps on the sreen
		ButtonDownAction(F,Vect,MouseX,MouseY,1);		
}

//This function takes back the coord of the mouse in the graph
//and calls the function to make a transformation into Hough's space.
void ButtonDownAction(window &F,vector<Transform> *Vect,int MouseX,int MouseY,bool Final)
{
	Transform Hough;		//this structure allow us to stock all the parameters of the Hough's transform
							//X and Y , R and theta

	//if the mouse is over the graph 1
	if(MouseX > SPACE_X1 && MouseX < SPACE_X2 && MouseY > SPACE_Y1 && MouseY < SPACE_Y2)
	{
		//We just have to tranform MouseX and MouseY to have the coord of the position of
		//the mouse on the graph
		Hough.X = MouseX-SPACE_CENTER_X;
		Hough.Y = SPACE_CENTER_Y-MouseY;
		//With X and Y we are able to get R and Theta in polar coord
		//R=sqrt(X² + Y²)
		//tan(Theta) = Y/X
		Hough.R = sqrt((double)(Hough.X*Hough.X+Hough.Y*Hough.Y));
		Hough.Theta = atan((double)Hough.Y/Hough.X)+Pi/2;
		if(Hough.X>0)
			Hough.Theta+=Pi;
		Hough.Inv_Transform = 0;		//Hough transform

		Vect->push_back(Hough);			//we push the new point
		UpdateHough(F,*Vect);			//and we update the transform
		if(!Final)
			Vect->pop_back();			//if we still click, we pop the new point
		else
		{
			F.SetPen(RED);				//if we stop clicking, we draw a circle on the screen
			F.SetBrush(RED);
			F.DrawCircle(MouseX,MouseY,3,FRAME);
	
		}
			

	}
	else if(MouseX >= HOUGH_X1 && MouseX < HOUGH_X2 && MouseY > HOUGH_Y1 && MouseY < HOUGH_Y2)
	{
		double Pas=Pi/(HOUGH_X2-HOUGH_X1);
		//We have got easily R and Theta 
		Hough.Theta = (MouseX-HOUGH_CENTER_X)*Pas;
		Hough.R = HOUGH_CENTER_Y-MouseY;
		//And we convert R and Theta in cartesien coord
		Hough.Y = Hough.R*cos(Hough.Theta);
		Hough.X = -Hough.R*sin(Hough.Theta);
		Hough.Inv_Transform = 1;		//Hough reverse

		Vect->push_back(Hough);
		UpdateSpace(F,*Vect);
		if(!Final)
			Vect->pop_back();
		else
		{
			F.SetBrush(GREEN);
			F.SetPen(GREEN);	
			F.DrawCircle(MouseX,MouseY,3,FILLED);
		}	
	}
}

void UpdateHough(window &F,vector<Transform> Vect)
{
	
	DrawAxis(F,2);
	F.SetPen(GREEN);
	F.SetBrush(GREEN);
	int Size=Vect.size();
	for(int i=0;i<Size;i++)
	{
		if(! Vect[i].Inv_Transform )
			DrawCosinus(F,Vect[i].R,Vect[i].Theta);
		else
			F.DrawCircle(Vect[i].Theta*(HOUGH_X2-HOUGH_X1)/Pi+HOUGH_CENTER_X,HOUGH_CENTER_Y-Vect[i].R,3);
			
	}
		
}
void UpdateSpace(window &F,vector<Transform> Vect)
{
	
	DrawAxis(F,1);
	F.SetPen(RED);
	F.SetBrush(RED);
	int Size=Vect.size();
	for(int i=0;i<Size;i++)
		if(Vect[i].Inv_Transform )
			DrawLine(F,Vect[i].X,Vect[i].Y,Vect[i].Theta);
		else
			F.DrawCircle(Vect[i].X+SPACE_CENTER_X,SPACE_CENTER_Y-Vect[i].Y,3);
			

}

void DrawCosinus(window &F,double R,double Theta)
{
	int Width=HOUGH_X2-HOUGH_X1;
	int Height=HOUGH_Y2-HOUGH_Y1;
	double x,y;
	double Pas=Pi/Width;		
	F.SetPen(GREEN);
	F.SetBrush(GREEN);
	//for each pixel of the graph, we evaluate the y coord with cosinus
	for(double i=HOUGH_X1;i<HOUGH_X2;i++)
	{
		x=i-HOUGH_CENTER_X;
		x*=Pas;					//conversion into radian
		y=R*cos(x-Theta);	
		F.DrawPixel(i,HOUGH_CENTER_Y-y);
	}		
}

//This function is able to draw a line with one point and one angle
void DrawLine(window &F,double X,double Y,double Theta)
{
	//Vecteur is the direction
	double Vecteur=tan(Theta);	

	X+=SPACE_CENTER_X;
	Y=SPACE_CENTER_Y-Y;
	if(X>SPACE_X2 || X<SPACE_X1 || Y>SPACE_Y2 || Y<SPACE_Y1)
		return;

	int LastX=X;
	int LastY=Y;
	double  i,j;
	
	F.SetPen(RED);
	F.SetBrush(RED);
	for(i=X;i<=SPACE_X2;i+=0.5)
	{
		if((j=Y-Vecteur*(i-X))>SPACE_Y2 || j<SPACE_Y1)
			break;
		
		LastX=i;				//We get the coord of the point whith cut the graph's window
		LastY=j;
	}
	F.DrawLine((int)X,(int)Y,LastX,LastY);
	LastX=X;
	LastY=Y;

	//We do the same thing in the other way
	for(i=X;i>=SPACE_X1;i-=0.5)
	{
		if((j=j=Y-Vecteur*(i-X))>SPACE_Y2 || j<SPACE_Y1)
			break;
		
		LastX=i;
		LastY=j;
	}
	F.DrawLine((int)X,(int)Y,LastX,LastY);

}

void DrawInterface(window &F,bool *Reset,bool *Exit,bool *Click)
{
	int MouseX,MouseY;
	F.SetFont(20,BOLD,SWISS);
	F.SetPen(WHITE);
	F.SetBrush(WHITE);
	F.DrawRectangle(100,540,400,590);

	F.SetPen(NAVYBLUE,2);
	//F.DrawRectangle(210,545,270,575);
	F.DrawString(110,550,"Reset");
	F.DrawString(220,550,"Quit");

	if(F.GetButtonState(LEFT_BUTTON,MouseX,MouseY)==BUTTON_DOWN)
	{
		if(MouseX>=80 && MouseX<180 && MouseY>545 && MouseY<575)

  • Click=true;
if(MouseX>=210 && MouseX<270 && MouseY>545 && MouseY<575)
  • Click=true;
} else if (*Click) {
  • Click=false;
if(MouseX>=80 && MouseX<180 && MouseY>545 && MouseY<575)
  • Reset=true;
if(MouseX>=210 && MouseX<270 && MouseY>545 && MouseY<575)
  • Exit=true;
} else
  • Click=false;
} void DrawAxis(window &F,int Graph) { F.SetBrush(WHITE); F.SetFont(15,BOLD,SWISS); if(Graph!=2) //We do not draw again graph 2 { F.SetPen(DARKRED,3); F.DrawRectangle(SPACE_X1,SPACE_Y1,SPACE_X2,SPACE_Y2); F.SetPen(BLACK); //y-axis F.DrawLine(SPACE_CENTER_X,SPACE_Y1,SPACE_CENTER_X,SPACE_Y2); //x-axis F.DrawLine(SPACE_X1,SPACE_CENTER_Y,SPACE_X2,SPACE_CENTER_Y); //Arrows F.DrawLine(SPACE_CENTER_X-5,SPACE_Y1+9,SPACE_CENTER_X,SPACE_Y1); F.DrawLine(SPACE_CENTER_X+5,SPACE_Y1+9,SPACE_CENTER_X,SPACE_Y1); F.DrawLine(SPACE_X2-5,SPACE_CENTER_Y+3,SPACE_X2,SPACE_CENTER_Y); F.DrawLine(SPACE_X2-5,SPACE_CENTER_Y-3,SPACE_X2,SPACE_CENTER_Y); //Legende F.DrawString(SPACE_CENTER_X,SPACE_Y1-16,"y"); F.DrawString(SPACE_X2+10,SPACE_CENTER_Y,"x"); } if(Graph!=1) //We do not draw again graph 1 { F.SetPen(DARKGREEN,3); F.DrawRectangle(HOUGH_X1,HOUGH_Y1,HOUGH_X2,HOUGH_Y2); F.SetPen(BLACK); //y-axis F.DrawLine(HOUGH_CENTER_X,HOUGH_Y1,HOUGH_CENTER_X,HOUGH_Y2); //x-axis F.DrawLine(HOUGH_X1,HOUGH_CENTER_Y,HOUGH_X2,HOUGH_CENTER_Y); //Arrows F.DrawLine(HOUGH_CENTER_X-5,HOUGH_Y1+9,HOUGH_CENTER_X,HOUGH_Y1); F.DrawLine(HOUGH_CENTER_X+5,HOUGH_Y1+9,HOUGH_CENTER_X,HOUGH_Y1); F.DrawLine(HOUGH_X2-5,HOUGH_CENTER_Y+3,HOUGH_X2,HOUGH_CENTER_Y); F.DrawLine(HOUGH_X2-5,HOUGH_CENTER_Y-3,HOUGH_X2,HOUGH_CENTER_Y); //Legende F.DrawString(HOUGH_CENTER_X,HOUGH_Y1-16,"Theta"); F.DrawString(HOUGH_X2+8,HOUGH_CENTER_Y,"R"); } }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.