c – QT QImage,如何提取RGB?

前端之家收集整理的这篇文章主要介绍了c – QT QImage,如何提取RGB?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从Q Image中的每个像素提取RGB.理想情况下,我想使用img.bits()函数.
QImage img;

if( img.load("Red.jpg") )
{
    uchar *bits = img.bits();

    for (int i = 0; i < 12; i++)
    {
        std::cout << (int) bits[i] << std::endl;
    }
}

如何操作返回的位?我预计全部是红色的,因为图片是在Paint中创建的纯红色图像.但是,我得到36,27,237,255,36等…

解决方法

QImage img( "Red.jpg" );

if ( false == img.isNull() )
{
    QVector<QRgb> v = img.colorTable(); // returns a list of colors contained in the image's color table.

    for ( QVector<QRgb>::const_iterator it = v.begin(),itE = v.end(); it != itE; ++it )
    {
        QColor clrCurrent( *it );
        std::cout << "Red: " << clrCurrent.red()
                  << " Green: " << clrCurrent.green()
                  << " Blue: " << clrCurrent.blue()
                  << " Alpha: " << clrCurrent.alpha()
                  << std::endl;
    }
}

但是,上面的示例将返回颜色表.颜色表不包括相同的颜色两次.他们将按照外观的顺序添加一次.
如果要获取每个像素的颜色,可以使用下一行:

for ( int row = 1; row < img.height() + 1; ++row )
    for ( int col = 1; col < img.width() + 1; ++col )
    {
        QColor clrCurrent( img.pixel( row,col ) );

        std::cout << "Pixel at [" << row << "," << col << "] contains color ("
                  << clrCurrent.red() << ","
                  << clrCurrent.green() << ","
                  << clrCurrent.blue() << ","
                  << clrCurrent.alpha() << ")."
                  << std::endl;
    }
原文链接:https://www.f2er.com/c/111692.html

猜你在找的C&C++相关文章