|
From: Gustavo P. B. <gb...@us...> - 2005-07-03 01:35:35
|
Update of /cvsroot/kimageprocess/kimageprocess/src/plugins/mindist In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15258/mindist Added Files: Makefile.am kimageprocess_mindist.desktop mindist.cpp mindist.h Log Message: Added the minimum euclidean distance classifier --- NEW FILE: kimageprocess_mindist.desktop --- [Desktop Entry] Name=Minimun Euclidean Distance Backend Comment=A classifier that uses the minimun euclidean distance ServiceTypes=KImageProcess/Plugin Type=Service X-KDE-Library=kimageprocess_mindist --- NEW FILE: Makefile.am --- INCLUDES = $(all_includes) -I$(srcdir)/../../libkimageprocess METASOURCES = AUTO kde_module_LTLIBRARIES = kimageprocess_mindist.la kimageprocess_mindist_la_LDFLAGS = -module $(KDE_PLUGIN) $(all_libraries) noinst_HEADERS = mindist.h kimageprocess_mindist_la_SOURCES = mindist.cpp kde_services_DATA = kimageprocess_mindist.desktop kimageprocess_mindist_la_LIBADD = $(top_builddir)/src/libkimageprocess/libkimageprocess.la \ -lkdeui $(LIB_KIO) --- NEW FILE: mindist.cpp --- /*************************************************************************** * Copyright (C) 2004-2005 by * * Gustavo Pichorim Boiko <gus...@kd...> * * Herton Ronaldo Krzesinski <he...@my...> * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "mindist.h" #include <kgenericfactory.h> #include <kaction.h> #include <kdebug.h> #include <ktempfile.h> #include <kfiledialog.h> #include <kio/netaccess.h> #include <ktimage.h> #include <ktpluginmanager.h> #include <ktpatternmanager.h> #include <math.h> K_EXPORT_COMPONENT_FACTORY( kimageprocess_mindist, KGenericFactory<KTMinDistPlugin>( "kimageprocess_mindist" ) ) KTMinDistPlugin::KTMinDistPlugin(QObject *parent, const char* name, const QStringList&) : KTClassifBackend(parent, name) { m_mean = 0; } KTMinDistPlugin::~KTMinDistPlugin() { if (m_mean != 0) delete m_mean; } void KTMinDistPlugin::doTraining(const QValueList<dataEntry>& data, int inputs, int outputs) { int count[outputs]; if (m_mean != 0) delete m_mean; m_mean = new QValueVector<float>[outputs]; for (int i=0; i < outputs; ++i) { m_mean[i].resize(inputs, 0.); count[i] = 0; } QValueList<dataEntry>::ConstIterator end = data.constEnd(); for (QValueList<dataEntry>::ConstIterator it = data.constBegin(); it != end; ++it) { for ( int i=0; i < inputs; ++i ) { m_mean[(*it).sampleClass-1][i] += (*it).inputs[i]; ++count[(*it).sampleClass-1]; } } for (int i=0; i < outputs; ++i) for (int j=0; j < inputs; ++j) { m_mean[i][j] /= count[i]; //kdDebug() << "MINDIST: m_mean[" << i << "][" << j << "] = " << m_mean[i][j] << endl; } } void KTMinDistPlugin::doClassify(const QValueList<dataEntry>& data, int inputs, int outputs) { float minValue; int minClass; bool m_first = true; QValueList<dataEntry>::const_iterator end = data.constEnd(); QValueList<dataEntry>::const_iterator it; KTImage *c_img = KTImageManager::self()->testingImage(); QImage *img = new QImage(c_img->width(), c_img->height(), 32); int x = 0, y = 0; int colorStep = 255 / (outputs - 1); int level; float sum; for (it = data.constBegin(); it != end; ++it) { m_first = true; for (int j=0; j < outputs; ++j) { sum = 0; for (int i=0; i < inputs; ++i) sum += ((*it).inputs[i] - m_mean[j][i]) * ((*it).inputs[i] - m_mean[j][i]); sum = sqrt(sum); if (m_first) { m_first = false; minValue = sum; minClass = j; } else if (sum < minValue) { minValue = sum; minClass = j; } } //kdDebug() << "MINDIST_CLASS: Pixel is of class " << minClass << endl; level = minClass * colorStep; img->setPixel(x, y++, qRgb(level, level, level)); if (y >= c_img->height()) { kdDebug() << "y = " << y << endl; y = 0; x++; } } //save the image KURL dest = KFileDialog::getSaveURL(QString::null,i18n("*.pgm|PGM File"),0,i18n("Save classified image")); if (!dest.isEmpty()) { KTempFile tmpImg(QString::null, ".pgm"); tmpImg.close(); img->save(tmpImg.name(), "PGM"); KIO::NetAccess::upload(tmpImg.name(), dest, 0); KIO::NetAccess::removeTempFile(tmpImg.name()); delete img; } } void KTMinDistPlugin::setupPlugin() { //connect signals/slots here } #include "mindist.moc" --- NEW FILE: mindist.h --- /*************************************************************************** * Copyright (C) 2004-2005 by * * Gustavo Pichorim Boiko <gus...@kd...> * * Herton Ronaldo Krzesinski <he...@my...> * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef KIMAGEPROCESS_MINDIST_H #define KIMAGEPROCESS_MINDIST_H #include <ktclassifbackend.h> #include <qvaluelist.h> #include <qvaluevector.h> #include <qstring.h> class KTImage; /** A SNNS backend plugin @author Gustavo Pichorim Boiko */ class KTMinDistPlugin : public KTClassifBackend { Q_OBJECT public: KTMinDistPlugin(QObject *parent, const char *name, const QStringList&); ~KTMinDistPlugin(); void setupPlugin(); // Reimplemented from KTClassifBackends void doTraining(const QValueList<dataEntry>& data, int inputs, int outputs); void doClassify(const QValueList<dataEntry>& data, int inputs, int outputs); QString classifierName() { return "mindist"; } private: QValueVector<float> *m_mean; }; #endif |