SFML community forums

Help => Graphics => Topic started by: Ajsgolf27 on June 09, 2014, 09:37:47 pm

Title: getpixel() not assigning the RGBA value properly
Post by: Ajsgolf27 on June 09, 2014, 09:37:47 pm
I am trying to add the red pixel values into a vector, but when I print out the vector its empty. The color value prints correctly as the loop goes so its getting the values from the pixels. Also this is extremely slow would there be a better way to do this? I am still a student so any help would be greatly appreciated.

vector <Uint8> pixelValues;
        x = myImage.getSize().x;
        y = myImage.getSize().y;
        for (Uint8 i = 0; i < x; i++){
               
                for (Uint8 j = 0; j < y; j++){

                        const Color color = myImage.getPixel(i, j);
                        pixelValues.push_back((Uint8)color.r);
                        cout << (int)color.r << endl;
                        cout << (int)color.b << endl;
                        cout << (int)color.g << endl;
                        cout <<"these are red pixels in array " << pixelValues[z++] << endl;
                }
      
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Laurent on June 09, 2014, 10:13:42 pm
You must cast it to int for printing (like you did for the color components), otherwise it will be interpreted as a character.

By the way, don't use Uint8 for i and j, you're uselessly limiting yourself to 255x255 images.
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ajsgolf27 on June 09, 2014, 10:26:01 pm
Thank you! I think this is going to take a really long time would there be a faster way?
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ixrec on June 09, 2014, 10:35:44 pm
Per-pixel processing like this is usually faster on the GPU than the CPU (since that's what GPUs are made for), but since we don't really know what you're trying to do I'm not sure what concrete methods to recommend.  https://stackoverflow.com/questions/3722325/sum-image-intensities-in-gpu might give a few hints.
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ajsgolf27 on June 09, 2014, 11:16:49 pm
Sorry I didn't specify the goal here but I'm trying to get the red , green and blue values for a set of images and save them to arrays.
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ixrec on June 09, 2014, 11:23:09 pm
That makes it sound like you just want to copy the entire image.  When you say "values" do you mean averages or sums or something?
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Jesper Juhl on June 09, 2014, 11:36:45 pm
Are you actually looking for this: sf::RenderWindow::capture (http://www.sfml-dev.org/documentation/2.1/classsf_1_1RenderWindow.php#a9bd8655d0bac83145bfc329ea7a6d538)?
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ajsgolf27 on June 09, 2014, 11:46:05 pm
In my loop I went through every pixel in my image and retrieved the red blue and green. I need to do that for every image and from there I need to take the same pixel lets say myImage1[0][0], myImage2[0][0] average them and put into an new array and display the new image
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ixrec on June 10, 2014, 12:04:37 am
I think you can achieve that just by drawing both images to an sf::RenderTexture with the right blend mode and/or transparencies.  Look up the tutorial on render textures and the documentation on blend modes (note that custom blend modes have been added since the 2.1 release).
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ajsgolf27 on June 10, 2014, 01:19:44 am
I'm not entirely sure if that will achieve what I am looking for but it's worth a try thanks for you help!
Title: Re: getpixel() not assigning the RGBA value properly
Post by: Ixrec on June 10, 2014, 07:55:44 am
If none of the blend modes are quite right, then try using a shader to draw to the render texture.  What you want can definitely be done with a very simple shader.