Results 1 to 6 of 6

Thread: error execution

  1. #1
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: error execution

    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
    Qt Code:
    1. #include "otsu.h"
    2. #include "fenetreprincipale.h"
    3.  
    4. FenetrePrincipale::FenetrePrincipale(): QMainWindow()
    5. {
    6.  
    7. label =new QLabel(&fentroi);
    8.  
    9. label->setGeometry(100,100,400,400);
    10. QMenu *menuFichier = menuBar()->addMenu("Fichier");
    11. QAction *actionOuvrir = new QAction("Ouvrir", this);
    12. QAction *actionQuitter = new QAction("Quitter", this);
    13. menuFichier->addAction(actionOuvrir);
    14. menuFichier->addAction(actionQuitter);
    15.  
    16. QMenu *menuTraitement = menuBar()->addMenu("Traitement");
    17. QAction *actionBinarisation = new QAction("Binarisation", this);
    18. menuTraitement->addAction(actionBinarisation);
    19.  
    20. QObject::connect(actionOuvrir, SIGNAL(triggered(bool)), this, SLOT(ouvrir()));
    21. QObject::connect(actionQuitter, SIGNAL(triggered(bool)), this, SLOT(close()));
    22.  
    23. QObject::connect(actionBinarisation, SIGNAL(triggered(bool)), this, SLOT(traiterOtsu()));
    24. }
    25. void FenetrePrincipale::ouvrir()
    26. {
    27. nomFichier = QFileDialog::getOpenFileName(this, tr("Open File"),"C:/Users/amine/Downloads");
    28.  
    29.  
    30.  
    31. image = new QImage(nomFichier);
    32. QPixmap pixmap;
    33. pixmap.convertFromImage(*image);
    34. label->setPixmap(pixmap);
    35. fentroi.show(); // fentroi (fenetre qui affiche l'image) attribut de fenetre principale
    36. label->update();
    37.  
    38. }
    39.  
    40. void FenetrePrincipale::traiterOtsu()
    41. {
    42. OtsuAlgo traitement(nomFichier);
    43. traitement.traiter(nomFichier);
    44. traitement.binarisation();
    45. QImage *im = new QImage(traitement.getImage());
    46. QLabel *label1 = new QLabel(&fenSec);
    47. label1->setGeometry(100,100, 400, 400);
    48. label1->setPixmap(QPixmap::fromImage(*im));
    49. QLCDNumber *lcd = new QLCDNumber(&fenSec);
    50. lcd->move(70,70);
    51. lcd->setSegmentStyle(QLCDNumber::Flat);
    52. lcd->display(traitement.getSeuil());
    53. fenSec.show();
    54. traitement.detection();
    55. //v = traitement.getCentres();
    56. /*for (int i=0;i<v.size();i++)
    57.   {
    58.   QPainter p;
    59.   p.begin(im);
    60.   p.drawPoint(v[i].getI(),v[i].getJ());
    61.   p.end();
    62.   }
    63. */
    64. }
    To copy to clipboard, switch view to plain text mode 
    fenetre.h
    Qt Code:
    1. #ifndef FENETREPRINCIPALE_H
    2. #define FENETREPRINCIPALE_H
    3.  
    4. #include <QtWidgets>
    5.  
    6. class FenetrePrincipale : public QMainWindow
    7. {
    8. Q_OBJECT
    9. public :
    10. FenetrePrincipale();
    11. public slots :
    12. void ouvrir();
    13. void traiterOtsu();
    14. private:
    15. QImage *image;
    16. QLabel *label;
    17. QString nomFichier;
    18. QWidget fentroi;
    19. QWidget fenSec;
    20.  
    21.  
    22.  
    23. };
    24.  
    25. #endif // FENETREPRINCIPALE_H
    To copy to clipboard, switch view to plain text mode 
    Otsu.h
    Qt Code:
    1. #ifndef OTSU_H
    2. #define OTSU_H
    3. #include <QtWidgets>
    4.  
    5. class Position
    6. {
    7. public:
    8. Position()
    9. {
    10. positionI = 0;
    11. positionJ = 0;
    12. nbr =0;
    13. }
    14. Position(int i, int j)
    15. {
    16. positionI = i;
    17. positionJ = j;
    18. }
    19. int getI()
    20. {return positionI;}
    21. int getJ()
    22. {return positionJ;}
    23. int getNbr()
    24. {return nbr;}
    25. void set(int i, int j, int nb)
    26. { positionI =i;
    27. positionJ =j;
    28. nbr =nb;
    29. }
    30. private:
    31. int positionI;
    32. int positionJ;
    33. int nbr;
    34. };
    35. class Composant
    36. {
    37. public:
    38. Composant(Position position)
    39. {
    40. p = position;
    41. }
    42. void set(Position p, int labell)
    43. {
    44. p = p;
    45. label =labell;
    46. }
    47.  
    48. private :
    49. Position p;
    50. int label;
    51. };
    52.  
    53. class OtsuAlgo
    54. {
    55. public:
    56. OtsuAlgo(QString s);
    57. void traiter(QString nom);
    58. void binarisation();
    59. QImage getImage();
    60. double getSeuil();
    61. void detection();
    62. //void chercher(int x, int y, int clabel);
    63. void centrer();
    64. QVector<Position> getCentres();
    65. void chercher(int x, int y, int clabel);
    66.  
    67.  
    68. private :
    69. QImage *image;
    70. QImage *procImage;
    71. QImage *clusterImage;
    72. QVector<Position> iwc;
    73. int labell;
    74. int n;
    75. int histog [256];
    76. double var;
    77. double q1;
    78. double q2;
    79. double u1;
    80. double u2;
    81. double sum;
    82. int somme;
    83. double sumB;
    84. double varMax;
    85. int seuil;
    86.  
    87. };
    88.  
    89. #endif // OTSU_H
    To copy to clipboard, switch view to plain text mode 
    otsu.cpp
    Qt Code:
    1. #include "otsu.h"
    2.  
    3. void OtsuAlgo::chercher(int x, int y, int clabel )
    4. {
    5. const int dx[] = {+1, 0, -1, 0};
    6. const int dy[] = {0, +1, 0, -1};
    7. if (x < 0 || x == procImage->height()) return; // out of bounds
    8. if (y < 0 || y == procImage->width()) return; // out of bounds
    9. if ((clusterImage->pixel(y,x)!= qRgb(0,0,0)) || (procImage->pixel(y,x)==qRgb(0,0,0)))
    10. return; // already labeled or not marked with 1 in m
    11.  
    12. // mark the current cell
    13. clusterImage->setPixel(y,x,qRgb(0,0,0));
    14. if(iwc.size()<clabel)
    15. {Position p;
    16. iwc.append(p);}
    17. 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);
    18.  
    19. // recursively mark the neighbors
    20. for (int i = 0; i < 4; i++)
    21. chercher(x + dx[i], y + dy[i], clabel);
    22. }
    23.  
    24. void OtsuAlgo::centrer()
    25. {
    26. for (int i =0; i<iwc.size(); i++)
    27. {
    28. iwc[i].set(iwc[i].getI()/iwc[i].getNbr(),iwc[i].getJ()/iwc[i].getNbr(),iwc[i].getNbr());
    29. }
    30. }
    31.  
    32. OtsuAlgo::OtsuAlgo(QString s):iwc(0)
    33. {
    34. seuil =0;
    35. image = new QImage(s);
    36. }
    37.  
    38. void OtsuAlgo::traiter(QString nom)
    39. {
    40. image->load(nom);
    41. // image->scaled(500,500,Qt::IgnoreAspectRatio);
    42. int gray;
    43. int S(0),N(0);
    44. int k,kStar;
    45. int N1,Sk(0);
    46. int L=256;
    47. double num, denom;
    48. double BCV, BCVmax;
    49.  
    50. for(int i=0; i< 256; i++)
    51. histog[i]= 0;
    52.  
    53. for(int i=0; i< image->height(); i++)
    54. {
    55. for(int j=0; j< image->width(); j++)
    56. {
    57. QRgb pal = image->pixel(j,i);
    58. gray = (qRed(pal)+ qGreen(pal) + qBlue(pal))/3;
    59. image->setPixel(j,i,qRgb(gray,gray,gray));
    60. }
    61. }
    62. //calcule de l'hist
    63. for (int i = 0; i< image->height();i++)
    64. {
    65. for(int j =0; j<image->width(); j++)
    66. {
    67.  
    68. QRgb pal = image->pixel(j,i);
    69. gray = (qRed(pal)+ qGreen(pal) + qBlue(pal))/3;
    70. histog[gray]= histog[gray]+1;
    71. }
    72. }
    73. for (k=0; k<L; k++){
    74. S += k * histog[k]; // Total histogram intensity
    75. N += histog[k]; // Total number of data points
    76. }
    77. N1 = histog[0]; // The entry for zero intensity
    78. BCV = 0;
    79. BCVmax=0;
    80. kStar = 0;
    81.  
    82. // Look at each possible threshold value,
    83. // calculate the between-class variance, and decide if it's a max
    84. for (k=1; k<L-1; k++) { // No need to check endpoints k = 0 or k = L-1
    85. Sk += k * histog[k];
    86. N1 += histog[k];
    87.  
    88. // The float casting here is to avoid compiler warning about loss of precision and
    89. // will prevent overflow in the case of large saturated images
    90. denom = (double)( N1) * (N - N1); // Maximum value of denom is (N^2)/4 = approx. 3E10
    91.  
    92. if (denom != 0 ){
    93. // Float here is to avoid loss of precision when dividing
    94. num = ( (double)N1 / N ) * S - Sk; // Maximum value of num = 255*N = approx 8E7
    95. BCV = (num * num) / denom;
    96. }
    97. else
    98. BCV = 0;
    99.  
    100. if (BCV >= BCVmax){ // Assign the best threshold found so far
    101. BCVmax = BCV;
    102. kStar = k;
    103. }
    104. }
    105. seuil = kStar;
    106.  
    107. }
    108.  
    109.  
    110. void OtsuAlgo::binarisation()
    111. {
    112. procImage = new QImage(*image);
    113.  
    114. for(int i=0; i< image->height(); i++)
    115. {
    116. for(int j=0; j< image->width(); j++)
    117. {
    118. if(image->pixel(j,i) < qRgb(seuil,seuil,seuil))
    119. {
    120. procImage->setPixel(j,i,qRgb(255,255,255));
    121. }
    122. else
    123. procImage->setPixel(j,i,qRgb(0,0,0));
    124. }
    125. }
    126. }
    127. QImage OtsuAlgo::getImage()
    128. {
    129. return (*procImage);
    130. }
    131. double OtsuAlgo::getSeuil()
    132. {
    133. return seuil;
    134. }
    135. void OtsuAlgo::detection()
    136. {
    137. clusterImage = new QImage(*procImage);
    138. for (int i =0; i<procImage->height(); i++)
    139. for(int j =0; j<procImage->height(); j++)
    140. {
    141. clusterImage->setPixel(j,i,qRgb(0,0,0));
    142. }
    143.  
    144. labell = 0;
    145. for (int i = 0; i <procImage->height(); i++)
    146. {for (int j = 0; j <procImage->width(); j++)
    147. {if (clusterImage->pixel(j,i)==qRgb(0,0,0) && procImage->pixel(j,i)==qRgb(255,255,255))
    148. {labell++;
    149. chercher(i, j, labell);
    150. }}}
    151. n++;
    152. }
    153. QVector<Position> OtsuAlgo::getCentres()
    154. {
    155. return iwc;
    156. }
    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

    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)))
    Last edited by rafik; 28th January 2016 at 16:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: error execution

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    rafik (28th January 2016)

  4. #3
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: error execution

    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: error execution

    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,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    rafik (28th January 2016)

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: error execution

    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.

  8. The following user says thank you to ChrisW67 for this useful post:

    rafik (28th January 2016)

  9. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: error execution

    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. error in program execution
    By qtlearner123 in forum Newbie
    Replies: 2
    Last Post: 24th April 2012, 08:43
  2. error while execution duplicate connection name
    By khadija123 in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2012, 14:59
  3. Replies: 2
    Last Post: 9th February 2011, 09:33
  4. Execution error
    By Shuchi Agrawal in forum Installation and Deployment
    Replies: 10
    Last Post: 9th February 2007, 12:12
  5. Qtopia example execution error(application hangs)
    By devendra in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 17th October 2006, 09:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.