使用煤渣转换为灰度

convert to grayscale with cinder

本文关键字:灰度 转换      更新时间:2023-10-16

我正在学习有关 Cinder 的教程,您可以在其中加载图像并将其显示为cinder::gl::Texture对象。此类没有convert2Grayscale方法,因此我自己可以实现这样的事情吗?我是否可以访问分离像素,在那里我可以应用简单的算法?(访问像素实际上更重要,因为我想将其用于另一个项目)

每个像素由一个 3D 矢量 [R,G,B] 表示其中 R 是红色通道 [0,1] 中的值,G 是绿色通道 [0,1] 中的值,B 是蓝色通道 [0,1] 中的值。将 3D RGB 像素转换为标量 Y 的最简单方法是使用以下公式:

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

其中大多数系统中的伽马等于 2.2

现在,就访问

煤渣中图像的像素而言,您必须将图像加载到Surface对象上。煤渣中的表面对象具有用于访问单个像素的接口功能。请参阅这个关于如何做到这一点的惊人教程:http://www.creativeapplications.net/tutorials/images-in-cinder-tutorials-cinder/

更简单的方法,如Cinder网站上的"Hello, Cinder"教程所示:

  1. 将图像加载到 Channel 对象中,默认情况下,该对象会立即将其全部转换为灰度。
  2. 使用该通道对象初始化可在draw()方法中使用的 Surface 对象,如下所示:

    void MyApp::setup(){

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

    }

    void TutorialApp::d raw()

    {

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

    }