#include "otsu.h"
#include "fenetreprincipale.h"
{
label->setGeometry(100,100,400,400);
QMenu *menuFichier
= menuBar
()->addMenu
("Fichier");
menuFichier->addAction(actionOuvrir);
menuFichier->addAction(actionQuitter);
QMenu *menuTraitement
= menuBar
()->addMenu
("Traitement");
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
);
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();
label1->setGeometry(100,100, 400, 400);
label1
->setPixmap
(QPixmap::fromImage(*im
));
lcd->move(70,70);
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();
}
*/
}
#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();
}
*/
}
To copy to clipboard, switch view to plain text mode
fenetre.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:
void binarisation();
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 :
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
#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
To copy to clipboard, switch view to plain text mode
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;
}
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));
}
}
}
{
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
i am note familiar with debug but when i clic on compilation with debug that demonstrates that the problem is in the chercher() method
Bookmarks