PDA

View Full Version : computation problem



rafik
16th October 2016, 12:05
Hello everone,
please if someone can help me, i want to compute the fourier transform of an image and display it in a Qtable view, but i don't know why it dosn't give an image that i expected. it give me a white image

here the .cpp


#include "fenprin.h"
#include <QVector>
#include "math.h"

FenPrin::FenPrin()
{
QWidget *zoneCentrale = new QWidget();
layout = new QVBoxLayout(this);
ok = new QPushButton("Charger image",this);
histogram = new QPushButton("calculer histogramme");
fourier = new QPushButton("calcule fourier");
label = new QLabel(this);
table = new QStandardItemModel(90, 90);
view = new QTableView;
layout->addWidget(ok);
// layout->addWidget(histogram);
layout->addWidget(fourier);
layout->addWidget(label);
layout->addWidget(&label2);
layout->addWidget(view);
zoneCentrale->setLayout(layout);
setCentralWidget(zoneCentrale);
QObject::connect(ok, SIGNAL(clicked(bool)), this, SLOT(afficher()));
//connect(histogram, SIGNAL(clicked(bool)), this, SLOT(histograme()));
connect(fourier, SIGNAL(clicked(bool)), this, SLOT(fourierTransform()));
}
void FenPrin::afficher()
{
myImage = QFileDialog::getOpenFileName(this, "Choisir image","C:/Users/amine/Desktop");
label->setPixmap(QPixmap(myImage));
}
double FenPrin::max(double **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 FenPrin::logOperator(double m)
{
double c;
c = 255 / log(1 + abs(m));
return c;
}

void FenPrin::histograme()
{
int i = 0,j =0;
int grayLev;
int *hist;
hist = new int[256];
for(int i = 0; i<256; i++)
hist[i] = 0;

while(i < image->height()){
while(j < image->width())
{
grayLev = (qRed(image->pixel(j,i)) + qBlue(image->pixel(j,i)) + qGreen(image->pixel(j,i)))/3;
hist[grayLev] += 1;
j++;
}
i++;
}
// paindre sur une image
for(int i=0; i<256; i++)
table->setItem(0, i, new QStandardItem(QString::number(hist[i])));
label2.setText(QString::number(hist[0]));
view->setModel(table);
// déclaration d'un tableau dynamique à 2 dimensions

QPixmap pixmap;
pixmap.convertFromImage(*image);
label->setPixmap(pixmap);

// afficher l'histogramme
}
void FenPrin::fourierTransform()
{
image = new QImage(myImage);
double sumRe=0, sumI=0;
double sumR[90];
double sumIm[90];
double angle;
double **fourierR;
double **fourierI;
double **fourierMag;
double *fourier1D;
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()];
// calcule de la transforme de fourier

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;
angle = (i*h)/image->height() + (j*l)/image->width();
sumRe += (grayLev) * cos(-2*M_PI*angle);
sumI += (grayLev) * sin(-2*M_PI*angle);
}
}
fourierR[i][j] = sumRe;
fourierI[i][j] = sumI;
sumRe = 0;
sumI = 0;
}
}


for(int i=0; i<image->height();i++)
for(int l=0; l<image->height();l++)
{
int grayLev = (qRed(image->pixel(l,i)) + qBlue(image->pixel(l,i)) + qGreen(image->pixel(l,i)))/3;
sumRe += (grayLev) * cos(-2*M_PI*i*l);
}

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(pow(fourierR[i][j],2) + pow(fourierI[i][j],2));
double maxi = max(fourierMag, image->height(), image->width());

for(int i=0; i<image->height();i++)
for(int j=0; j<image->width();j++)
fourierMag[i][j] = logOperator(maxi) * log(1 + abs(fourierMag[i][j]));

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]));

QPixmap pixmap;
pixmap.convertFromImage(*image);
label->setPixmap(pixmap);
label2.setText(QString::number(fourierMag[0][0]));
for(int i=0; i<90; i++)
for( int j=0; j< image->width(); j++)
table->setItem(i, j, new QStandardItem(QString::number(fourierMag[i][j])));
view->setModel(table);
}


thank you.

d_stranz
26th October 2016, 21:38
image->setPixel(j, i, qRgb(fourierMag[i][j],fourierMag[i][j],fourierMag[i][j]));

qRgb() expects integer arguments in the range 0 - 255. You are giving it float arguments, and I doubt these are in that same range.