stretching histogram

This commit is contained in:
Damian Kowalski 2024-04-07 12:39:28 +02:00
parent 664d4e19f9
commit 7e1be18f0d

View File

@ -20,42 +20,76 @@ 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 minValue = -1; int maxRed = 255, maxGreen = 255, maxBlue = 255, maxGrey = 255;
int maxValue = -1;
for (int y = 0; y < image->height(); ++y) { int minRed = 0, minGreen = 0, minBlue = 0, minGrey = 0;
for (int x = 0; x < image->width(); ++x) {
if (histogram->get(Histogram::LChannel)->value(x + y * x) != 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++)
{ {
minValue = histogram->get(Histogram::LChannel)->value(x + y * x); if (R->value(minRed) == 0) {
break; minRed++;
} }
if (G->value(minGreen) == 0) {
minGreen++;
}
if (B->value(minBlue) == 0) {
minBlue++;
}
if (L->value(minGrey) == 0) {
minGrey++;
} }
} }
for (int y = image->height(); y > 0; --y) { for (int i=256; i>0; i--)
for (int x = image->width(); x < 0; --x) {
if (histogram->get(Histogram::LChannel)->value(x + y * x) != 0)
{ {
minValue = histogram->get(Histogram::LChannel)->value(x + y * x); if (R->value(maxRed) == 0) {
break; maxRed--;
}
}
} }
for (int y = 0; y < image->height(); ++y) { if (G->value(maxGreen) == 0) {
for (int x = 0; x < image->width(); ++x) { maxGreen--;
QRgb pixel = image->pixel(x, y); }
int red = qRed(pixel); if (B->value(maxBlue) == 0) {
int green = qGreen(pixel); maxBlue--;
int blue = qBlue(pixel); }
int stretchedRed = (255 * (red - minValue)) / (maxValue - minValue); if (L->value(maxGrey) == 0) {
int stretchedGreen = (255 * (green - minValue)) / (maxValue - minValue); maxGrey--;
int stretchedBlue = (255 * (blue - minValue)) / (maxValue - minValue); }
}
newImage->setPixel(x, y, qRgb(stretchedRed, stretchedGreen, stretchedBlue)); for (int x = 0; x < image->width(); x++) {
for (int y = 0; y < image->height(); y++) {
QRgb p = image->pixel(x, y);
if(image->format() == QImage::Format_RGB32) {
int red = qRed(p);
int green = qGreen(p);
int blue = qBlue(p);
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));
}
} }
} }