Zadanie_03_ready

This commit is contained in:
Dawid_Kreft 2020-03-22 19:09:14 +01:00
parent ca19e6fe34
commit c618954323
5 changed files with 160 additions and 8 deletions

View File

@ -25,7 +25,7 @@ Histogram::~Histogram()
void Histogram::generate(QImage* image)
{
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
qDebug() << Q_FUNC_INFO << " Generating histogram !";
for(int i = 0 ; i < image->height(); i++){
for(int j = 0 ; j < image->width() ; j++){
QColor *clrCurrent = new QColor( image->pixel(i,j) );
@ -35,18 +35,88 @@ void Histogram::generate(QImage* image)
L->insert(clrCurrent->alpha(),R->value(clrCurrent->alpha())+1);
}
}
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
}
/** Returns the maximal value of the histogram in the given channel */
int Histogram::maximumValue(Channel selectedChannel = RGB)
{
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
int max = 0;
if(selectedChannel == LChannel){
return getMaxFromQHashHelper(get(selectedChannel));
}
return 0;
max = getMaxFromQHashHelper(R);
if(getMaxFromQHashHelper(G) > max){
max = getMaxFromQHashHelper(G);
}
if(getMaxFromQHashHelper(B) > max){
max = getMaxFromQHashHelper(B);
}
return max;
}
// Return max or -1 when QHash is empty
int Histogram::getMaxFromQHashHelper( QHash<int, int> * channel ){
int max = -1;
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.value() > max){
max = iterator.value();
}
++iterator;
}
return max;
}
int Histogram::getMaxKey( QHash<int, int> * channel ){
int max = -1;
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.key() > max){
max = iterator.key();
}
++iterator;
}
return max;
}
int Histogram::getMinKey( QHash<int, int> * channel ){
int min = 256;
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.key() < min){
min = iterator.key();
}
++iterator;
}
return min;
}
//Return min or 256 when qhash is empty
int Histogram::getMinFromQHashHelper( QHash<int, int> * channel ){
int min = 256;
QHash<int, int>::const_iterator iterator = channel->begin();
while (iterator != channel->end())
{
if(iterator.value() < min){
min = iterator.value();
}
++iterator;
}
return min;
}

View File

@ -16,10 +16,15 @@ public:
QHash<int, int>* get(Histogram::Channel);
QImage getImage(Histogram::Channel, QBrush);
int maximumValue(Channel);
int getMaxFromQHashHelper( QHash<int, int> *);
int getMinFromQHashHelper( QHash<int, int> *);
int getMinKey( QHash<int, int> *);
int getMaxKey( QHash<int, int> *);
private:
void generate(QImage*); // iterates all parent image pixels and set the Hashes
QHash<int, int>* R;
QHash<int, int>* G;
QHash<int, int>* B;

View File

@ -14,13 +14,41 @@ 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();
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
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));
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++) {
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));
// 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());
}
}
return newImage;
}

View File

@ -0,0 +1,48 @@
#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;
}

View File

@ -6,6 +6,7 @@
class HistogramStretching : public Transformation
{
public:
int const MAX_VALUE = 255;
HistogramStretching(PNM*);
HistogramStretching(PNM*, ImageViewer* iv);