-
1 Attachment(s)
image manipulation
hello all, I'm having a lot of trouble with this assignment. I'm supposed to make an image manipulator using CxImage library...I run into a problem when I want to, for example brighten/darken the image. This will be quite difficult to grasp from the code I will post here, and the whole zipped file along with the libraries and such is too big to attach so I will only attach my .cpp file, any how, here is the brighten function:
Code:
void applyBrighten(Picture& pict)
{
int width = pict.getWidth();
int height = pict.getHeight();
int addMe = 50;
Color tColor(0, 0, 0);
for (int i=0; i < width; i++){
for (int j=0; j < height; j++){
tColor = pict.getPixel(i, j);
tColor = tColor.addToColor(addMe);
pict.setPixel(i, j, tColor);
}
}
}
I think that the problem is with pict.setPixel() function. Here is how it is defined:
Code:
void setPixel(int i, int j, const Color& c) {matrix[i][j] = c; }
I'm sure that this is very difficult to grasp by just looking at these snippets, so if anyone is willing to take some time to look at the complete program I would greatly appreciate it. If you want me to send the other files, libraries please say so,
axon
-
Try changing this statement: Code:
tColor = tColor.addToColor(addMe);
to Code:
tColor.addToColor(addMe);
gg
-
Thanks for the response codeplug, but that does not change anything...it gives the exact same effect. When I do run that line what I get is that the picture does, indeed get manipulated, but not the way I want it to.
That void setPixel() function seems to be right as it does work when I run my function to make a 'negative' of the picture:
Code:
void applyNegative(Picture& pict)
{
int width = pict.getWidth();
int height = pict.getHeight();
Color negative( 0, 0, 0 );
for (int i=0; i < width; i++){
for (int j=0; j < height; j++){
negative = pict.getPixel(i, j);
pict.setPixel(i, j, negative.complement());
}
}
So I have no idea why the brighten/darken function don't work?! Maybe I'm not sepperating all the color chanells properly, but then how come does the neative function work? For the first time in this class I am trully lost....
axon
-
The Color method "constrain()" does not modify any member variables - it simply returns the constrained value. Try changing your addToColor() method to this:
Code:
Color addToColor(int add)
{
r = constrain(r + add, 0, 255);
g = constrain(g + add, 0, 255);
b = constrain(b + add, 0, 255);
return *this;
}
gg
-
YES!
THANKS A MILLION CODEPLUG...this did the trick...I mean it thanks a lot, now I just have to disect what you did to understand it. If you will be reading this post again could you answer this question: Do you think that this program could be a little much for a first programming/c++ class? (CS 102)
axon
-
Looks like an excellent project - working through tough problems is a good way to learn.
gg
-
It is a very cool project, but we have not covered 50% of what we are supposed to do here in class. So that is why I am having trouble...
If you have still some patiance Codeplug, could you help me with the Flip upside-down function, I can do it, when the image is grayscale, (2D) by doing something like this:
Code:
void applyUpsideDown(float pict[][Height])
{
for (int i=0; i < Width; i++) {
for (int j=0, k=Height-1; j < k; j++, k--) {
float temp = pict[i][j];
pict[i][j] = pict[i][k];
pict[i][k] = temp;
}
}
}
and this does it. But when I want to do a similar thing with the color image it seems not to be working. I'm not exactly sure if I'm even on the right track here is the code so far:
Code:
void applyUpsideDown(Picture& pict)
{
int width = pict.getWidth();
int height = pict.getHeight();
int i, j, k;
Color flip( 0, 0, 0 );
for ( i=0; i < width; i++){
for ( j=0, k = (height-1); j < k; j++, k++){
flip = pict.getPixel(i, j);
Color temp = flip;
flip = pict.getPixel(i, k);
pict.setPixel(i, j, flip);
pict.setPixel(i, j, temp);
}
}
}
axon
-
Code:
pict.setPixel(i, j, flip);
pict.setPixel(i, j, temp);
I think you need a 5 min. break :)
gg
-
hey, I changed it but I'm getting an error now!
axon
-
Code:
for ( j=0, k = (height-1); j < k; j++, k++)
That's not good either.
gg
-
THANKS for all the help codeplug...I've figure everything out by Monday (due day), I'll post it here in a few days...some of the things are really pretty cool! just for completeness here is how I did the upside down. Its weird how this was herder for me then convolution matrix problem!
Code:
void applyUpsideDown(Picture& pict)
{
for (int i=0; i < pict.getWidth(); i++) {
for (int j=0, k=pict.getHeight()-1; j < k; j++, k--) {
Color temp = pict.getPixel(i,j);
pict.setPixel(i, j, pict.getPixel(i,k) );
pict.setPixel(i, k, temp );
}
}
}
axon
-
By the way:
Just adding a constant to the RGB value is not a good way of increasing brightness. The colors will get messed up.
http://www.cprogramming.com/cboard/s...ighlight=color