PDA

View Full Version : Someone maybe has written filter median to run to gray and color pictures ?



Godlike
29th March 2006, 20:54
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.

jacek
29th March 2006, 21:29
Use QImage to access pixels.

Godlike
29th March 2006, 21:51
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.

jacek
29th March 2006, 22:17
for i = 0..oldImage.width do
for j = 0..oldImage.height do
newImage[i,j] = filter(oldImage, i, j);
end
end
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.

Godlike
30th March 2006, 10:18
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 ;)

jacek
30th March 2006, 13:27
See the docs: QImage QColor
There is everything you need --- even examples.

Godlike
31st March 2006, 11:20
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.

jacek
31st March 2006, 11:57
What did you try? What QImage methods did you try to invoke?

Godlike
31st March 2006, 12:22
Here is everything i made so far, maybe somone will need something like this sometime also, so ill post everything :

main.cpp

#include <QApplication>
#include <QImageWriter>
#include "imageviewer.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ImageViewer imageViewer;
imageViewer.show();
return app.exec();
}


imageviewer.h

#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H

#include <QMainWindow>
#include <QPrinter>
#include <QImageWriter>
#include <QImage>

class QAction;
class QLabel;
class QMenu;
class QScrollArea;
class QScrollBar;

class ImageViewer : public QMainWindow
{
Q_OBJECT

public:
ImageViewer();

private slots:
void open();
void save();
void reload();
void zoomIn();
void zoomOut();
void normalSize();
void fitToWindow();
void bw();
void mediana();
void about();

private:
void createActions();
void createMenus();
void updateActions();
void scaleImage(double factor);
void adjustScrollBar(QScrollBar *scrollBar, double factor);

QLabel *imageLabel;
QImage image;
QString fileName;
QScrollArea *scrollArea;
double scaleFactor;

QPrinter printer;

QAction *openAct;
QAction *saveAct;
QAction *reloadAct;
QAction *exitAct;
QAction *zoomInAct;
QAction *zoomOutAct;
QAction *normalSizeAct;
QAction *fitToWindowAct;
QAction *bwAct;
QAction *medianaAct;
QAction *aboutAct;

QMenu *fileMenu;
QMenu *viewMenu;
QMenu *filterMenu;
QMenu *helpMenu;
};
#endif

imageviewer.cpp


#include <QtGui>
#include <QImageWriter>
#include "imageviewer.h"

ImageViewer::ImageViewer()
{
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);

scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Mid);
scrollArea->setWidget(imageLabel);
setCentralWidget(scrollArea);

createActions();
createMenus();

setWindowTitle(tr("Obdelava in Analiza slik"));
resize(500, 400);
}

void ImageViewer::open()
{
QString filter;
QString selectedFilter;
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
foreach (QString format, formats)
{
filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
}

// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}

QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath(), filter, &selectedFilter);
if (!fileName.isEmpty())
{
image.load(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("ImageViewer"), tr("Cannot load %1.").arg(fileName));
return;
}
imageLabel->setPixmap(QPixmap::fromImage(image));
scaleFactor = 1.0;

saveAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();

if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
}

void ImageViewer::save()
{
// construct a filter of all supported formats
QString filter;
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
foreach (QString format, formats)
{
filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
}

// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}

// get save file name
QString selectedFilter;
QString s = QFileDialog::getSaveFileName(this, "Save image as", QDir::currentPath(), filter, &selectedFilter);

if (!s.isEmpty())
{

// check for the selected format
QString format = selectedFilter.split(" ").at(0);
QFileInfo fi(s);

if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
s.chop(fi.suffix().length());
// set correct suffix
s += "." + format.toLower();
}

// save image in the selected format
if (!image.save(s, format.toAscii().constData()))
{
QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(s));
}
}
}

void ImageViewer::zoomIn()
{
scaleImage(1.25);
}

void ImageViewer::zoomOut()
{
scaleImage(0.8);
}

void ImageViewer::normalSize()
{
imageLabel->adjustSize();
scaleFactor = 1.0;
}

void ImageViewer::fitToWindow()
{
bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow)
{
normalSize();
}
updateActions();
}

void ImageViewer::about()
{
QMessageBox::about(this, tr("About"),
tr("<h2>Obdelava in Analiza slik</h2>");
}

void ImageViewer::createActions()
{
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

saveAct = new QAction(tr("&Save..."), this);
saveAct->setShortcut(tr("Ctrl+S"));
saveAct->setEnabled(false);
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));

reloadAct = new QAction(tr("&Reload Image..."), this);
reloadAct->setShortcut(tr("Ctrl+R"));
connect(reloadAct, SIGNAL(triggered()), this, SLOT(reload()));

exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
zoomInAct->setShortcut(tr("Ctrl++"));
zoomInAct->setEnabled(false);
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));

zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
zoomOutAct->setShortcut(tr("Ctrl+-"));
zoomOutAct->setEnabled(false);
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));

normalSizeAct = new QAction(tr("&Normal Size"), this);
normalSizeAct->setShortcut(tr("Ctrl+S"));
normalSizeAct->setEnabled(false);
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));

fitToWindowAct = new QAction(tr("&Fit to Window"), this);
fitToWindowAct->setEnabled(false);
fitToWindowAct->setCheckable(true);
fitToWindowAct->setShortcut(tr("Ctrl+F"));
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));

bwAct = new QAction(tr("Black &White"), this);
bwAct->setShortcut(tr("Ctrl+B"));
connect(bwAct, SIGNAL(triggered()), this, SLOT(bw()));

medianaAct = new QAction(tr("&Mediana"), this);
medianaAct->setShortcut(tr("Ctrl+M"));
connect(medianaAct, SIGNAL(triggered()), this, SLOT(mediana()));

aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}

void ImageViewer::createMenus()
{
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(reloadAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);

viewMenu = new QMenu(tr("&View"), this);
viewMenu->addAction(zoomInAct);
viewMenu->addAction(zoomOutAct);
viewMenu->addAction(normalSizeAct);
viewMenu->addSeparator();
viewMenu->addAction(fitToWindowAct);

filterMenu = new QMenu(tr("&Filters"), this);
filterMenu->addAction(bwAct);
filterMenu->addAction(medianaAct);

helpMenu = new QMenu(tr("&Help"), this);
helpMenu->addAction(aboutAct);

menuBar()->addMenu(fileMenu);
menuBar()->addMenu(viewMenu);
menuBar()->addMenu(filterMenu);
menuBar()->addMenu(helpMenu);
}

void ImageViewer::bw()
{
}

void ImageViewer::mediana()
{
}

void ImageViewer::reload()
{
image.load(fileName);
imageLabel->setPixmap(QPixmap::fromImage(image));
scaleFactor = 1.0;

saveAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();

if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}

void ImageViewer::updateActions()
{
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}

void ImageViewer::scaleImage(double factor)
{
Q_ASSERT(imageLabel->pixmap());
scaleFactor *= factor;
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());

adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
adjustScrollBar(scrollArea->verticalScrollBar(), factor);

zoomInAct->setEnabled(scaleFactor < 3.0);
zoomOutAct->setEnabled(scaleFactor > 0.333);
}

void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
}


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

:p

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

jacek
31st March 2006, 13:07
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.