Hardware Freak
Registriert seit: 28.03.2003
Beiträge: 8.252
| AW: [C++] Problem bei Denkaufgabe So funktioniert es, ich weiss aber noch nicht ganz warum. Bei deiner Berechnung bekommt alpha zwar Werte, allerdings nur stark gerundete. In meiner Version kommen da viel genauere Werte raus. Mit sinkendem y-Wert wird der Unterschied dann immer gravierender, daher wohl die Verschiebung. Code: //Bei dir vermutlich anders:
//#define cimg_graphicsmagick_path 'D:\GraphicsMagick'
#include "CImg.h"
#include <iostream>
using namespace cimg_library;
using namespace std;
int main()
{
CImg<int>img("leucht.jpg");
CImgList<double>visu(img, CImg<int>(100,450,1,3,0));
CImgDisplay disp(visu,"Test");
int pw, mw, neu, i, j, valR, valG, valB, avg = 0;
int tsize = 24;
double upperOffset = tsize+25;
double lowerOffset = 450-tsize-25;
double offsetMod = 2;
double oldalpha = 0.0;
const unsigned char white[] = {255,255,255};
const unsigned char blue[] = {0,0,255};
const unsigned char lblue[] = {0,0,192};
const unsigned char dblue[] = {0,0,128};
for (double alpha = 0.5; !disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC(); )
{
for(int x=1; x<img.width()-1; x++)
{
for(int y=1; y<img.height()-1; y++)
{
pw = img(x,y,0,0);
mw = 0;
for(i=-1; i<=1; i++)
{
for(j=-1; j<=1; j++)
{
mw += img(x+i, y+j, 0, 0);
}
}
mw /= 9;
neu = (int)((pw - (alpha*mw)) / (1.0-alpha));
if(neu > 255)
{
neu = 255;
}
else if(neu < 0)
{
neu = 0;
}
visu[0](x,y,0,0) = neu;
valR = visu[0](x,y,0,0); // Read the red component
valG = visu[0](x,y,0,1); // Read the green component
valB = visu[0](x,y,0,2); // Read the blue component (Z
avg = (int)((valR + valG + valB)/3); // Compute average pixel value.
visu[0](x,y,0,0) = visu[0](x,y,0,1) = visu[0](x,y,0,2) = avg; //Graubild
}
}
visu[1].fill(0).draw_text(25,5,"Alpha",white,0,1,tsize).
draw_text(35,450-tsize-5,"%.2f",white,0,1,tsize,alpha);
visu[0].draw_line(img.width()-1,0,img.width()-1,img.height(),white,1);
const int yb = (int)((450-tsize-25)-(alpha*(450-50-(2*tsize))));
visu[1].draw_rectangle(38, 450-tsize-23, 62, tsize+23, lblue, 1).
draw_rectangle(39, 450-tsize-24, 61, tsize+24, dblue, 1).draw_rectangle(40, 450-tsize-25, 60, yb, blue, 1);
// cout << "Mouse: " << disp.mouse_y() << endl;
if ((disp.button()) && (disp.mouse_x()>=img.width() + 38) && (disp.mouse_x()<=img.width() + 62) &&
(disp.mouse_y()>=tsize + 25) && (disp.mouse_y()<=450-tsize-25))
{
alpha = ((disp.mouse_y() - lowerOffset) / ((offsetMod*upperOffset -450)/100));
oldalpha = ((disp.mouse_y() -450 +25 +tsize) / ((50 +(2*tsize) -450)/100));
alpha/= 100;
oldalpha /= 100;
if(alpha>=0.999)
{
alpha = 0.999;
}
if(oldalpha>=0.999)
{
oldalpha = 0.999;
}
cout << "alpha: " << alpha << endl;
cout << "oldalpha: " << oldalpha << endl;
}
disp.resize(disp,false).display(visu).wait();
}
} |