PDA

View Full Version : error execution



rafik
28th January 2016, 16:09
hello, i have an error and i can't understand where is it?
when i execute my program it craches when i click on binarisation().

if some one can help me

here the code
fenetrePrincipale.cpp


#include "otsu.h"
#include "fenetreprincipale.h"

FenetrePrincipale::FenetrePrincipale(): QMainWindow()
{

label =new QLabel(&fentroi);

label->setGeometry(100,100,400,400);
QMenu *menuFichier = menuBar()->addMenu("Fichier");
QAction *actionOuvrir = new QAction("Ouvrir", this);
QAction *actionQuitter = new QAction("Quitter", this);
menuFichier->addAction(actionOuvrir);
menuFichier->addAction(actionQuitter);

QMenu *menuTraitement = menuBar()->addMenu("Traitement");
QAction *actionBinarisation = new QAction("Binarisation", this);
menuTraitement->addAction(actionBinarisation);

QObject::connect(actionOuvrir, SIGNAL(triggered(bool)), this, SLOT(ouvrir()));
QObject::connect(actionQuitter, SIGNAL(triggered(bool)), this, SLOT(close()));

QObject::connect(actionBinarisation, SIGNAL(triggered(bool)), this, SLOT(traiterOtsu()));
}
void FenetrePrincipale::ouvrir()
{
nomFichier = QFileDialog::getOpenFileName(this, tr("Open File"),"C:/Users/amine/Downloads");



image = new QImage(nomFichier);
QPixmap pixmap;
pixmap.convertFromImage(*image);
label->setPixmap(pixmap);
fentroi.show(); // fentroi (fenetre qui affiche l'image) attribut de fenetre principale
label->update();

}

void FenetrePrincipale::traiterOtsu()
{
OtsuAlgo traitement(nomFichier);
traitement.traiter(nomFichier);
traitement.binarisation();
QImage *im = new QImage(traitement.getImage());
QLabel *label1 = new QLabel(&fenSec);
label1->setGeometry(100,100, 400, 400);
label1->setPixmap(QPixmap::fromImage(*im));
QLCDNumber *lcd = new QLCDNumber(&fenSec);
lcd->move(70,70);
lcd->setSegmentStyle(QLCDNumber::Flat);
lcd->display(traitement.getSeuil());
fenSec.show();
traitement.detection();
//v = traitement.getCentres();
/*for (int i=0;i<v.size();i++)
{
QPainter p;
p.begin(im);
p.drawPoint(v[i].getI(),v[i].getJ());
p.end();
}
*/
}

fenetre.h


#ifndef FENETREPRINCIPALE_H
#define FENETREPRINCIPALE_H

#include <QtWidgets>

class FenetrePrincipale : public QMainWindow
{
Q_OBJECT
public :
FenetrePrincipale();
public slots :
void ouvrir();
void traiterOtsu();
private:
QImage *image;
QLabel *label;
QString nomFichier;
QWidget fentroi;
QWidget fenSec;



};

#endif // FENETREPRINCIPALE_H

Otsu.h


#ifndef OTSU_H
#define OTSU_H
#include <QtWidgets>

class Position
{
public:
Position()
{
positionI = 0;
positionJ = 0;
nbr =0;
}
Position(int i, int j)
{
positionI = i;
positionJ = j;
}
int getI()
{return positionI;}
int getJ()
{return positionJ;}
int getNbr()
{return nbr;}
void set(int i, int j, int nb)
{ positionI =i;
positionJ =j;
nbr =nb;
}
private:
int positionI;
int positionJ;
int nbr;
};
class Composant
{
public:
Composant(Position position)
{
p = position;
}
void set(Position p, int labell)
{
p = p;
label =labell;
}

private :
Position p;
int label;
};

class OtsuAlgo
{
public:
OtsuAlgo(QString s);
void traiter(QString nom);
void binarisation();
QImage getImage();
double getSeuil();
void detection();
//void chercher(int x, int y, int clabel);
void centrer();
QVector<Position> getCentres();
void chercher(int x, int y, int clabel);


private :
QImage *image;
QImage *procImage;
QImage *clusterImage;
QVector<Position> iwc;
int labell;
int n;
int histog [256];
double var;
double q1;
double q2;
double u1;
double u2;
double sum;
int somme;
double sumB;
double varMax;
int seuil;

};

#endif // OTSU_H


otsu.cpp


#include "otsu.h"

void OtsuAlgo::chercher(int x, int y, int clabel )
{
const int dx[] = {+1, 0, -1, 0};
const int dy[] = {0, +1, 0, -1};
if (x < 0 || x == procImage->height()) return; // out of bounds
if (y < 0 || y == procImage->width()) return; // out of bounds
if ((clusterImage->pixel(y,x)!= qRgb(0,0,0)) || (procImage->pixel(y,x)==qRgb(0,0,0)))
return; // already labeled or not marked with 1 in m

// mark the current cell
clusterImage->setPixel(y,x,qRgb(0,0,0));
if(iwc.size()<clabel)
{Position p;
iwc.append(p);}
iwc[clabel - 1].set((iwc[clabel - 1].getI() + procImage->pixel(y,x)*x),iwc[clabel - 1].getJ() + (procImage->pixel(y,x)*y),iwc[clabel - 1].getNbr()+1);

// recursively mark the neighbors
for (int i = 0; i < 4; i++)
chercher(x + dx[i], y + dy[i], clabel);
}

