如何在不使用opencv的情况下通过在C ++中使用原始形式的图像来降低亮度?

时间:2015-07-31 11:18:11

标签: c++ gtkmm

如果我不想使用opencv或其他东西,如何通过在C ++中读取原始图像形式来降低亮度?我尝试了很多东西,这就是我得到的东西。我非常感谢你的帮助。

void MyArea::on_brightness_increase()
{
//  m_refpixbuf1 = m_refpiximage->copy();
    nPixelvalue = m_refpixbuf1->get_pixels();

    wincenter = wincenter + 10;
    pixmax2 = wincenter + (winwidth / 2);
    pixmin2 = wincenter - (winwidth / 2);

    nimage_height = m_refpiximage->get_height();
    nimage_width = m_refpiximage->get_width();
    nimage_size = nimage_height*nimage_width*3;


    for(int i=0;i<nimage_size;i++)
    {
        if(nPixelvalue[i]<=250)
            nPixelvalue[i] +=5;
        else
            nPixelvalue[i] = 255;

        if(nPixelvalue[i+1] <= 250)
            nPixelvalue[i+1] +=5;
        else
            nPixelvalue[i+1] = 255;

        if(nPixelvalue[i+2] <= 250)
            nPixelvalue[i+2] +=5;
        else
            nPixelvalue[i+2] = 255;
    }
    get_window()->invalidate(false);
}

void MyArea::on_brightness_decrease()
{
//  m_refpixbuf1 = m_refpiximage->copy();
    nPixelvalue2 = m_refpixbuf3->get_pixels();

    wincenter = wincenter - 10;

    pixmax2 = wincenter + (winwidth / 2);
    pixmin2 = wincenter - (winwidth / 2);

    nimage_height = m_refpiximage->get_height();
    nimage_width = m_refpiximage->get_width();
    nimage_size = nimage_height*nimage_width*3;

    for (int i=0;i<nimage_size;i++)
    {
        if(nPixelvalue2[i] >=5)
            nPixelvalue2[i] -=5;
        else
            nPixelvalue2[i] = 0;

        if(nPixelvalue2[i+1] >=5)
            nPixelvalue2[i+1] -=5;
        else
            nPixelvalue2[i] = 0;
        if(nPixelvalue2[i+2] >=5)
            nPixelvalue2[i+2] -=5;
        else
            nPixelvalue2[i+2] = 0;
    }
    get_window()->invalidate(false);
}

1 个答案:

答案 0 :(得分:2)

首先,您需要做的是为图像类

创建像素的结构
struct pixel {
    public float red; // 0 = 0, 1 = 255
    public float blue; // 0 = 0, 1 = 255
    public float green; // 0 = 0, 1 = 255
    // appropriate methods, constructors
    // if you don't want hdr pixels, cap your members at 1.0
}

然后,您想要创建一种增加亮度的方法。请看这里的亮度公式:

https://en.wikipedia.org/wiki/Brightness

formula brightness

考虑一种方法,以便用来增加或减少亮度。将其添加到像素结构中,并为图像中的每个像素调用它。