Zadanie 3 ready with all changes

This commit is contained in:
Dawid_Kreft 2020-03-22 19:10:02 +01:00
parent c618954323
commit ebc28216cd
5 changed files with 80 additions and 70 deletions

View File

@ -29,10 +29,12 @@ void Histogram::generate(QImage* image)
for(int i = 0 ; i < image->height(); i++){
for(int j = 0 ; j < image->width() ; j++){
QColor *clrCurrent = new QColor( image->pixel(i,j) );
R->insert(clrCurrent->red(),R->value(clrCurrent->red())+1);
G->insert(clrCurrent->green(),R->value(clrCurrent->green())+1);
B->insert(clrCurrent->blue(),R->value(clrCurrent->blue())+1);
L->insert(clrCurrent->alpha(),R->value(clrCurrent->alpha())+1);
QRgb pixel = image->pixel(i,j);
R->insert(qRed(pixel),R->value(qRed(pixel))+1);
G->insert(qGreen(pixel),G->value(qGreen(pixel))+1);
B->insert(qBlue(pixel),B->value(qBlue(pixel))+1);
L->insert(qAlpha(pixel),L->value(qAlpha(pixel))+1);
}
}
}
@ -65,7 +67,7 @@ int Histogram::getMaxFromQHashHelper( QHash<int, int> * channel ){
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.value() > max){
if(iterator.value() > max && iterator.value() != 0 ){
max = iterator.value();
}
++iterator;
@ -80,7 +82,7 @@ int Histogram::getMaxKey( QHash<int, int> * channel ){
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.key() > max){
if(iterator.key() > max && iterator.value() != 0){
max = iterator.key();
}
++iterator;
@ -95,7 +97,7 @@ int Histogram::getMinKey( QHash<int, int> * channel ){
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.key() < min){
if(iterator.key() < min && iterator.value() != 0){
min = iterator.key();
}
++iterator;
@ -111,7 +113,7 @@ int Histogram::getMinFromQHashHelper( QHash<int, int> * channel ){
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.value() < min){
if(iterator.value() < min && iterator.value() != 0){
min = iterator.value();
}
++iterator;

View File

@ -16,11 +16,73 @@ PNM* HistogramEqualization::transform()
{
int width = image->width();
int height = image->height();
int pixelAmounts = width* height;
PNM* newImage = new PNM(width, height, image->format());
double LUTr[256], LUTg[256], LUTb[256],
redHist[256], greenHist[256], blueHist[256],
distRed[256], distGreen[256], distBlue[256];
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
zeros(redHist,greenHist,blueHist);
zeros(distRed,distGreen,distBlue);
zeros(LUTr,LUTb,LUTg);
for (int x = 0; x < width; x++){
for (int y = 0; y < height; y++)
{
QRgb pixel = image->pixel(x,y);
redHist[qRed(pixel)]++;
greenHist[qGreen(pixel)]++;
blueHist[qBlue(pixel)]++;
}
}
double sumR = 0;
double sumG = 0;
double sumB = 0;
for (int i=0; i<256; i++)
{
sumR += (redHist[i]/pixelAmounts);
sumG += (greenHist[i]/pixelAmounts);
sumB += (blueHist[i]/pixelAmounts);
distRed[i] += sumR;
distGreen[i] += sumG;
distBlue[i] += sumB;
}
prepareLUTperChannel(distRed, LUTr);
prepareLUTperChannel(distGreen, LUTg );
prepareLUTperChannel(distBlue, LUTb );
for (int i = 0; i< width; i++){
for (int j = 0; j< height; j++)
{
QRgb pixel = image->pixel(i,j);
newImage->setPixel(i,j, QColor( LUTr[qRed(pixel)], LUTg[qGreen(pixel)], LUTb[qBlue(pixel)]).rgba() );
}
}
return newImage;
}
void HistogramEqualization::prepareLUTperChannel(double *D, double *LUT)
{
double Dmin = 0;
for(int i = 0; i < 256 ; i++){
if(D[i+1] > 0){
Dmin = D[i+1];
break;
}
}
for (int i=0; i<256; i++){
LUT[i] = ( 255* ((D[i] - Dmin) / (1 - Dmin))) ;
}
}
void HistogramEqualization::zeros(double * r_tab, double * g_tab, double* b_tab){
for (int i=0; i<256; i++)
{
r_tab[i] = 0;
g_tab[i] = 0;
b_tab[i] = 0;
}
}

View File

@ -8,8 +8,12 @@ class HistogramEqualization : public Transformation
public:
HistogramEqualization(PNM*);
HistogramEqualization(PNM*, ImageViewer*);
QHash<int, int> prepareDistrubuantPerChannel(QHash <int,int> * , int);
void prepareLUTperChannel(double *, double * );
void zeros(double *, double * , double * );
virtual PNM* transform();
int min(QHash<int, int> *dist);
};

View File

@ -14,10 +14,9 @@ HistogramStretching::HistogramStretching(PNM* img, ImageViewer* iv) :
PNM* HistogramStretching::transform()
{
qDebug() << Q_FUNC_INFO << "START";
int width = image->width();
int height = image->height();
PNM* newImage = new PNM(width, height, image->format());
Histogram * histogram = image->getHistogram();
@ -30,10 +29,6 @@ PNM* HistogramStretching::transform()
int maxBlue = histogram->getMaxKey(histogram->get(histogram->BChannel));
int minBlue = histogram->getMinKey(histogram->get(histogram->BChannel));
int maxAlpha = histogram->getMaxKey(histogram->get(histogram->LChannel));
int minAlpha = histogram->getMinKey(histogram->get(histogram->LChannel));
qDebug() << Q_FUNC_INFO << "LOOP";
for(int x = 0 ; x < width; x++){
for (int y = 0 ; y < height; y++) {
@ -41,11 +36,6 @@ PNM* HistogramStretching::transform()
int g = (qGreen(pixel) - minGreen) * (MAX_VALUE/(maxGreen-minGreen));
int r = (qRed(pixel) - minRed) * (MAX_VALUE/(maxRed-minRed));
int b = (qBlue(pixel) - minBlue) * (MAX_VALUE/(maxBlue-minBlue));
// int a = (qAlpha(pixel) - minAlpha) * (MAX_VALUE/(maxAlpha-minAlpha));
qDebug() << Q_FUNC_INFO << "1. Here is working !!";
QRgb blee = QColor(r,g,b).rgba();
qDebug() << Q_FUNC_INFO << "2. Here is workingt too !!";
newImage->setPixel(x,y,QColor(r,g,b).rgba());
}
}

View File

@ -1,48 +0,0 @@
#include "histogram_stretching.h"
#include "../histogram.h"
HistogramStretching::HistogramStretching(PNM* img) :
Transformation(img)
{
}
HistogramStretching::HistogramStretching(PNM* img, ImageViewer* iv) :
Transformation(img, iv)
{
}
PNM* HistogramStretching::transform()
{
qDebug() << Q_FUNC_INFO << "START";
int width = image->width();
int height = image->height();
PNM* newImage = new PNM(width, height, image->format());
Histogram * histogram = image->getHistogram();
int maxRed = histogram->getMaxKey(histogram->get(histogram->RChannel));
int minRed = histogram->getMinKey(histogram->get(histogram->RChannel));
int maxGreen = histogram->getMaxKey(histogram->get(histogram->GChannel));
int minGreen = histogram->getMinKey(histogram->get(histogram->GChannel));
int maxBlue = histogram->getMaxKey(histogram->get(histogram->BChannel));
int minBlue = histogram->getMinKey(histogram->get(histogram->BChannel));
qDebug() << Q_FUNC_INFO << "LOOP";
for(int x = 0 ; x < width; x++){
for (int y = 0 ; y < height; y++) {
QRgb pixel = image->pixel(x,y);
int g = (qGreen(pixel) - minGreen) * (MAX_VALUE/(maxGreen-minGreen));
int r = (qRed(pixel) - minRed) * (MAX_VALUE/(maxRed-minRed));
int b = (qBlue(pixel) - minBlue) * (MAX_VALUE/(maxBlue-minBlue));
newImage->setPixel(x,y,QColor(r,g,b).rgba());
}
}
return newImage;
}