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);
minValue = histogram->get(Histogram::LChannel)->value(x + y * x); QHash<int, int>* B = histogram->get(histogram->BChannel);
break; QHash<int, int>* L = histogram->get(histogram->LChannel);
}
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 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) 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 y = 0; y < image->height(); ++y) { for (int x = 0; x < image->width(); x++) {
for (int x = 0; x < image->width(); ++x) { for (int y = 0; y < image->height(); y++) {
QRgb pixel = image->pixel(x, y); QRgb p = image->pixel(x, y);
int red = qRed(pixel); if(image->format() == QImage::Format_RGB32) {
int green = qGreen(pixel); int red = qRed(p);
int blue = qBlue(pixel); int green = qGreen(p);
int blue = qBlue(p);
int stretchedRed = (255 * (red - minValue)) / (maxValue - minValue); newImage->setPixel(
int stretchedGreen = (255 * (green - minValue)) / (maxValue - minValue); x,
int stretchedBlue = (255 * (blue - minValue)) / (maxValue - minValue); y,
QColor(
newImage->setPixel(x, y, qRgb(stretchedRed, stretchedGreen, stretchedBlue)); (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));
}
} }
} }