Thread: image manipulation

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Try changing this statement:
    Code:
    tColor = tColor.addToColor(addMe);
    to
    Code:
    tColor.addToColor(addMe);
    gg

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Looks like an excellent project - working through tough problems is a good way to learn.

    gg

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    pict.setPixel(i, j, flip);
    pict.setPixel(i, j, temp);
    I think you need a 5 min. break

    gg

  9. #9
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    hey, I changed it but I'm getting an error now!

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    for ( j=0, k = (height-1);  j < k; j++, k++)
    That's not good either.

    gg

  11. #11
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Image Manipulation
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 04-16-2009, 07:19 PM
  3. Image Manipulation
    By Jason84 in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2004, 07:30 AM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  5. Image manipulation
    By Colin in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2003, 12:46 PM