Compare commits

..

No commits in common. "96129bc8ab78265908c436bdcf19fe69d206d5d4" and "664d4e19f9d409bf10f1b7a336e8acbac23c3d7a" have entirely different histories.

2 changed files with 56 additions and 123 deletions

View File

@ -1,6 +1,6 @@
#include "histogram_equalization.h"
#include "../histogram.h"
#include <cmath> // Dla funkcji round
HistogramEqualization::HistogramEqualization(PNM* img) :
Transformation(img)
@ -14,77 +14,44 @@ HistogramEqualization::HistogramEqualization(PNM* img, ImageViewer* iv) :
PNM* HistogramEqualization::transform()
{
float width = image->width();
float height = image->height();
int width = image->width();
int height = image->height();
// Tworzymy nowy obiekt PNM do przechowywania przetworzonego obrazu
PNM* newImage = new PNM(width, height, image->format());
Histogram* histogram = image->getHistogram();
QHash<int, int>* R = histogram->get(Histogram::RChannel);
QHash<int, int>* G = histogram->get(Histogram::GChannel);
QHash<int, int>* B = histogram->get(Histogram::BChannel);
QHash<int, int>* L = histogram->get(Histogram::LChannel);
// Tworzymy histogram dla obecnego obrazu
Histogram* hist = image->getHistogram(); // Nie przekazujemy argumentów
// Probability tables
float redProb[255];
float greenProb[255];
float blueProb[255];
float greyProb[255];
// Pobieramy łączną liczbę pikseli na obrazie
int totalPixels = width * height;
float pixelSum = width * height;
for (int i = 0; i < 256; i++)
{
redProb[i] = (R->value(i)) / (pixelSum);
greenProb[i] = (G->value(i)) / (pixelSum);
blueProb[i] = (B->value(i)) / (pixelSum);
greyProb[i] = (L->value(i)) / (pixelSum);
// 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;
}
// D look-up
float redD[255];
float greenD[255];
float blueD[255];
float greyD[255];
// Create template values
float sumRed = 0;
float sumGreen = 0;
float sumBlue = 0;
float sumGrey = 0;
for(int i = 0; i < 255; i++)
{
sumRed = redProb[i] + sumRed;
redD[i] = sumRed;
sumGreen = greenProb[i] + sumGreen;
greenD[i] = sumGreen;
sumBlue = blueProb[i] + sumBlue;
blueD[i] = sumBlue;
sumGrey= greyProb[i] + sumGrey;
greyD[i] = sumGrey;
// 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);
}
for (int x = 0; x < image->width(); x++)
{
for (int y = 0; y < image->height(); y++)
{
// Przeprowadzamy wyrównywanie histogramu
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
QRgb pixel = image->pixel(x, y);
if (image->format() == QImage::Format_RGB32) {
newImage->setPixel(
x,
y,
QColor(
redD[qRed(pixel)] * 255,
greenD[qGreen(pixel)] * 255,
blueD[qBlue(pixel)] * 255
).rgb());
}
else {
newImage->setPixel(x, y, greyD[qGray(pixel)] * 255);
}
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);
}
}

View File

@ -20,76 +20,42 @@ PNM* HistogramStretching::transform()
PNM* newImage = new PNM(width, height, image->format());
Histogram* histogram = image->getHistogram();
int maxRed = 255, maxGreen = 255, maxBlue = 255, maxGrey = 255;
int minValue = -1;
int maxValue = -1;
int minRed = 0, minGreen = 0, minBlue = 0, minGrey = 0;
QHash<int, int>* R = histogram->get(histogram->RChannel);
QHash<int, int>* G = histogram->get(histogram->GChannel);
QHash<int, int>* B = histogram->get(histogram->BChannel);
QHash<int, int>* L = histogram->get(histogram->LChannel);
for (int i=0; i<256; i++)
for (int y = 0; y < image->height(); ++y) {
for (int x = 0; x < image->width(); ++x) {
if (histogram->get(Histogram::LChannel)->value(x + y * x) != 0)
{
if (R->value(minRed) == 0) {
minRed++;
minValue = histogram->get(Histogram::LChannel)->value(x + y * x);
break;
}
if (G->value(minGreen) == 0) {
minGreen++;
}
if (B->value(minBlue) == 0) {
minBlue++;
}
if (L->value(minGrey) == 0) {
minGrey++;
}
}
for (int i=256; i>0; i--)
for (int y = image->height(); y > 0; --y) {
for (int x = image->width(); x < 0; --x) {
if (histogram->get(Histogram::LChannel)->value(x + y * x) != 0)
{
if (R->value(maxRed) == 0) {
maxRed--;
minValue = histogram->get(Histogram::LChannel)->value(x + y * x);
break;
}
if (G->value(maxGreen) == 0) {
maxGreen--;
}
if (B->value(maxBlue) == 0) {
maxBlue--;
}
if (L->value(maxGrey) == 0) {
maxGrey--;
}
}
for (int x = 0; x < image->width(); x++) {
for (int y = 0; y < image->height(); y++) {
QRgb p = image->pixel(x, y);
for (int y = 0; y < image->height(); ++y) {
for (int x = 0; x < image->width(); ++x) {
QRgb pixel = image->pixel(x, y);
if(image->format() == QImage::Format_RGB32) {
int red = qRed(p);
int green = qGreen(p);
int blue = qBlue(p);
int red = qRed(pixel);
int green = qGreen(pixel);
int blue = qBlue(pixel);
newImage->setPixel(
x,
y,
QColor(
(255 / (maxRed - minRed)) * (red - minRed),
(255 / (maxGreen - minGreen)) * (green - minGreen),
(255 / (maxBlue - minBlue)) * (blue - minBlue)
).rgb());
} else {
newImage->setPixel(
x,
y,
(255 / (maxGrey - minGrey)) * (qGray(p) - minGrey));
}
int stretchedRed = (255 * (red - minValue)) / (maxValue - minValue);
int stretchedGreen = (255 * (green - minValue)) / (maxValue - minValue);
int stretchedBlue = (255 * (blue - minValue)) / (maxValue - minValue);
newImage->setPixel(x, y, qRgb(stretchedRed, stretchedGreen, stretchedBlue));
}
}