1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "header.h"
#define MIN(a,b) ( (a)<(b) ? (a):(b) ) //Macro pour le calcul du minimum de 2 nombres
#define MAX(a,b) ( (a)>(b) ? (a):(b) ) //Macro pour le calcul du maximum de 2 nombres
// Image definié en 16:9e pour tests
#define M 9 //Nombre de lignes de l'image
#define N 16 //Nombre de colonnes de l'image
// Valeur arbitraire pour les couleurs de l'image (noir et blanc)
// A remplacer par les valeurs données par le groupe 1
#define val_noir 0
#define val_blanc 1
// Toutes les variables pour chaque image
typedef struct
{
int t[M][N]; //Image stockée
int gX, gY; //Coordonnées du centre de gravité
int gAxe[2], pAxe[2]; //Coordonnées des points a et b dans l'ellipse
float theta; //Angle d'inclinaison du corps
} Image;
// Fonction de calcul du Plus Grand Commun Diviseur de 2 nombres
int PGCD(int a, int b)
{
while(b!=0)
{
if( a>=0 && b>=0 )
{
int c=a%b;
a=b;
b=c;
}
else
{
if(a<0) a=-a;
if(b<0) b=-b;
}
}
return a;
}
// Fonction de calcul de Tout les Communs Diviseurs à partir du PGCD
void TCD(int *t, int val)
{
int i=2, k=1;
while( i<=val )
{
if( !(val%i) )
{
t[k]=i;
k++;
}
i++;
}
t[0]=k-1;
}
// Fonction de calcul du centre de gravité par la
// methode des moments geometriques
void pointG(Image *img)
{
int i, j;
int m00=0, m10=0, m01=0;
for (i=0; i<M; i++)
{
for (j=0; j<N; j++)
{
if(img->t[i][j] == val_blanc)
{
m00++;
m10+=i;
m01+=j;
}
}
}
img->gX=(int)( m10/m00 );
img->gY=(int)( m01/m00 );
}
/*
// Fonctin de calcul du centre de gravité par une
// methode personnelle
void pointG(Image *img)
{
int i, j;
int Xmin=M, Xmax=0, Ymin=N, Ymax=0;
for (i=0; i<M; i++)
{
for (j=0; j<N; j++)
{
if(img->t[i][j] == val_blanc)
{
Xmin=MIN(Xmin, i);
Ymin=MIN(Ymin, j);
Xmax=MAX(Xmax, i);
Ymax=MAX(Ymax, j);
}
}
}
img->gX=(int)( (Xmin+Xmax)/2 );
img->gY=(int)( (Ymin+Ymax)/2 );
}
*/
// Fonction de calcul du grand axe de l'ellipse via la plus grande
// distance par rapport au centre de gravité
void grandAxe(Image *img)
{
int i, j;
float dist=0, tmp=0;
for (i=0; i<=img->gX; i++)
{
for (j=0; j<N; j++)
{
if(img->t[i][j] == val_blanc)
{
tmp=sqrt( pow(abs(i-img->gX), 2) + pow(abs(j-img->gY), 2) );
if( tmp>dist )
{
dist=tmp;
img->gAxe[0]=i;
img->gAxe[1]=j;
}
}
}
}
}
// Fonction de calcul du petit axe de l'ellipse via le calcul des TCD
// et de la perpendicularité des 2 axes
void petitAxe(Image *img)
{
int i, tmpI, tmpJ;
int tcd[ PGCD( (img->gAxe[0] - img->gX) , (img->gAxe[1] - img->gY) ) ];
TCD(tcd, PGCD( (img->gAxe[0] - img->gX) , (img->gAxe[1] - img->gY) ));
if( tcd[0]==0 || tcd[0]==1 )
{
img->pAxe[0]=img->gX;
img->pAxe[1]=img->gY;
}
else
{
for(i=1; i<=tcd[0]; i++)
{
tmpI = (int)(img->gAxe[0]/tcd[i]);
tmpJ = (int)(img->gAxe[1]/tcd[i]);
if( img->t[tmpI][tmpJ] == val_blanc )
{
img->pAxe[0]=img->gX - tmpI;
img->pAxe[1]=img->gY - tmpJ;
}
}
}
}
// Fonction de calcul de théta, angle entre l'axe des ordonnées et
// l'orientation du grand axe de l'ellipse
void valTheta(Image* img)
{
int i, j;
float num=0, den1=0, den2=0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
if( img->t[i][j] == val_blanc )
{
num += i*j;
den1 += pow(i,2);
den2 += pow(j,2);
}
}
}
img->theta=(180*atan(2*num/(den1-den2)))/M_PI;
if(img->theta < 0) img->theta = -img->theta;
}
// A voir pour les taches
int main(int argc, char *argv[])
{
Image image={ {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,
0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,
0,0,1,0,0,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,
0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0},
{0},{0},{0,0},{0,0},{0} };
o int i, j;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%d ", image.t[i][j]);
}
printf("\n");
}
pointG(&image);
grandAxe(&image);
petitAxe(&image);
valTheta(&image);
printf("\nCoordonnees de G: %d-%d\n", image.gX, image.gY);
printf("\nCoordonnees de a: %d-%d\n", image.gAxe[0], image.gAxe[1]);
printf("\nCoordonnees de b: %d-%d\n", image.pAxe[0], image.pAxe[1]);
printf("\nValeur de l'angle theta: %f\n", image.theta);
return 0;
} |
Partager