Results 1 to 10 of 10

Thread: Someone maybe has written filter median to run to gray and color pictures ?

  1. #1
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Someone maybe has written filter median to run to gray and color pictures ?

    I made a simple image viewer and i can save pictures aswell etc. Now i would like to apply a filter to that picture, display it again changed and then save it. I only need filter median to implement in c++ to use it then. But i dont have slightest ideer how. Maybe someone who already made or found this somewhere would be nice to post it. Or help me how this can be made.

    thx.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    Use QImage to access pixels.

  3. #3
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    Some simple example on how to modify one picture. Lets say for example change it to black and white or something So i get the principle on what i should do, then i can try to find some specs for median filter and write it for use.

    And again thx for all the help, realy great people here on this forum. I am glad i signed in.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    Qt Code:
    1. for i = 0..oldImage.width do
    2. for j = 0..oldImage.height do
    3. newImage[i,j] = filter(oldImage, i, j);
    4. end
    5. end
    To copy to clipboard, switch view to plain text mode 
    where filter finds median value for each of the three colour components in (i,j) pixel's neighborhood.

    Of course with a little effort you can do it using only one image.

  5. #5
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    Ok this part i understood. What i dont get is how i actualy get to each and one pixel from image. Where do i store such pixels from image i read. And how are they presented when they are stored. I need some background so i know what i want to do

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    See the docs: QImage QColorThere is everything you need --- even examples.

  7. #7
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    Bah. Not good at c++ or QT so much. Trying to get those pixels and do something with it for a day and cant figure a working solution

    Is there one more nice soul out there, who could post me a example from getting pixels from image alreay in QImage image, and then making something with its pixels and then writing it from the memory to file again. Im trying to get this on my own, but hence hard to do if you are not so familliar with QT and c++. Need this for my school project. And i only miss this part yet. Made the other things.

    Thx big time again.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    What did you try? What QImage methods did you try to invoke?

  9. #9
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    Here is everything i made so far, maybe somone will need something like this sometime also, so ill post everything :

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QImageWriter>
    3. #include "imageviewer.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. ImageViewer imageViewer;
    9. imageViewer.show();
    10. return app.exec();
    11. }
    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 <QImageWriter>
    7. #include <QImage>
    8.  
    9. class QAction;
    10. class QLabel;
    11. class QMenu;
    12. class QScrollBar;
    13.  
    14. class ImageViewer : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. ImageViewer();
    20.  
    21. private slots:
    22. void open();
    23. void save();
    24. void reload();
    25. void zoomIn();
    26. void zoomOut();
    27. void normalSize();
    28. void fitToWindow();
    29. void bw();
    30. void mediana();
    31. void about();
    32.  
    33. private:
    34. void createActions();
    35. void createMenus();
    36. void updateActions();
    37. void scaleImage(double factor);
    38. void adjustScrollBar(QScrollBar *scrollBar, double factor);
    39.  
    40. QLabel *imageLabel;
    41. QImage image;
    42. QString fileName;
    43. QScrollArea *scrollArea;
    44. double scaleFactor;
    45.  
    46. QPrinter printer;
    47.  
    48. QAction *openAct;
    49. QAction *saveAct;
    50. QAction *reloadAct;
    51. QAction *exitAct;
    52. QAction *zoomInAct;
    53. QAction *zoomOutAct;
    54. QAction *normalSizeAct;
    55. QAction *fitToWindowAct;
    56. QAction *bwAct;
    57. QAction *medianaAct;
    58. QAction *aboutAct;
    59.  
    60. QMenu *fileMenu;
    61. QMenu *viewMenu;
    62. QMenu *filterMenu;
    63. QMenu *helpMenu;
    64. };
    65. #endif
    To copy to clipboard, switch view to plain text mode 

    imageviewer.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include <QImageWriter>
    3. #include "imageviewer.h"
    4.  
    5. ImageViewer::ImageViewer()
    6. {
    7. imageLabel = new QLabel;
    8. imageLabel->setBackgroundRole(QPalette::Base);
    9. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    10. imageLabel->setScaledContents(true);
    11.  
    12. scrollArea = new QScrollArea;
    13. scrollArea->setBackgroundRole(QPalette::Mid);
    14. scrollArea->setWidget(imageLabel);
    15. setCentralWidget(scrollArea);
    16.  
    17. createActions();
    18. createMenus();
    19.  
    20. setWindowTitle(tr("Obdelava in Analiza slik"));
    21. resize(500, 400);
    22. }
    23.  
    24. void ImageViewer::open()
    25. {
    26. QString filter;
    27. QString selectedFilter;
    28. QList<QByteArray> formats = QImageWriter::supportedImageFormats();
    29. foreach (QString format, formats)
    30. {
    31. filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
    32. }
    33.  
    34. // remove unnecessary chars from the end of the filter
    35. if (filter.endsWith(";;"))
    36. {
    37. filter.chop(2);
    38. }
    39.  
    40. QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath(), filter, &selectedFilter);
    41. if (!fileName.isEmpty())
    42. {
    43. image.load(fileName);
    44. if (image.isNull())
    45. {
    46. QMessageBox::information(this, tr("ImageViewer"), tr("Cannot load %1.").arg(fileName));
    47. return;
    48. }
    49. imageLabel->setPixmap(QPixmap::fromImage(image));
    50. scaleFactor = 1.0;
    51.  
    52. saveAct->setEnabled(true);
    53. fitToWindowAct->setEnabled(true);
    54. updateActions();
    55.  
    56. if (!fitToWindowAct->isChecked())
    57. imageLabel->adjustSize();
    58. }
    59. }
    60.  
    61. void ImageViewer::save()
    62. {
    63. // construct a filter of all supported formats
    64. QString filter;
    65. QList<QByteArray> formats = QImageWriter::supportedImageFormats();
    66. foreach (QString format, formats)
    67. {
    68. filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
    69. }
    70.  
    71. // remove unnecessary chars from the end of the filter
    72. if (filter.endsWith(";;"))
    73. {
    74. filter.chop(2);
    75. }
    76.  
    77. // get save file name
    78. QString selectedFilter;
    79. QString s = QFileDialog::getSaveFileName(this, "Save image as", QDir::currentPath(), filter, &selectedFilter);
    80.  
    81. if (!s.isEmpty())
    82. {
    83.  
    84. // check for the selected format
    85. QString format = selectedFilter.split(" ").at(0);
    86. QFileInfo fi(s);
    87.  
    88. if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
    89. {
    90. // remove possible incorrect suffix
    91. s.chop(fi.suffix().length());
    92. // set correct suffix
    93. s += "." + format.toLower();
    94. }
    95.  
    96. // save image in the selected format
    97. if (!image.save(s, format.toAscii().constData()))
    98. {
    99. QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(s));
    100. }
    101. }
    102. }
    103.  
    104. void ImageViewer::zoomIn()
    105. {
    106. scaleImage(1.25);
    107. }
    108.  
    109. void ImageViewer::zoomOut()
    110. {
    111. scaleImage(0.8);
    112. }
    113.  
    114. void ImageViewer::normalSize()
    115. {
    116. imageLabel->adjustSize();
    117. scaleFactor = 1.0;
    118. }
    119.  
    120. void ImageViewer::fitToWindow()
    121. {
    122. bool fitToWindow = fitToWindowAct->isChecked();
    123. scrollArea->setWidgetResizable(fitToWindow);
    124. if (!fitToWindow)
    125. {
    126. normalSize();
    127. }
    128. updateActions();
    129. }
    130.  
    131. void ImageViewer::about()
    132. {
    133. QMessageBox::about(this, tr("About"),
    134. tr("<h2>Obdelava in Analiza slik</h2>");
    135. }
    136.  
    137. void ImageViewer::createActions()
    138. {
    139. openAct = new QAction(tr("&Open..."), this);
    140. openAct->setShortcut(tr("Ctrl+O"));
    141. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
    142.  
    143. saveAct = new QAction(tr("&Save..."), this);
    144. saveAct->setShortcut(tr("Ctrl+S"));
    145. saveAct->setEnabled(false);
    146. connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
    147.  
    148. reloadAct = new QAction(tr("&Reload Image..."), this);
    149. reloadAct->setShortcut(tr("Ctrl+R"));
    150. connect(reloadAct, SIGNAL(triggered()), this, SLOT(reload()));
    151.  
    152. exitAct = new QAction(tr("E&xit"), this);
    153. exitAct->setShortcut(tr("Ctrl+Q"));
    154. connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
    155.  
    156. zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
    157. zoomInAct->setShortcut(tr("Ctrl++"));
    158. zoomInAct->setEnabled(false);
    159. connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
    160.  
    161. zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
    162. zoomOutAct->setShortcut(tr("Ctrl+-"));
    163. zoomOutAct->setEnabled(false);
    164. connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
    165.  
    166. normalSizeAct = new QAction(tr("&Normal Size"), this);
    167. normalSizeAct->setShortcut(tr("Ctrl+S"));
    168. normalSizeAct->setEnabled(false);
    169. connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
    170.  
    171. fitToWindowAct = new QAction(tr("&Fit to Window"), this);
    172. fitToWindowAct->setEnabled(false);
    173. fitToWindowAct->setCheckable(true);
    174. fitToWindowAct->setShortcut(tr("Ctrl+F"));
    175. connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
    176.  
    177. bwAct = new QAction(tr("Black &White"), this);
    178. bwAct->setShortcut(tr("Ctrl+B"));
    179. connect(bwAct, SIGNAL(triggered()), this, SLOT(bw()));
    180.  
    181. medianaAct = new QAction(tr("&Mediana"), this);
    182. medianaAct->setShortcut(tr("Ctrl+M"));
    183. connect(medianaAct, SIGNAL(triggered()), this, SLOT(mediana()));
    184.  
    185. aboutAct = new QAction(tr("&About"), this);
    186. connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
    187. }
    188.  
    189. void ImageViewer::createMenus()
    190. {
    191. fileMenu = new QMenu(tr("&File"), this);
    192. fileMenu->addAction(openAct);
    193. fileMenu->addAction(saveAct);
    194. fileMenu->addSeparator();
    195. fileMenu->addAction(reloadAct);
    196. fileMenu->addSeparator();
    197. fileMenu->addAction(exitAct);
    198.  
    199. viewMenu = new QMenu(tr("&View"), this);
    200. viewMenu->addAction(zoomInAct);
    201. viewMenu->addAction(zoomOutAct);
    202. viewMenu->addAction(normalSizeAct);
    203. viewMenu->addSeparator();
    204. viewMenu->addAction(fitToWindowAct);
    205.  
    206. filterMenu = new QMenu(tr("&Filters"), this);
    207. filterMenu->addAction(bwAct);
    208. filterMenu->addAction(medianaAct);
    209.  
    210. helpMenu = new QMenu(tr("&Help"), this);
    211. helpMenu->addAction(aboutAct);
    212.  
    213. menuBar()->addMenu(fileMenu);
    214. menuBar()->addMenu(viewMenu);
    215. menuBar()->addMenu(filterMenu);
    216. menuBar()->addMenu(helpMenu);
    217. }
    218.  
    219. void ImageViewer::bw()
    220. {
    221. }
    222.  
    223. void ImageViewer::mediana()
    224. {
    225. }
    226.  
    227. void ImageViewer::reload()
    228. {
    229. image.load(fileName);
    230. imageLabel->setPixmap(QPixmap::fromImage(image));
    231. scaleFactor = 1.0;
    232.  
    233. saveAct->setEnabled(true);
    234. fitToWindowAct->setEnabled(true);
    235. updateActions();
    236.  
    237. if (!fitToWindowAct->isChecked())
    238. imageLabel->adjustSize();
    239. }
    240.  
    241. void ImageViewer::updateActions()
    242. {
    243. zoomInAct->setEnabled(!fitToWindowAct->isChecked());
    244. zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
    245. normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
    246. }
    247.  
    248. void ImageViewer::scaleImage(double factor)
    249. {
    250. Q_ASSERT(imageLabel->pixmap());
    251. scaleFactor *= factor;
    252. imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
    253.  
    254. adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
    255. adjustScrollBar(scrollArea->verticalScrollBar(), factor);
    256.  
    257. zoomInAct->setEnabled(scaleFactor < 3.0);
    258. zoomOutAct->setEnabled(scaleFactor > 0.333);
    259. }
    260.  
    261. void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
    262. {
    263. scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
    264. }
    To copy to clipboard, switch view to plain text mode 

    Sorry for long post. I just have to make those two modifications to image when i open them. So median and black & white.



    But i dont know how to change the source image. Get to pixels in the area and get median value for example.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Someone maybe has written filter median to run to gray and color pictures ?

    Quote Originally Posted by Godlike
    Get to pixels in the area and get median value for example.
    If you did actually look into docs, I'm sure you would notice QImage::pixel() and QImage::setPixel() methods.

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.