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
|
#include "mainwin.h"
#include <QVector>
#include "math.h"
MainWin::MainWin()
{
QWidget *zoneCentrale = new QWidget();
layout = new QVBoxLayout(this);
ok = new QPushButton("Charger image",this);
fourier = new QPushButton("calcule fourier");
sauvgarderIm = new QPushButton("Sauvgarder", this);
label = new QLabel(this);
table = new QStandardItemModel(90, 90);
view = new QTableView;
layout->addWidget(ok);
layout->addWidget(fourier);
layout->addWidget(sauvgarderIm);
layout->addWidget(label);
layout->addWidget(&label2);
layout->addWidget(view);
zoneCentrale->setLayout(layout);
setCentralWidget(zoneCentrale);
QObject::connect(ok, SIGNAL(clicked(bool)), this, SLOT(afficher()));
connect(fourier, SIGNAL(clicked(bool)), this, SLOT(fourierTransform()));
connect(sauvgarderIm, SIGNAL(clicked(bool)), this, SLOT(sauvgarder()));
}
void MainWin::freeTab(double **&matrix, int n)
{
for(int i=0; i<n; i++)
delete[] matrix[i];
delete[] matrix;
}
void MainWin::afficher()
{
myImage = QFileDialog::getOpenFileName(this, "Choisir image","C:/Users/amine/Desktop");
label->setPixmap(QPixmap(myImage));
}
void MainWin::sauvgarder()
{
fileName = QFileDialog::getSaveFileName(this,"Choisir dossier","","Images(*.jpg)");
label2.setText(fileName);
image->save(fileName);
}
double MainWin::max(double ** const &matrix, int h, int w)
{
double max = 0.0;
for(int i=0; i<h; i++)
for(int j=0; j<w; j++){
if (matrix[i][j] > max)
max = matrix[i][j];
}
return max;
}
double MainWin::min(double ** const &matrix, int h, int w)
{
double min = 0.0;
for(int i=0; i<h; i++)
for(int j=0; j<w; j++){
if (matrix[i][j] < min)
min = matrix[i][j];
}
return min;
}
double MainWin::logConstant(double m)
{
double c;
c = 255 / log(1 + abs(m));
return c;
}
void MainWin::normalization(double **&grayLev, int h, int w, double min, double max)
{
for(int i=0.0; i<h; i++)
for(int j=0.0; j<w; j++)
grayLev[i][j] = (grayLev[i][j] - min)*((255-0)/(max-min))+0;
}
void MainWin::fourierTransform()
{
image = new QImage(myImage);
// matrices needed
double sumRe=0;
double sumI=0;
double angle;
double **fourierR;
double **fourierI;
double **fourierMag;
fourierR = new double *[image->height()];
for(int i=0; i<image->height(); i++)
fourierR[i] = new double[image->width()];
fourierI = new double *[image->height()];
for(int i=0; i<image->height(); i++)
fourierI[i] = new double[image->width()];
// computation of fourier transform
for(int i=0; i<image->height();i++){
for(int j=0; j<image->width();j++){
for(int h=0; h<image->height(); h++){
for(int l=0; l<image->width(); l++){
int grayLev = (qRed(image->pixel(l,h)) + qBlue(image->pixel(l,h)) + qGreen(image->pixel(l,h)))/3;
sumRe += (double)grayLev * cos(-2*M_PI*(((double)i*(double)h/(double)image->height()) + ((double)j*(double)l/(double)image->width())));
sumI += (double)grayLev * sin(-2*M_PI*(((double)i*(double)h/(double)image->height()) + ((double)j*(double)l/(double)image->width())));
}
}
fourierR[i][j] = sumRe ;
fourierI[i][j] = sumI ;
sumRe = 0.0;
sumI = 0.0;
}
}
//computing the magnitude
fourierMag = new double *[image->height()];
for (int i=0; i<image->height();i++)
fourierMag[i] = new double[image->width()];
for(int i=0; i<image->height();i++)
for(int j=0; j<image->width();j++){
fourierMag[i][j] = sqrt((fourierR[i][j]*fourierR[i][j])+(fourierI[i][j]*fourierI[i][j]));
}
// application of the logaritmic transformation on the magnitude of the image
for(int i=0; i<image->height();i++){
for(int j=0; j<image->width();j++){
//double maxValue = max(fourierMag, image->height(), image->width());
fourierMag[i][j] = log(1 + abs(fourierMag[i][j]));
}
}
// Centrer les fréquences Zéros
for(int i=0; i<image->height();i++){
for(int j=0; j<image->width();j++){
//double maxValue = max(fourierMag, image->height(), image->width());
fourierMag[i][j] *= pow(-1.0,(double)i+j);
}
}
double maxi = max(fourierMag, image->height(), image->width());
double mini = min(fourierMag, image->height(), image->width());
normalization(fourierMag, image->height(), image->width(), mini, maxi);
for(int i=0; i<image->height();i++)
for(int j=0; j<image->width();j++)
image->setPixel(j, i, qRgb(fourierMag[i][j],fourierMag[i][j],fourierMag[i][j]));
// displaying the image on a QLabel
QPixmap pixmap;
pixmap.convertFromImage(*image);
label->setPixmap(pixmap);
for(int i=0; i<90; i++)
for(int j=0; j<90; j++)
table->setItem(i, j, new QStandardItem(QString::number(fourierMag[i][j])));
view->setModel(table);
freeTab(fourierR, image->height());
freeTab(fourierI, image->height());
freeTab(fourierMag, image->height());
} |
Partager