Accueil > > > TRANSFORMÉE DE FOURIER RAPIDE 2D POUR LES IMAGES
TRANSFORMÉE DE FOURIER RAPIDE 2D POUR LES IMAGES
Information sur la source
Description
Très utile en traitement d'image et analyse d'image Utiliser avec la classe CxImage que vous pouvez trouver au : http://www.xdp.it/
Source
- bool CxImage::FFT2(CxImage* srcReal, CxImage* srcImag, CxImage* dstReal, CxImage* dstImag,
- long direction, bool bForceFFT, bool bMagnitude)
- {
- //check if there is something to convert
- if (srcReal==NULL && srcImag==NULL) return false;
-
- long w,h;
- //get width and height
- if (srcReal) {
- w=srcReal->GetWidth();
- h=srcReal->GetHeight();
- } else {
- w=srcImag->GetWidth();
- h=srcImag->GetHeight();
- }
-
- bool bXpow2 = IsPowerof2(w);
- bool bYpow2 = IsPowerof2(h);
- //if bForceFFT, width AND height must be powers of 2
- if (bForceFFT && !(bXpow2 && bYpow2)) {
- long i;
-
- i=0;
- while((1<<i)<w) i++;
- w=1<<i;
- bXpow2=true;
-
- i=0;
- while((1<<i)<h) i++;
- h=1<<i;
- bYpow2=true;
- }
-
- // I/O images for FFT
- CxImage *tmpReal,*tmpImag;
-
- // select output
- tmpReal = (dstReal) ? dstReal : srcReal;
- tmpImag = (dstImag) ? dstImag : srcImag;
-
- // src!=dst -> copy the image
- if (srcReal && dstReal) tmpReal->Copy(*srcReal,true,false,false);
- if (srcImag && dstImag) tmpImag->Copy(*srcImag,true,false,false);
-
- // dst&&src are empty -> create new one, else turn to GrayScale
- if (srcReal==0 && dstReal==0){
- tmpReal = new CxImage(w,h,8);
- tmpReal->Clear(0);
- tmpReal->SetGrayPalette();
- } else {
- if (!tmpReal->IsGrayScale()) tmpReal->GrayScale();
- }
- if (srcImag==0 && dstImag==0){
- tmpImag = new CxImage(w,h,8);
- tmpImag->Clear(0);
- tmpImag->SetGrayPalette();
- } else {
- if (!tmpImag->IsGrayScale()) tmpImag->GrayScale();
- }
-
- if (!(tmpReal->IsValid() && tmpImag->IsValid())){
- if (srcReal==0 && dstReal==0) delete tmpReal;
- if (srcImag==0 && dstImag==0) delete tmpImag;
- return false;
- }
-
- //resample for FFT, if necessary
- tmpReal->Resample(w,h,0);
- tmpImag->Resample(w,h,0);
-
- //ok, here we have 2 (w x h), grayscale images ready for a FFT
-
- double* real;
- double* imag;
- long j,k,m;
-
- _complex **grid;
- //double mean = tmpReal->Mean();
- /* Allocate memory for the grid */
- grid = (_complex **)malloc(w * sizeof(_complex));
- for (k=0;k<w;k++) {
- grid[k] = (_complex *)malloc(h * sizeof(_complex));
- }
- for (j=0;j<h;j++) {
- for (k=0;k<w;k++) {
- grid[k][j].x = tmpReal->GetPixelIndex(k,j)-128;
- grid[k][j].y = tmpImag->GetPixelIndex(k,j)-128;
- }
- }
-
- //DFT buffers
- double *real2,*imag2;
- real2 = (double*)malloc(max(w,h) * sizeof(double));
- imag2 = (double*)malloc(max(w,h) * sizeof(double));
-
- /* Transform the rows */
- real = (double *)malloc(w * sizeof(double));
- imag = (double *)malloc(w * sizeof(double));
-
- m=0;
- while((1<<m)<w) m++;
-
- for (j=0;j<h;j++) {
- for (k=0;k<w;k++) {
- real[k] = grid[k][j].x;
- imag[k] = grid[k][j].y;
- }
-
- if (bXpow2) FFT(direction,m,real,imag);
- else DFT(direction,w,real,imag,real2,imag2);
-
- for (k=0;k<w;k++) {
- grid[k][j].x = real[k];
- grid[k][j].y = imag[k];
- }
- }
- free(real);
- free(imag);
-
- /* Transform the columns */
- real = (double *)malloc(h * sizeof(double));
- imag = (double *)malloc(h * sizeof(double));
-
- m=0;
- while((1<<m)<h) m++;
-
- for (k=0;k<w;k++) {
- for (j=0;j<h;j++) {
- real[j] = grid[k][j].x;
- imag[j] = grid[k][j].y;
- }
-
- if (bYpow2) FFT(direction,m,real,imag);
- else DFT(direction,h,real,imag,real2,imag2);
-
- for (j=0;j<h;j++) {
- grid[k][j].x = real[j];
- grid[k][j].y = imag[j];
- }
- }
- free(real);
- free(imag);
-
- free(real2);
- free(imag2);
-
- /* converting from double to byte, there is a HUGE loss in the dynamics
- "nn" tries to keep an acceptable SNR, but 8bit=48dB: don't ask more */
- double nn=pow((double)2,(double)log((double)max(w,h))/(double)log((double)2)-4);
- //reversed gain for reversed transform
- if (direction==-1) nn=1/nn;
- //bMagnitude : just to see it on the screen
- if (bMagnitude) nn*=4;
-
- for (j=0;j<h;j++) {
- for (k=0;k<w;k++) {
- if (bMagnitude){
- tmpReal->SetPixelIndex(k,j,(BYTE)max(0,min(255,(nn*(3+log(_cabs(grid[k][j])))))));
- if (grid[k][j].x==0){
- tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128+(atan(grid[k][j].y/0.0000000001)*nn)))));
- } else {
- tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128+(atan(grid[k][j].y/grid[k][j].x)*nn)))));
- }
- } else {
- tmpReal->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128 + grid[k][j].x*nn))));
- tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128 + grid[k][j].y*nn))));
- }
- }
- }
-
- for (k=0;k<w;k++) free (grid[k]);
- free (grid);
-
- if (srcReal==0 && dstReal==0) delete tmpReal;
- if (srcImag==0 && dstImag==0) delete tmpImag;
-
- return true;
- }
bool CxImage::FFT2(CxImage* srcReal, CxImage* srcImag, CxImage* dstReal, CxImage* dstImag,
long direction, bool bForceFFT, bool bMagnitude)
{
//check if there is something to convert
if (srcReal==NULL && srcImag==NULL) return false;
long w,h;
//get width and height
if (srcReal) {
w=srcReal->GetWidth();
h=srcReal->GetHeight();
} else {
w=srcImag->GetWidth();
h=srcImag->GetHeight();
}
bool bXpow2 = IsPowerof2(w);
bool bYpow2 = IsPowerof2(h);
//if bForceFFT, width AND height must be powers of 2
if (bForceFFT && !(bXpow2 && bYpow2)) {
long i;
i=0;
while((1<<i)<w) i++;
w=1<<i;
bXpow2=true;
i=0;
while((1<<i)<h) i++;
h=1<<i;
bYpow2=true;
}
// I/O images for FFT
CxImage *tmpReal,*tmpImag;
// select output
tmpReal = (dstReal) ? dstReal : srcReal;
tmpImag = (dstImag) ? dstImag : srcImag;
// src!=dst -> copy the image
if (srcReal && dstReal) tmpReal->Copy(*srcReal,true,false,false);
if (srcImag && dstImag) tmpImag->Copy(*srcImag,true,false,false);
// dst&&src are empty -> create new one, else turn to GrayScale
if (srcReal==0 && dstReal==0){
tmpReal = new CxImage(w,h,8);
tmpReal->Clear(0);
tmpReal->SetGrayPalette();
} else {
if (!tmpReal->IsGrayScale()) tmpReal->GrayScale();
}
if (srcImag==0 && dstImag==0){
tmpImag = new CxImage(w,h,8);
tmpImag->Clear(0);
tmpImag->SetGrayPalette();
} else {
if (!tmpImag->IsGrayScale()) tmpImag->GrayScale();
}
if (!(tmpReal->IsValid() && tmpImag->IsValid())){
if (srcReal==0 && dstReal==0) delete tmpReal;
if (srcImag==0 && dstImag==0) delete tmpImag;
return false;
}
//resample for FFT, if necessary
tmpReal->Resample(w,h,0);
tmpImag->Resample(w,h,0);
//ok, here we have 2 (w x h), grayscale images ready for a FFT
double* real;
double* imag;
long j,k,m;
_complex **grid;
//double mean = tmpReal->Mean();
/* Allocate memory for the grid */
grid = (_complex **)malloc(w * sizeof(_complex));
for (k=0;k<w;k++) {
grid[k] = (_complex *)malloc(h * sizeof(_complex));
}
for (j=0;j<h;j++) {
for (k=0;k<w;k++) {
grid[k][j].x = tmpReal->GetPixelIndex(k,j)-128;
grid[k][j].y = tmpImag->GetPixelIndex(k,j)-128;
}
}
//DFT buffers
double *real2,*imag2;
real2 = (double*)malloc(max(w,h) * sizeof(double));
imag2 = (double*)malloc(max(w,h) * sizeof(double));
/* Transform the rows */
real = (double *)malloc(w * sizeof(double));
imag = (double *)malloc(w * sizeof(double));
m=0;
while((1<<m)<w) m++;
for (j=0;j<h;j++) {
for (k=0;k<w;k++) {
real[k] = grid[k][j].x;
imag[k] = grid[k][j].y;
}
if (bXpow2) FFT(direction,m,real,imag);
else DFT(direction,w,real,imag,real2,imag2);
for (k=0;k<w;k++) {
grid[k][j].x = real[k];
grid[k][j].y = imag[k];
}
}
free(real);
free(imag);
/* Transform the columns */
real = (double *)malloc(h * sizeof(double));
imag = (double *)malloc(h * sizeof(double));
m=0;
while((1<<m)<h) m++;
for (k=0;k<w;k++) {
for (j=0;j<h;j++) {
real[j] = grid[k][j].x;
imag[j] = grid[k][j].y;
}
if (bYpow2) FFT(direction,m,real,imag);
else DFT(direction,h,real,imag,real2,imag2);
for (j=0;j<h;j++) {
grid[k][j].x = real[j];
grid[k][j].y = imag[j];
}
}
free(real);
free(imag);
free(real2);
free(imag2);
/* converting from double to byte, there is a HUGE loss in the dynamics
"nn" tries to keep an acceptable SNR, but 8bit=48dB: don't ask more */
double nn=pow((double)2,(double)log((double)max(w,h))/(double)log((double)2)-4);
//reversed gain for reversed transform
if (direction==-1) nn=1/nn;
//bMagnitude : just to see it on the screen
if (bMagnitude) nn*=4;
for (j=0;j<h;j++) {
for (k=0;k<w;k++) {
if (bMagnitude){
tmpReal->SetPixelIndex(k,j,(BYTE)max(0,min(255,(nn*(3+log(_cabs(grid[k][j])))))));
if (grid[k][j].x==0){
tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128+(atan(grid[k][j].y/0.0000000001)*nn)))));
} else {
tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128+(atan(grid[k][j].y/grid[k][j].x)*nn)))));
}
} else {
tmpReal->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128 + grid[k][j].x*nn))));
tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128 + grid[k][j].y*nn))));
}
}
}
for (k=0;k<w;k++) free (grid[k]);
free (grid);
if (srcReal==0 && dstReal==0) delete tmpReal;
if (srcImag==0 && dstImag==0) delete tmpImag;
return true;
}
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|