Oui, l'intro est peut être abrupte. L'oeil c'est la partie encrée du caractère qui lui-même est défini dans une boîte rectangulaire. Je récupère donc les pixels colorés (en inverse video) de l'oeil et je mets les coordonnées dans un tableau de pair. Je pose mes points "carrés" sur ma picture.
Il faut aussi un fichier pgm qui contient le caractère.
J'espère que le code est assez clair.
Mais je vais poser la question sur le forum d'Asymptote.
Code : Tout sélectionner
import graph;
usepackage("amsmath");
struct profils {
pair[] hist[];
int[] dimhist;
}
struct image{
string nom;
int largeur,hauteur;
pair[] valeurs;
int hauteuroeil,largeuroeil;
profils histogrammeV,histogrammeH;
}
image lect_image(string fichier)
{
int resolution = 2400;
int taille = 12;
string fich = fichier + ".pgm";
string[] nom = split(fichier,"/");
image glyphe = new image;
glyphe.nom = nom[(nom.length)-1]; //Nom du glyphe
file fin=input(fich).line().word();
string ligne=fin.line();
int[] ligne=fin.line();
int largeur = ligne[0]; //largeur de la boîte
glyphe.largeur = largeur;
int hauteur = ligne[1]; //hauteur de la boîte
glyphe.hauteur = hauteur;
string ligne=fin.line();
int[] valeur;
while (!(eof(fin)))
{
int[] ligne=(int[])split(fin.line()," ");
valeur.append(ligne);
}
close(fin);
int x[],y[];
int cpt=0;
for(int i = 0 ; i < valeur.length ; ++i)
{
if (valeur[i] != 0)
{
int lg = (int)(i/largeur);
int co = i % largeur;
x.push(co);
y.push(hauteur-lg);
cpt = cpt +1;
}
}
pair[] zz = pairs(x,y);
glyphe.valeurs = zz;
return glyphe;
}
// Dimension de la bbox lue dans le fichier pgm
int[] dimension(string fichier)
{
string fich = fichier + ".pgm";
file fin=input(fich).line().csv();
string ligne;
for (int i=1; i<=2; ++i)
{
ligne=fin.line();
}
string[] dimension;
dimension = split(ligne, " ");
return (int[])dimension;
close(fin);
}
// Génération du fichier png de l'enveloppe convexe des caractères
void envconv(string fichier,int largeur,int hauteur)
{
string fich = fichier + "_env_conv.dat";
file fin=input(fich).line().csv();
real[][] A=fin.dimension(0,0);
A = transpose(A);
real[] abscisses= A[0], ordonnees= A[1];
size(largeur,hauteur);
real[] x,y;
path cadre=nullpath;
cadre = ((0,0)--(largeur,0)--(largeur,hauteur)--(0,hauteur)--cycle);
// fill(cadre,white);
// draw(cadre,red);
path CH = nullpath;
CH = (abscisses[0],ordonnees[0]);
for (int i = 1; i <abscisses.length; ++i)
{
CH = CH --(abscisses[i],ordonnees[i]);
}
CH = CH --cycle;
// draw(CH,linewidth(1.5bp));
//shipout(fichier + "_env_conv",format = "png");
//erase();
}
// Structure récupérant le contenu des fichiers de données des caractères
struct data{
string[] glyphe;
real[] Soeil, Poeil;
int[] chi;
real[] complexite, Sboite, Pboite, inradius, centre_x, centre_y, Aire_CH, Perimetre_CH, Diametre;
}
data lect_data(string fonte)
{
int resolution = 2400;
int taille = 12;
string chemin = "./"+fonte+"/"+fonte+"-"+(string)resolution+"/";
string fichier=chemin+fonte+"-"+(string)resolution+"-"+string(taille);
string fich = fichier + ".dat";
data T = new data;
file fin=input(fich).line().word();
string[][] A=fin.dimension(1,0);
A=fin.dimension(0,0);
string[][] B = transpose(A);
T.glyphe = B[0];
T.Soeil = (real[])B[1];
T.Poeil = (real[])B[2];
T.chi = (int[])B[3];
T.complexite = (real[])B[4];
T.Sboite = (real[])B[5];
T.Pboite = (real[])B[6];
T.inradius = (real[])B[7];
T.centre_x = (real[])B[8];
T.centre_y = (real[])B[9];
T.Aire_CH = (real[])B[10];
T.Perimetre_CH = (real[])B[11];
T.Diametre = (real[])B[12];
close(fin);
return T;
}
// // Récupération des coordonnées du contour d'un caractère
pair[] contour(string fichier)//,int largeur,int hauteur)
{
string fich = fichier + "_outline.dat";
file fin=input(fich).line().csv();
string titre;
titre = fin.line();
real[][] A=fin.dimension(0,0);
A = transpose(A);
real[] abscisses= A[0], ordonnees= A[1];
pair[] z;
for (int i = 0; i <abscisses.length; ++i)
{
z[i] = (abscisses[i],ordonnees[i]);
}
close(fin);
//shipout(fichier + "_outline",format = "png");
//erase();
return z;
}
pair[] env_conv(string fichier)//,int largeur,int hauteur)
{
string fich = fichier + "_env_conv.dat";
file fin=input(fich).line().csv();
real[][] A=fin.dimension(0,0);
A = transpose(A);
real[] abscisses= A[0], ordonnees= A[1];
pair[] z;
for (int i = 0; i <abscisses.length; ++i)
{
z[i] = (abscisses[i],ordonnees[i]);
}
//shipout(fichier + "_outline",format = "png");
//erase();
close(fin);
return z;
}
// picture histV;
path proj(profils toto,int largeur) //profils : struct profils
{
path histo = (0,0);
for(int k=0; k<toto.dimhist.length; ++k )
{ histo = histo -- (k,toto.dimhist[k]); }
histo = histo -- (largeur,0)--cycle;
return histo;
}
pair[] derivee(profils profil)
{
pair[] resultat;
// for(int k=0; k < glyphe.histogrammeH.dimhist.length;++k)
for(int k=0; k < profil.dimhist.length;++k)
{int d;
if (k == 0)
{ d = profil.dimhist[0]-profil.dimhist[profil.dimhist.length-1];}
else
{
if(k==profil.dimhist.length-1)
{ d = profil.dimhist[profil.dimhist.length-1]-profil.dimhist[0];}
else { d = profil.dimhist[k+1] - profil.dimhist[k];}
}
// write(sortie,(string)k + ' ' +(string)glyphe.histogrammeH.dimhist[k]+' '+(string)d+'\n');
resultat.push((k,d));
}
return resultat;
}
//string [] fontes ={"Courier","Cantarell-Regular","Times-Italic","Palatino-Italic" ,"AMS-Medium","Century-Schoolbook-Roman" ,"Times-Roman","Arial-Normal","AvantGarde-Book","Palatino-Roman","Helvetica-Oblique", "Computer-Modern-Medium","Helvetica","NewCenturySchlbk-Roman","NewCenturySchlbk-Italic","Bookman-Demi"};
string[] fontes = {"Computer-Modern-Medium"};
// [] fontes ={//"Courier","Cantarell-Regular","Times-Italic","Palatino-Italic" ,"AMS-Medium","Century-Schoolbook-Roman" ,
//"Times-Roman","Arial-Normal","AvantGarde-Book","Palatino-Roman",
//"Helvetica-Oblique","Computer-Modern-Medium","Helvetica","NewCenturySchlbk-Roman","NewCenturySchlbk-Italic","Bookman-Demi"};
string lettre = "NOPQRSTUVWXYZ0123456789";//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//";//
string extension[]= {"_outline.dat",".pgm","_outline.png"};
int resolution = 2400;
int taille = 12;
for (string fonte : fontes)
{
string chemin = "./"+fonte+"/"+fonte+"-"+(string)resolution+"/";
// write(chemin);
// data T = lect_data(fonte);
for (int i = 0; i<length(lettre); ++i)
{
string fichier=chemin+substr(lettre,i,1)+"_"+fonte+"-"+(string)resolution+"-"+string(taille);
write('fichier : ', fichier);
image glyphe = new image;
glyphe = lect_image(fichier); //récupère les éléments de l'image du glyphe
int[] x,y;
// write('nombre de pixels : ', glyphe.valeurs.length);
for(pair p : glyphe.valeurs)
{
x.push((int)xpart(p)); y.push((int)ypart(p));
}
int miny,maxy,minx,maxx;
minx = min(x);
maxx = max(x);
miny = min(y);
maxy = max(y);
pair delta = (minx,miny);
int h_oeil = (maxy - miny);
// write('hauteur de l\'oeil : ',h_oeil);
glyphe.hauteuroeil = h_oeil;
int l_oeil = (maxx - minx);
// write('largeur de l\'oeil : ',l_oeil);
glyphe.largeuroeil = l_oeil;
//tracé du contour de la boite du glyphe -> picture glyphe_plan
//pen ordinaire + rouge
path cadre_glyphe = nullpath;
pen stylo=makepen(scale(.8)*unitsquare);
pen stylo_glyphe=makepen(scale(.8)*unitsquare);
profils profglypheH = new profils;
for(int lig=miny; lig <= maxy; ++lig)
{
pair[] ligneH;
for(int indice=0; indice<glyphe.valeurs.length;++indice)
{
if (ypart(glyphe.valeurs[indice]) == lig)
{
ligneH.push(glyphe.valeurs[indice]);
}
}
profglypheH.hist.push(ligneH);
profglypheH.dimhist.push(ligneH.length);
// dot(ligneV,convex3);
}
profils profglypheV = new profils;
for(int col=minx; col <= maxx; ++col)
{
pair[] ligneV;
for(int indice=0; indice<glyphe.valeurs.length;++indice)
{
if (xpart(glyphe.valeurs[indice]) == col)
{
ligneV.push(glyphe.valeurs[indice]);
}
}
profglypheV.hist.push(ligneV);
profglypheV.dimhist.push(ligneV.length);
// dot(ligneH,convex3);
}
glyphe.histogrammeH = profglypheH;
glyphe.histogrammeV = profglypheV;
if (rfind("abcdefghijklmnopqrstuvwxyz", substr(lettre,i,1), pos=-1) != -1)
{fichier=chemin+substr(lettre,i,1)+"_min_"+fonte+"-"+(string)resolution+"-"+string(taille);}
else
{fichier=chemin+substr(lettre,i,1)+"_"+fonte+"-"+(string)resolution+"-"+string(taille);}
path boite=box((0,0),(glyphe.largeuroeil,glyphe.hauteuroeil+1));
cadre_glyphe = shift(0,0)*((0,0)--(glyphe.largeuroeil,0)--(glyphe.largeuroeil,glyphe.hauteuroeil)--(0,glyphe.hauteuroeil)--cycle);
// size(glyphe.largeur+2*minx,glyphe.hauteur+2*miny);
fill(currentpicture,shift(0,0)*boite,white);
dot(currentpicture,shift(-minx,-miny)*glyphe.valeurs,stylo_glyphe+lightgray);
draw(currentpicture,shift(0,-miny)*proj(profglypheV,glyphe.largeuroeil));
draw(currentpicture,shift(glyphe.largeuroeil+1+minx,0)*rotate(90,(0,0))*proj(profglypheH,glyphe.hauteuroeil));
string nom_sauv = fichier+"_profils.dat";
file sortie= output(nom_sauv);
write(sortie,substr(lettre,i,1)+"_"+fonte+"-"+(string)resolution+"-"+string(taille)+ '\n');
write(sortie,"Profil vertical"+ '\n');
write(sortie, "abscisse, ordonnée, pente"+ '\n');
pair[] toto = derivee(glyphe.histogrammeV) ;
for(int k = 0; k <toto.length; ++k)
{
write(sortie,(string)k + ' ' +(string)glyphe.histogrammeV.dimhist[k]+' '+(string)ypart(toto[k])+'\n');
draw(currentpicture,shift(0,-miny)*((xpart(toto[k]),0)--(xpart(toto[k]),ypart(toto[k]))));
}
write(sortie, '\n');
write(sortie,"Profil horizontal"+ '\n');
pair[] toto = derivee(glyphe.histogrammeH) ;
for(int k = 0; k <toto.length; ++k)
{
write(sortie,(string)k + ' ' +(string)glyphe.histogrammeH.dimhist[k]+' '+(string)ypart(toto[k])+'\n');
draw(currentpicture,shift(glyphe.largeuroeil+1+minx,0)*rotate(90,(0,0))*((xpart(toto[k]),0)--(xpart(toto[k]),ypart(toto[k]))));
}
close(sortie);
shipout(fichier + "_profils",format = "png");
erase(currentpicture);
// currentpicture = nullpicture
write('fin',fichier);
// write('fin');
}
}