void OtsuAlgo::centrer()
{
for (int i =0; i<iwc.size(); i++)
{
iwc[i].set(iwc[i].getI()/iwc[i].getNbr(),iwc[i].getJ()/iwc[i].getNbr(),iwc[i].getNbr());
}
}

OtsuAlgo::OtsuAlgo(QString s):iwc(0)
{
seuil =0;
image = new QImage(s);
}

void OtsuAlgo::traiter(QString nom)
{
image->load(nom);
// image->scaled(500,500,Qt::IgnoreAspectRatio);
int gray;
int S(0),N(0);
int k,kStar;
int N1,Sk(0);
int L=256;
double num, denom;
double BCV, BCVmax;

for(int i=0; i< 256; i++)
histog[i]= 0;

for(int i=0; i< image->height(); i++)
{
for(int j=0; j< image->width(); j++)
{
QRgb pal = image->pixel(j,i);
gray = (qRed(pal)+ qGreen(pal) + qBlue(pal))/3;
image->setPixel(j,i,qRgb(gray,gray,gray));
}
}
//calcule de l'hist
for (int i = 0; i< image->height();i++)
{
for(int j =0; j<image->width(); j++)
{

QRgb pal = image->pixel(j,i);
gray = (qRed(pal)+ qGreen(pal) + qBlue(pal))/3;
histog[gray]= histog[gray]+1;
}
}
for (k=0; k<L; k++){
S += k * histog[k]; // Total histogram intensity
N += histog[k]; // Total number of data points
}
N1 = histog[0]; // The entry for zero intensity
BCV = 0;
BCVmax=0;
kStar = 0;

// Look at each possible threshold value,
// calculate the between-class variance, and decide if it's a max
for (k=1; k<L-1; k++) { // No need to check endpoints k = 0 or k = L-1
Sk += k * histog[k];
N1 += histog[k];

// The float casting here is to avoid compiler warning about loss of precision and
// will prevent overflow in the case of large saturated images
denom = (double)( N1) * (N - N1); // Maximum value of denom is (N^2)/4 = approx. 3E10

if (denom != 0 ){
// Float here is to avoid loss of precision when dividing
num = ( (double)N1 / N ) * S - Sk; // Maximum value of num = 255*N = approx 8E7
BCV = (num * num) / denom;
}
else
BCV = 0;

if (BCV >= BCVmax){ // Assign the best threshold found so far
BCVmax = BCV;
kStar = k;
}
}
seuil = kStar;

}


void OtsuAlgo::binarisation()
{
procImage = new QImage(*image);

for(int i=0; i< image->height(); i++)
{
for(int j=0; j< image->width(); j++)
{
if(image->pixel(j,i) < qRgb(seuil,seuil,seuil))
{
procImage->setPixel(j,i,qRgb(255,255,255));
}
else
procImage->setPixel(j,i,qRgb(0,0,0));
}
}
}
QImage OtsuAlgo::getImage()
{
return (*procImage);
}
double OtsuAlgo::getSeuil()
{
return seuil;
}
void OtsuAlgo::detection()
{
clusterImage = new QImage(*procImage);
for (int i =0; i<procImage->height(); i++)
for(int j =0; j<procImage->height(); j++)
{
clusterImage->setPixel(j,i,qRgb(0,0,0));
}

labell = 0;
for (int i = 0; i <procImage->height(); i++)
{for (int j = 0; j <procImage->width(); j++)
{if (clusterImage->pixel(j,i)==qRgb(0,0,0) && procImage->pixel(j,i)==qRgb(255,255,255))
{labell++;
chercher(i, j, labell);
}}}
n++;
}
QVector<Position> OtsuAlgo::getCentres()
{
return iwc;
}

i am note familiar with debug but when i clic on compilation with debug that demonstrates that the problem is in the chercher() method

thank you

i have a project for tomorrow if some one can help me please,

Added after 40 minutes:

are there any problem in this line ?
if ((clusterImage->pixel(y,x)!= qRgb(0,0,0)) || (procImage->pixel(y,x)==qRgb(0,0,0)))

anda_skoa
28th January 2016, 16:20
Hard to tell.
Is procImage a valid pointer?

Btw, repeating myself from several other of your threads: don't allocate QImage on the heap.

Cheers,
_

rafik
28th January 2016, 17:28
Thank you Mr anda, i did a mistake (in the programming qRgb(0,0,0)) but i correct it,

for the QImage i will try to use one QImage in the future
thank you.

anda_skoa
28th January 2016, 19:50
You can use as many images as you like, but my guess is you don't want to leak memory.
So either delete when appropriate or avoid the whole issue by not allocating the image on the heap.

Cheers,
_

ChrisW67
28th January 2016, 20:37
i am note familiar with debug but when i clic on compilation with debug that demonstrates that the problem is in the chercher() method

If the problem is showing during compilation then:

The program is not crashing, just failing to compile.
you need to read and post the actual error message provided by your compiler.

If the problem happens when you run the compiled (debug) executable the you need to read and post the debugger backtrace after it crashes when running in your debugger. This will tell you exactly which line it died on and let you inspect the state of variables at that time.

In all likelihood either (or both) procImage or clusterImage is an uninitialised, null, or invalid pointer. Anda_skoa's suggestion to not allocate the QImage on the heap is likely the best solution to your problem... Not just a nice idea.

jefftee
29th January 2016, 03:07
You also use a pointer named "image" on like 112, which if null or pointing to memory that has already been freed will cause a crash. Set a breakpoint on that line and check the value of that pointer.

You should get in the habit of checking pointers for nullptr before using them and setting them to nullptr when freed, etc.