histogram refactor + fixes
This commit is contained in:
parent
718517590b
commit
664d4e19f9
@ -23,26 +23,14 @@ Histogram::~Histogram()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Histogram::generate(QImage* image) {
|
void Histogram::generate(QImage* image) {
|
||||||
R->clear();
|
|
||||||
G->clear();
|
|
||||||
B->clear();
|
|
||||||
L->clear();
|
|
||||||
|
|
||||||
if (image == nullptr) {
|
|
||||||
qDebug() << "Invalid image provided.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int y = 0; y < image->height(); ++y) {
|
|
||||||
for (int x = 0; x < image->width(); ++x) {
|
for (int x = 0; x < image->width(); ++x) {
|
||||||
QColor pixelColor = image->pixelColor(x, y);
|
for (int y = 0; y < image->height(); ++y) {
|
||||||
|
QRgb p = image->pixel(x, y);
|
||||||
|
|
||||||
R->insert(pixelColor.red(), R->value(pixelColor.red()) + 1);
|
Histogram::R->insert(qRed(p), Histogram::R->value(qRed(p)) + 1);
|
||||||
G->insert(pixelColor.green(), G->value(pixelColor.green()) + 1);
|
Histogram::G->insert(qGreen(p), Histogram::G->value(qGreen(p)) + 1);
|
||||||
B->insert(pixelColor.blue(), B->value(pixelColor.blue()) + 1);
|
Histogram::B->insert(qBlue(p), Histogram::B->value(qBlue(p)) + 1);
|
||||||
|
Histogram::L->insert(qGray(p), Histogram::L->value(qGray(p)) + 1);
|
||||||
int luminance = qRound(0.299 * pixelColor.red() + 0.587 * pixelColor.green() + 0.114 * pixelColor.blue());
|
|
||||||
L->insert(luminance, L->value(luminance) + 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,27 +39,37 @@ int Histogram::maximumValue(Channel selectedChannel)
|
|||||||
{
|
{
|
||||||
int maxVal = 0;
|
int maxVal = 0;
|
||||||
|
|
||||||
if (selectedChannel == RGB) {
|
if(Histogram::getMaxChannel(R) > maxVal) {
|
||||||
int maxR = maximumValue(RChannel);
|
maxVal = Histogram::getMaxChannel(R);
|
||||||
int maxG = maximumValue(GChannel);
|
|
||||||
int maxB = maximumValue(BChannel);
|
|
||||||
maxVal = std::max(maxR, std::max(maxG, maxB));
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
QHash<int, int>* hist = get(selectedChannel);
|
|
||||||
|
|
||||||
QHash<int, int>::const_iterator cit = hist->constBegin();
|
if(Histogram::getMaxChannel(G) > maxVal) {
|
||||||
while (cit != hist->constEnd()) {
|
maxVal = Histogram::getMaxChannel(G);
|
||||||
if (cit.value() > maxVal) {
|
|
||||||
maxVal = cit.value();
|
|
||||||
}
|
}
|
||||||
++cit;
|
|
||||||
|
if(Histogram::getMaxChannel(B) > maxVal) {
|
||||||
|
maxVal = Histogram::getMaxChannel(B);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(Histogram::getMaxChannel(L) > maxVal) {
|
||||||
|
maxVal = Histogram::getMaxChannel(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
return maxVal;
|
return maxVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Histogram::getMaxChannel(QHash<int, int>* channel) {
|
||||||
|
int maxValue = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < channel->size(); i++)
|
||||||
|
{
|
||||||
|
if(channel->value(i) > maxValue)
|
||||||
|
maxValue = channel->value(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return maxValue;
|
||||||
|
}
|
||||||
|
|
||||||
QHash<int, int>* Histogram::get(Channel channel = LChannel)
|
QHash<int, int>* Histogram::get(Channel channel = LChannel)
|
||||||
{
|
{
|
||||||
if (channel==LChannel) return L;
|
if (channel==LChannel) return L;
|
||||||
|
@ -19,6 +19,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void generate(QImage*); // iterates all parent image pixels and set the Hashes
|
void generate(QImage*); // iterates all parent image pixels and set the Hashes
|
||||||
|
int getMaxChannel(QHash<int, int>*);
|
||||||
|
|
||||||
QHash<int, int>* R;
|
QHash<int, int>* R;
|
||||||
QHash<int, int>* G;
|
QHash<int, int>* G;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "histogram_equalization.h"
|
#include "histogram_equalization.h"
|
||||||
|
|
||||||
#include "../histogram.h"
|
#include "../histogram.h"
|
||||||
|
#include <cmath> // Dla funkcji round
|
||||||
|
|
||||||
HistogramEqualization::HistogramEqualization(PNM* img) :
|
HistogramEqualization::HistogramEqualization(PNM* img) :
|
||||||
Transformation(img)
|
Transformation(img)
|
||||||
@ -17,10 +17,43 @@ PNM* HistogramEqualization::transform()
|
|||||||
int width = image->width();
|
int width = image->width();
|
||||||
int height = image->height();
|
int height = image->height();
|
||||||
|
|
||||||
|
// Tworzymy nowy obiekt PNM do przechowywania przetworzonego obrazu
|
||||||
PNM* newImage = new PNM(width, height, image->format());
|
PNM* newImage = new PNM(width, height, image->format());
|
||||||
|
|
||||||
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
|
// Tworzymy histogram dla obecnego obrazu
|
||||||
|
Histogram* hist = image->getHistogram(); // Nie przekazujemy argumentów
|
||||||
|
|
||||||
|
// Pobieramy łączną liczbę pikseli na obrazie
|
||||||
|
int totalPixels = width * height;
|
||||||
|
|
||||||
|
// Tworzymy dystrybuantę
|
||||||
|
QVector<double> cdf(256, 0.0); // Inicjalizujemy wszystkie wartości na 0.0
|
||||||
|
double sum = 0.0;
|
||||||
|
for (int i = 0; i < 256; ++i) {
|
||||||
|
sum += hist->get(Histogram::LChannel)->value(i) / static_cast<double>(totalPixels); // Używamy kanału luminancji
|
||||||
|
cdf[i] = sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalizujemy dystrybuantę do przedziału [0, 255]
|
||||||
|
QVector<int> lookupTable(256, 0);
|
||||||
|
for (int i = 0; i < 256; ++i) {
|
||||||
|
lookupTable[i] = round(cdf[i] * 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Przeprowadzamy wyrównywanie histogramu
|
||||||
|
for (int y = 0; y < height; ++y) {
|
||||||
|
for (int x = 0; x < width; ++x) {
|
||||||
|
QRgb pixel = image->pixel(x, y);
|
||||||
|
int grayValue = qGray(pixel); // Pobieramy wartość odcienia szarości
|
||||||
|
|
||||||
|
// Używamy wartości z lookupTable jako nowej wartości odcienia szarości
|
||||||
|
int newGrayValue = lookupTable[grayValue];
|
||||||
|
QRgb newPixel = qRgb(newGrayValue, newGrayValue, newGrayValue); // Ustawiamy nową wartość dla każdego kanału RGB
|
||||||
|
|
||||||
|
// Ustawiamy nowy piksel na obrazie docelowym
|
||||||
|
newImage->setPixel(x, y, newPixel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return newImage;
|
return newImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user