Results 1 to 5 of 5

Thread: Select pixel from image

  1. #1
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question Select pixel from image

    Hi!
    Firt of all, sorry for my poor English. I'm new in QT.

    I open an image in a QLabel: imageLabel->setPixmap(QPixmap::fromImage(image));

    This works well, but then I would like to add a mouse event. When the mouse was cliked in the image returns the pixel (of the image) in coordenates (X,Y). ¿It's possible?

    I read that the QLabel class could not get a mouseClicked. Do I have to use another class? ¿Do I have to use the class Graphics View?

    I'm using the example code of QT 4.7.2 called "Imageviewer". I'm programming in Windows with Eclipse Helios Release 2 (CDT).

    Thank you very very much!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Select pixel from image

    Your English is just fine, I have seen much worse here, believe me!
    I read that the QLabel class could not get a mouseClicked.
    Where did you read that?

    Do I have to use another class?
    In your code you are setting a QPixmap in your QLabel.
    The QPixmap is the object that is representing your Image.
    You can either:
    1. install an event filter on the QPixmap to extract the mouse position during a mousePressEvent()
    2. Subclass QPixmap and override the mousePressEvent().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    sergio87 (26th April 2011)

  4. #3
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Select pixel from image

    First of all, thanks for answer!!

    I tried to subclass QPixmap and override the mousePressEvent(). But nothing happened. I'll explain:

    I subclass the QPixmap like this (in imageviewer.h):
    Qt Code:
    1. class SubQPixmap : public QPixmap
    2. {
    3. public:
    4. void mousePressEvent(QMouseEvent *event);
    5. };
    To copy to clipboard, switch view to plain text mode 

    Then, I put this code in imageviewer.cpp:
    Qt Code:
    1. void SubQPixmap::mousePressEvent(QMouseEvent *event)
    2. {
    3. cout<<"entra"<<endl;
    4.  
    5. if (event->button() == Qt::LeftButton) {
    6. cout<<"pulsado en ("<<event->x()<<","<<event->y()<<")"<<endl;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    But nothing is printed to the console.

    I read that to subclass any "QT class" and override any signal or slot, the "QT class" has to be a QObject. QPixmap is not a QObject.
    http://lists.trolltech.com/qt-intere...ad01050-0.html

    So, if I do this:
    Qt Code:
    1. class SubQPixmap : public QPixmap
    2. {
    3. Q_OBJECT
    4. public:
    5. void mousePressEvent(QMouseEvent *event);
    6. };
    To copy to clipboard, switch view to plain text mode 

    The compiler says:

    'staticMetaObject' is not a member of 'QPixmap'

    So, I think that it didn't work for this reason. ¿It's possible?

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Select pixel from image

    Sorry, you are right, I forgot QPixmap is not a QWidget, so you don't have mousePressEvent().
    So just to the same to the QLabel.
    QLabel is a QWidget so you can catch the mouse press on it.
    You can then either use QPixmap or QImage to extract the pixel data.
    QImage should be faster for pixel access.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #5
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Select pixel from image

    OK, thanks!

    I did what you said.

    Now I can extract the pixel where the mouse cliked. But is not the pixel of the image, it's the pixel of QLabel. So, If y scale the image, (zoom, for example), I don't extract the real pixel of the image, only of the QLabel. But I solved this saving the scale factor and doing some operations with it and the X,Y.


    If somebody has the same problem as me, there is the code:

    This code works and get the pixel of any image. (It's based on image_viewer example of QT).
    imageviewer.cpp:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "imageviewer.h"
    4. #include <iostream> // one of two
    5. using namespace std; // two of two, yay!
    6.  
    7. void SubQLabel::setFactor(const double &factor){
    8. cout<<"setFactor"<<endl;
    9. scaleFactor = factor;
    10. }
    11.  
    12. ImageViewer::ImageViewer()
    13. {
    14. imageLabel = new SubQLabel;
    15. imageLabel->setBackgroundRole(QPalette::Base);
    16. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    17. imageLabel->setScaledContents(true);
    18.  
    19. scrollArea = new QScrollArea;
    20. scrollArea->setBackgroundRole(QPalette::Dark);
    21. scrollArea->setWidget(imageLabel);
    22. setCentralWidget(scrollArea);
    23.  
    24. createActions();
    25. createMenus();
    26.  
    27. setWindowTitle(tr("Image Viewer"));
    28. resize(500, 400);
    29. }
    30.  
    31. void ImageViewer::open()
    32. {
    33. QString fileName = QFileDialog::getOpenFileName(this,
    34. tr("Open File"), QDir::currentPath());
    35. if (!fileName.isEmpty()) {
    36. QImage image(fileName);
    37. if (image.isNull()) {
    38. QMessageBox::information(this, tr("Image Viewer"),
    39. tr("Cannot load %1.").arg(fileName));
    40. return;
    41. }
    42. imageLabel->setPixmap(QPixmap::fromImage(image));
    43. scaleFactor = 1.0;
    44.  
    45. printAct->setEnabled(true);
    46. fitToWindowAct->setEnabled(true);
    47. updateActions();
    48.  
    49. if (!fitToWindowAct->isChecked())
    50. imageLabel->adjustSize();
    51. }
    52. }
    53.  
    54. void ImageViewer::print()
    55. {
    56. Q_ASSERT(imageLabel->pixmap());
    57. #ifndef QT_NO_PRINTER
    58. QPrintDialog dialog(&printer, this);
    59. if (dialog.exec()) {
    60. QPainter painter(&printer);
    61. QRect rect = painter.viewport();
    62. QSize size = imageLabel->pixmap()->size();
    63. size.scale(rect.size(), Qt::KeepAspectRatio);
    64. painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
    65. painter.setWindow(imageLabel->pixmap()->rect());
    66. painter.drawPixmap(0, 0, *imageLabel->pixmap());
    67. }
    68. #endif
    69. }
    70.  
    71. void ImageViewer::zoomIn()
    72. {
    73. scaleImage(1.25);
    74. }
    75.  
    76. void ImageViewer::zoomOut()
    77. {
    78. scaleImage(0.8);
    79. }
    80.  
    81. void ImageViewer::normalSize()
    82. {
    83. imageLabel->adjustSize();
    84. scaleFactor = 1.0;
    85. }
    86.  
    87. void ImageViewer::fitToWindow()
    88. {
    89. bool fitToWindow = fitToWindowAct->isChecked();
    90. scrollArea->setWidgetResizable(fitToWindow);
    91. if (!fitToWindow) {
    92. normalSize();
    93. }
    94. updateActions();
    95. }
    96.  
    97. void SubQLabel::mousePressEvent(QMouseEvent *event)
    98.  
    99. {
    100. cout<<"Ha pulsado:"<<endl;
    101. int x = event->x();
    102. int y = event->y();
    103.  
    104. int x_image = x/scaleFactor;
    105. int y_image = y/scaleFactor;
    106.  
    107. if (event->button() == Qt::LeftButton) {
    108. cout<<"x: "<< x <<endl;
    109. cout<<"y: "<< y <<endl;
    110.  
    111. cout<<"Pixels de la imagen: ("<< x_image <<","<< y_image <<endl;
    112. //cout<<"pulsado en ("<<event->x()/scaleFactor<<","<<event->y()/scaleFactor<<")"<<endl;
    113.  
    114. }
    115. cout<<"Factor de escalado" << scaleFactor<<endl;
    116. }
    117.  
    118. void SubQLabel::setScaleFactor(double factor){
    119. scaleFactor = factor;
    120. }
    121.  
    122. void ImageViewer::about()
    123. {
    124. QMessageBox::about(this, tr("About Image Viewer"),
    125. tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
    126. "and QScrollArea to display an image. QLabel is typically used "
    127. "for displaying a text, but it can also display an image. "
    128. "QScrollArea provides a scrolling view around another widget. "
    129. "If the child widget exceeds the size of the frame, QScrollArea "
    130. "automatically provides scroll bars. </p><p>The example "
    131. "demonstrates how QLabel's ability to scale its contents "
    132. "(QLabel::scaledContents), and QScrollArea's ability to "
    133. "automatically resize its contents "
    134. "(QScrollArea::widgetResizable), can be used to implement "
    135. "zooming and scaling features. </p><p>In addition the example "
    136. "shows how to use QPainter to print an image.</p>"));
    137. }
    138.  
    139. void ImageViewer::createActions()
    140. {
    141. openAct = new QAction(tr("&Open..."), this);
    142. openAct->setShortcut(tr("Ctrl+O"));
    143. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
    144.  
    145. printAct = new QAction(tr("&Print..."), this);
    146. printAct->setShortcut(tr("Ctrl+P"));
    147. printAct->setEnabled(false);
    148. connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
    149.  
    150. exitAct = new QAction(tr("E&xit"), this);
    151. exitAct->setShortcut(tr("Ctrl+Q"));
    152. connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
    153.  
    154. zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
    155. zoomInAct->setShortcut(tr("Ctrl++"));
    156. zoomInAct->setEnabled(false);
    157. connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
    158.  
    159. zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
    160. zoomOutAct->setShortcut(tr("Ctrl+-"));
    161. zoomOutAct->setEnabled(false);
    162. connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
    163.  
    164. normalSizeAct = new QAction(tr("&Normal Size"), this);
    165. normalSizeAct->setShortcut(tr("Ctrl+S"));
    166. normalSizeAct->setEnabled(false);
    167. connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
    168.  
    169. fitToWindowAct = new QAction(tr("&Fit to Window"), this);
    170. fitToWindowAct->setEnabled(false);
    171. fitToWindowAct->setCheckable(true);
    172. fitToWindowAct->setShortcut(tr("Ctrl+F"));
    173. connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
    174.  
    175. aboutAct = new QAction(tr("&About"), this);
    176. connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
    177.  
    178. aboutQtAct = new QAction(tr("About &Qt"), this);
    179. connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    180.  
    181. connect(
    182. this,
    183. SIGNAL(scaleFactorChanged(const double&)),
    184. imageLabel,
    185. SLOT(setFactor(const double&)));
    186. }
    187.  
    188. void ImageViewer::createMenus()
    189. {
    190. fileMenu = new QMenu(tr("&File"), this);
    191. fileMenu->addAction(openAct);
    192. fileMenu->addAction(printAct);
    193. fileMenu->addSeparator();
    194. fileMenu->addAction(exitAct);
    195.  
    196. viewMenu = new QMenu(tr("&View"), this);
    197. viewMenu->addAction(zoomInAct);
    198. viewMenu->addAction(zoomOutAct);
    199. viewMenu->addAction(normalSizeAct);
    200. viewMenu->addSeparator();
    201. viewMenu->addAction(fitToWindowAct);
    202.  
    203. helpMenu = new QMenu(tr("&Help"), this);
    204. helpMenu->addAction(aboutAct);
    205. helpMenu->addAction(aboutQtAct);
    206.  
    207. menuBar()->addMenu(fileMenu);
    208. menuBar()->addMenu(viewMenu);
    209. menuBar()->addMenu(helpMenu);
    210. }
    211.  
    212. void ImageViewer::updateActions()
    213. {
    214. zoomInAct->setEnabled(!fitToWindowAct->isChecked());
    215. zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
    216. normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
    217. }
    218.  
    219. void ImageViewer::scaleImage(double factor)
    220. {
    221. Q_ASSERT(imageLabel->pixmap());
    222. scaleFactor *= factor;
    223. imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
    224.  
    225. adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
    226. adjustScrollBar(scrollArea->verticalScrollBar(), factor);
    227.  
    228. zoomInAct->setEnabled(scaleFactor < 3.0);
    229. zoomOutAct->setEnabled(scaleFactor > 0.333);
    230.  
    231. emit scaleFactorChanged( scaleFactor );
    232. }
    233.  
    234. void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
    235. {
    236. scrollBar->setValue(int(factor * scrollBar->value()
    237. + ((factor - 1) * scrollBar->pageStep()/2)));
    238. }
    To copy to clipboard, switch view to plain text mode 
    ImageViewer.h:
    Qt Code:
    1. #ifndef IMAGEVIEWER_H_
    2. #define IMAGEVIEWER_H_
    3.  
    4. #include <QMainWindow>
    5. #include <QPrinter>
    6. #include <QPixmap>
    7. #include <QLabel>
    8. #include <iostream>
    9.  
    10. class QAction;
    11. class QLabel;
    12. class QMenu;
    13. class QScrollArea;
    14. class QScrollBar;
    15. class SubQLabel;
    16.  
    17.  
    18.  
    19. class ImageViewer : public QMainWindow
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. ImageViewer();
    25.  
    26. private slots:
    27. void open();
    28. void print();
    29. void zoomIn();
    30. void zoomOut();
    31. void normalSize();
    32. void fitToWindow();
    33. void about();
    34.  
    35. //protected:
    36. // void mousePressEvent(QMouseEvent *event);
    37. signals:
    38. void scaleFactorChanged(const double& );
    39.  
    40.  
    41. private:
    42. void createActions();
    43. void createMenus();
    44. void updateActions();
    45. void scaleImage(double factor);
    46. void adjustScrollBar(QScrollBar *scrollBar, double factor);
    47. double getSacleImage();
    48.  
    49. SubQLabel *imageLabel;
    50. QScrollArea *scrollArea;
    51. double scaleFactor;
    52.  
    53. #ifndef QT_NO_PRINTER
    54. QPrinter printer;
    55. #endif
    56.  
    57. QAction *openAct;
    58. QAction *printAct;
    59. QAction *exitAct;
    60. QAction *zoomInAct;
    61. QAction *zoomOutAct;
    62. QAction *normalSizeAct;
    63. QAction *fitToWindowAct;
    64. QAction *aboutAct;
    65. QAction *aboutQtAct;
    66.  
    67. QMenu *fileMenu;
    68. QMenu *viewMenu;
    69. QMenu *helpMenu;
    70. };
    71.  
    72. #endif /* IMAGEVIEWER_H_ */
    73.  
    74.  
    75. class SubQLabel : public QLabel
    76. {
    77. Q_OBJECT
    78.  
    79. public:
    80. void mousePressEvent(QMouseEvent *event);
    81. void setScaleFactor(double factor);
    82.  
    83. public slots:
    84. void setFactor(const double &factor);
    85.  
    86. private:
    87. double scaleFactor;
    88. //public:
    89. //SubQPixmap();
    90.  
    91. };
    To copy to clipboard, switch view to plain text mode 


    With signals and slots I get and set the scaleFactor.

    Thanks!!

Similar Threads

  1. Raw Pixel Data to Image or Picture, to draw in a Widget
    By augusbas in forum Qt Programming
    Replies: 1
    Last Post: 7th September 2010, 14:15
  2. Replies: 0
    Last Post: 7th May 2010, 23:45
  3. Select Region from image
    By yazwas in forum Newbie
    Replies: 4
    Last Post: 15th July 2009, 18:41
  4. How do i display the rgb value of a pixel
    By davidw in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2009, 00:51
  5. QPixmap pixel by pixel ?
    By tommy in forum Qt Programming
    Replies: 19
    Last Post: 3rd December 2007, 23:52

Tags for this Thread

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.