Compare commits
No commits in common. "96129bc8ab78265908c436bdcf19fe69d206d5d4" and "664d4e19f9d409bf10f1b7a336e8acbac23c3d7a" have entirely different histories.
96129bc8ab
...
664d4e19f9
@ -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)
|
||||||
@ -14,77 +14,44 @@ HistogramEqualization::HistogramEqualization(PNM* img, ImageViewer* iv) :
|
|||||||
|
|
||||||
PNM* HistogramEqualization::transform()
|
PNM* HistogramEqualization::transform()
|
||||||
{
|
{
|
||||||
float width = image->width();
|
int width = image->width();
|
||||||
float 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());
|
||||||
Histogram* histogram = image->getHistogram();
|
|
||||||
|
|
||||||
QHash<int, int>* R = histogram->get(Histogram::RChannel);
|
// Tworzymy histogram dla obecnego obrazu
|
||||||
QHash<int, int>* G = histogram->get(Histogram::GChannel);
|
Histogram* hist = image->getHistogram(); // Nie przekazujemy argumentów
|
||||||
QHash<int, int>* B = histogram->get(Histogram::BChannel);
|
|
||||||
QHash<int, int>* L = histogram->get(Histogram::LChannel);
|
|
||||||
|
|
||||||
// Probability tables
|
// Pobieramy łączną liczbę pikseli na obrazie
|
||||||
float redProb[255];
|
int totalPixels = width * height;
|
||||||
float greenProb[255];
|
|
||||||
float blueProb[255];
|
|
||||||
float greyProb[255];
|
|
||||||
|
|
||||||
float pixelSum = width * height;
|
// Tworzymy dystrybuantę
|
||||||
for (int i = 0; i < 256; i++)
|
QVector<double> cdf(256, 0.0); // Inicjalizujemy wszystkie wartości na 0.0
|
||||||
{
|
double sum = 0.0;
|
||||||
redProb[i] = (R->value(i)) / (pixelSum);
|
for (int i = 0; i < 256; ++i) {
|
||||||
greenProb[i] = (G->value(i)) / (pixelSum);
|
sum += hist->get(Histogram::LChannel)->value(i) / static_cast<double>(totalPixels); // Używamy kanału luminancji
|
||||||
blueProb[i] = (B->value(i)) / (pixelSum);
|
cdf[i] = sum;
|
||||||
greyProb[i] = (L->value(i)) / (pixelSum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// D look-up
|
// Normalizujemy dystrybuantę do przedziału [0, 255]
|
||||||
float redD[255];
|
QVector<int> lookupTable(256, 0);
|
||||||
float greenD[255];
|
for (int i = 0; i < 256; ++i) {
|
||||||
float blueD[255];
|
lookupTable[i] = round(cdf[i] * 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int x = 0; x < image->width(); x++)
|
// Przeprowadzamy wyrównywanie histogramu
|
||||||
{
|
for (int y = 0; y < height; ++y) {
|
||||||
for (int y = 0; y < image->height(); y++)
|
for (int x = 0; x < width; ++x) {
|
||||||
{
|
|
||||||
QRgb pixel = image->pixel(x, y);
|
QRgb pixel = image->pixel(x, y);
|
||||||
if (image->format() == QImage::Format_RGB32) {
|
int grayValue = qGray(pixel); // Pobieramy wartość odcienia szarości
|
||||||
newImage->setPixel(
|
|
||||||
x,
|
// Używamy wartości z lookupTable jako nowej wartości odcienia szarości
|
||||||
y,
|
int newGrayValue = lookupTable[grayValue];
|
||||||
QColor(
|
QRgb newPixel = qRgb(newGrayValue, newGrayValue, newGrayValue); // Ustawiamy nową wartość dla każdego kanału RGB
|
||||||
redD[qRed(pixel)] * 255,
|
|
||||||
greenD[qGreen(pixel)] * 255,
|
// Ustawiamy nowy piksel na obrazie docelowym
|
||||||
blueD[qBlue(pixel)] * 255
|
newImage->setPixel(x, y, newPixel);
|
||||||
).rgb());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
newImage->setPixel(x, y, greyD[qGray(pixel)] * 255);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,76 +20,42 @@ PNM* HistogramStretching::transform()
|
|||||||
PNM* newImage = new PNM(width, height, image->format());
|
PNM* newImage = new PNM(width, height, image->format());
|
||||||
Histogram* histogram = image->getHistogram();
|
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;
|
for (int y = 0; y < image->height(); ++y) {
|
||||||
|
for (int x = 0; x < image->width(); ++x) {
|
||||||
QHash<int, int>* R = histogram->get(histogram->RChannel);
|
if (histogram->get(Histogram::LChannel)->value(x + y * x) != 0)
|
||||||
QHash<int, int>* G = histogram->get(histogram->GChannel);
|
{
|
||||||
QHash<int, int>* B = histogram->get(histogram->BChannel);
|
minValue = histogram->get(Histogram::LChannel)->value(x + y * x);
|
||||||
QHash<int, int>* L = histogram->get(histogram->LChannel);
|
break;
|
||||||
|
}
|
||||||
for (int i=0; i<256; i++)
|
|
||||||
{
|
|
||||||
if (R->value(minRed) == 0) {
|
|
||||||
minRed++;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (R->value(maxRed) == 0) {
|
if (histogram->get(Histogram::LChannel)->value(x + y * x) != 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) {
|
||||||
for (int y = 0; y < image->height(); y++) {
|
for (int x = 0; x < image->width(); ++x) {
|
||||||
QRgb p = image->pixel(x, y);
|
QRgb pixel = image->pixel(x, y);
|
||||||
|
|
||||||
if(image->format() == QImage::Format_RGB32) {
|
int red = qRed(pixel);
|
||||||
int red = qRed(p);
|
int green = qGreen(pixel);
|
||||||
int green = qGreen(p);
|
int blue = qBlue(pixel);
|
||||||
int blue = qBlue(p);
|
|
||||||
|
|
||||||
newImage->setPixel(
|
int stretchedRed = (255 * (red - minValue)) / (maxValue - minValue);
|
||||||
x,
|
int stretchedGreen = (255 * (green - minValue)) / (maxValue - minValue);
|
||||||
y,
|
int stretchedBlue = (255 * (blue - minValue)) / (maxValue - minValue);
|
||||||
QColor(
|
|
||||||
(255 / (maxRed - minRed)) * (red - minRed),
|
newImage->setPixel(x, y, qRgb(stretchedRed, stretchedGreen, stretchedBlue));
|
||||||
(255 / (maxGreen - minGreen)) * (green - minGreen),
|
|
||||||
(255 / (maxBlue - minBlue)) * (blue - minBlue)
|
|
||||||
).rgb());
|
|
||||||
} else {
|
|
||||||
newImage->setPixel(
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
(255 / (maxGrey - minGrey)) * (qGray(p) - minGrey));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user