Question

I am following a tutorial on Cinder where you load and display images as cinder::gl::Texture objects. This class has no convert2Grayscale method, so is it possible to implement something like that on my own ? Do I have access on separete pixels where I could apply a simple algorithm? (Accessing pixels is actually more important as I want to use this for another project)

Was it helpful?

Solution

Each pixel is represented by a 3D vector [R,G,B] Where R is the value in [0,1] of the red channel, G is the value in [0,1] of the green channel and B is the value in [0,1] of the blue channel. The easiest way to turn a 3D RGB pixel into a scalar Y which will represent light intensity in [0,1] (i.e., gray-scale) is to use the following formula:

Y = .2126 * R^gamma + .7152 * G^gamma + .0722 * B^gamma

Where gamma in most systems is equal to 2.2

Now as far as it concerns the access to pixels of an image in cinder you will have to load your image on a Surface object. Surface objects in cinder have interface functions for accessing individual pixels. See this amazing tutorial on how to do it: http://www.creativeapplications.net/tutorials/images-in-cinder-tutorials-cinder/

OTHER TIPS

Simpler approach, as shown in the 'Hello, Cinder' tutorials on the Cinder website:

  1. Load the image into a Channel object, which will by default convert it to grayscale all at once.
  2. Use that Channel object to initialize a Surface object that can be used in your draw() method, something like:

    void MyApp::setup() {

    Url url( "http://libcinder.org/media/tutorial/paris.jpg" );
    
    mChannel = Channel32f( loadImage( loadUrl( url ) ) );
    mTexture = mChannel;
    mDrawImage = true;
    

    }

    void TutorialApp::draw()

    {

    gl::clear( Color( 0, 0, 0 ), true );
    
    if( mDrawImage )
    {
      mTexture.enableAndBind();
      gl::draw( mTexture, getWindowBounds() );
    }
    

    }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top