From 8171c1f21396a7a73280eb13ab3ce646a986e317 Mon Sep 17 00:00:00 2001 From: qbigot <quentin.bigot@insa-rennes.fr> Date: Fri, 21 Jul 2017 11:46:56 +0200 Subject: [PATCH] add possibility to calculate entropy for a double image --- app/CMakeLists.txt | 2 + app/Operations/DoubleEntropyOp.cpp | 59 ++++++++++++++++++++++++++ app/Operations/DoubleEntropyOp.h | 35 +++++++++++++++ app/Operations/EntropyOp.cpp | 68 +++--------------------------- app/imageinsa_en.ts | 14 +++++- app/imageinsa_fr.ts | 14 +++++- app/main.cpp | 2 + lib/detiq-t | 2 +- 8 files changed, 130 insertions(+), 66 deletions(-) create mode 100644 app/Operations/DoubleEntropyOp.cpp create mode 100644 app/Operations/DoubleEntropyOp.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index ba856bb..0690c8a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -45,6 +45,8 @@ set(imageinsa_SOURCES Operations/DMMDialog.h Operations/DMMOp.cpp Operations/DMMOp.h + Operations/DoubleEntropyOp.cpp + Operations/DoubleEntropyOp.h Operations/EntropyOp.cpp Operations/EntropyOp.h Operations/FFTDialog.cpp diff --git a/app/Operations/DoubleEntropyOp.cpp b/app/Operations/DoubleEntropyOp.cpp new file mode 100644 index 0000000..b553bb1 --- /dev/null +++ b/app/Operations/DoubleEntropyOp.cpp @@ -0,0 +1,59 @@ +/* + * Copyright 2011-2012 INSA Rennes + * + * This file is part of ImageINSA. + * + * ImageINSA 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 3 of the License, or + * (at your option) any later version. + * + * ImageINSA 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 ImageINSA. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <cmath> + +#include "DoubleEntropyOp.h" + +#include "../Tools.h" +#include <Image.h> + +#include "GenericInterface.h" +#include <Array.h> +#include <Converter.h> + +#include <QMap> +using namespace std; +using namespace imagein; + +DoubleEntropyOp::DoubleEntropyOp() : DoubleOperation(qApp->translate("Operations", "Calcul d'entropie (image double)").toStdString()) +{ +} + +void DoubleEntropyOp::operator()(const Image_t<double>* image, const std::map<const imagein::Image_t<double>*, std::string>&) { + + double entropy = 0.; + + for(unsigned int c = 0; c < image->getNbChannels(); ++c) { + Histogram histo = image->getHistogram(c); + for(int i = 0; i < 256; ++i) { + if(histo[i] > 0) { + double p = (double)histo[i] / image->getWidth() /image->getHeight(); + entropy += p * log(p); + } + } + } + + entropy = - entropy / log(2); + outText(qApp->translate("Operations", "Entropy of the image = %1").arg(entropy).toStdString()); +} + +bool DoubleEntropyOp::needCurrentImg() const { + return true; +} diff --git a/app/Operations/DoubleEntropyOp.h b/app/Operations/DoubleEntropyOp.h new file mode 100644 index 0000000..4df8cd7 --- /dev/null +++ b/app/Operations/DoubleEntropyOp.h @@ -0,0 +1,35 @@ +/* + * Copyright 2011-2012 INSA Rennes + * + * This file is part of ImageINSA. + * + * ImageINSA 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 3 of the License, or + * (at your option) any later version. + * + * ImageINSA 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 ImageINSA. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef DOUBLEENTROPYOP_H +#define DOUBLEENTROPYOP_H + +#include <Operation.h> + +class DoubleEntropyOp : public DoubleOperation +{ +public: + DoubleEntropyOp(); + + void operator()(const imagein::Image_t<double>*, const std::map<const imagein::Image_t<double>*, std::string>&); + + bool needCurrentImg() const; +}; + +#endif // DOUBLEENTROPYOP_H diff --git a/app/Operations/EntropyOp.cpp b/app/Operations/EntropyOp.cpp index d79009c..c14e279 100644 --- a/app/Operations/EntropyOp.cpp +++ b/app/Operations/EntropyOp.cpp @@ -24,90 +24,36 @@ #include "../Tools.h" #include <Image.h> -#include "../../lib/detiq-t/GenericInterface/Widgets/ImageWidgets/GraphicalHistogram.h" -#include "../../lib/detiq-t/GenericInterface/Widgets/ImageWidgets/GenericHistogramView.h" +#include "GenericInterface.h" +#include <Array.h> +#include <Converter.h> +#include <QMap> using namespace std; using namespace imagein; -EntropyOp::EntropyOp() : Operation(qApp->translate("Operations", "Calcul d'entropie").toStdString()) +EntropyOp::EntropyOp() : Operation(qApp->translate("Operations", "Calcul d'entropie (image standard)").toStdString()) { } void EntropyOp::operator()(const Image* image, const std::map<const imagein::Image*, std::string>&) { - /* - std::vector<GraphicalHistogram*> _graphicalHistos; - for(uint i = 0; i < nbChannels; ++i) - { - GraphicalHistogram* graphicalHisto; - switch(i) - { - case 0: - if(nbChannels == 1 || nbChannels == 2) - graphicalHisto = new GraphicalHistogram(tr("Black"), Qt::black); - else - graphicalHisto = new GraphicalHistogram(tr("Red"), Qt::red); - break; - case 1: - if(nbChannels == 1 || nbChannels == 2) - graphicalHisto = new GraphicalHistogram(tr("Alpha"), Qt::white); - else - graphicalHisto = new GraphicalHistogram(tr("Green"), Qt::green); - break; - case 2: - graphicalHisto = new GraphicalHistogram(tr("Blue"), Qt::blue); - break; - case 3: - graphicalHisto = new GraphicalHistogram(tr("Alpha"), Qt::black); - break; - default: - graphicalHisto = new GraphicalHistogram(tr("Channel"), Qt::black); - } - _graphicalHistos.push_back(graphicalHisto); - } - for(unsigned int channel = 0; channel < _graphicalHistos.size(); ++channel) { - GraphicalHistogram* graphicalHisto = _graphicalHistos[channel]; - - - QMap<int, int> cumulativeValues; - - uint maxw = rect.w > 0 ? rect.x + rect.w : image->getWidth(); - uint maxh = rect.h > 0 ? rect.y + rect.h : image->getHeight(); - for(uint j = rect.y; j < maxh; j++) { - for(uint i = rect.x; i < maxw; i++) { - double pixel = image->getPixel(i, j, channel); - cumulativeValues[qFloor(pixel)]++; - } - } - - QVector<QwtIntervalSample> samples; - for(int i = qFloor(image->min()); i <= qFloor(image->max()); ++i) { - QwtIntervalSample sample(cumulativeValues.value(i, 0), i, i + 1); - samples << sample; - } - - graphicalHisto->setData(new QwtIntervalSeriesData(samples)); - - } - */ double entropy = 0.; + for(unsigned int c = 0; c < image->getNbChannels(); ++c) { Histogram histo = image->getHistogram(c); for(int i = 0; i < 256; ++i) { if(histo[i] > 0) { double p = (double)histo[i] / image->getWidth() /image->getHeight(); entropy += p * log(p); - - } } } + entropy = - entropy / log(2); outText(qApp->translate("Operations", "Entropy of the image = %1").arg(entropy).toStdString()); } bool EntropyOp::needCurrentImg() const { return true; - //return false; } diff --git a/app/imageinsa_en.ts b/app/imageinsa_en.ts index 7973051..6145f5a 100644 --- a/app/imageinsa_en.ts +++ b/app/imageinsa_en.ts @@ -962,10 +962,20 @@ Number of classes = %1 </translation> <source>DMM</source> <translation>DMM</translation> </message> + <message> + <location filename="Operations/DoubleEntropyOp.cpp" line="30"/> + <source>Calcul d'entropie (image double)</source> + <translation>Entropy calculation (double image)</translation> + </message> + <message> + <location filename="Operations/DoubleEntropyOp.cpp" line="47"/> + <source>Entropy of the image = %1</source> + <translation>Entropy of the image = %1</translation> + </message> <message> <location filename="Operations/EntropyOp.cpp" line="30"/> - <source>Calcul d'entropie</source> - <translation>Entropy calculation</translation> + <source>Calcul d'entropie (image standard)</source> + <translation>Entropy calculation (standard image)</translation> </message> <message> <location filename="Operations/EntropyOp.cpp" line="47"/> diff --git a/app/imageinsa_fr.ts b/app/imageinsa_fr.ts index d83ccba..3c7548f 100644 --- a/app/imageinsa_fr.ts +++ b/app/imageinsa_fr.ts @@ -962,10 +962,20 @@ Nombre de classes = %1 </translation> <source>DMM</source> <translation>DMM</translation> </message> + <message> + <location filename="Operations/DoubleEntropyOp.cpp" line="30"/> + <source>Calcul d'entropie (image double)</source> + <translation>Calcul d'entropie (image double)</translation> + </message> + <message> + <location filename="Operations/DoubleEntropyOp.cpp" line="47"/> + <source>Entropy of the image = %1</source> + <translation>Entropie de l'image = %1</translation> + </message> <message> <location filename="Operations/EntropyOp.cpp" line="30"/> - <source>Calcul d'entropie</source> - <translation>Calcul d'entropie</translation> + <source>Calcul d'entropie (image standard)</source> + <translation>Calcul d'entropie (image standard)</translation> </message> <message> <location filename="Operations/EntropyOp.cpp" line="47"/> diff --git a/app/main.cpp b/app/main.cpp index 205a66c..4b0a0ac 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -56,6 +56,7 @@ #include "Operations/SinusSynthesisOp.h" #include "Operations/ScalingOp.h" #include "Operations/QuantificationOp.h" +#include "Operations/DoubleEntropyOp.h" #include "Operations/EntropyOp.h" #include "Operations/HuffmanOp.h" #include "Operations/RejectionRingOp.h" @@ -145,6 +146,7 @@ int main(int argc, char** argv) tools->addOperation(new SignalToNoiseOp()); tools->addOperation(new MeanSquaredErrorOp()); + tools->addOperation(new DoubleEntropyOp()); tools->addOperation(new EntropyOp()); tools->addOperation(new NoiseOp()); tools->addOperation(new SeparatorOp()); diff --git a/lib/detiq-t b/lib/detiq-t index c7d057a..8f80b6c 160000 --- a/lib/detiq-t +++ b/lib/detiq-t @@ -1 +1 @@ -Subproject commit c7d057ad6372f580a645d26dc53f7bd5a65bbe06 +Subproject commit 8f80b6ccd42cca069eea444f935d70c2470fa1e3 -- GitLab