加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – QT QImage,如何提取RGB?

发布时间:2020-12-16 03:22:09 所属栏目:百科 来源:网络整理
导读:我想从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; }} 如何操作返回的位?我预计全部是红色的,因
我想从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;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读