QT QImage,如何提取RGB
QT QImage, how to extract RGB?
我想从QImage中的每个像素中提取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;
}
bits()
的参考说明:
返回指向第一个像素数据的指针。这相当于scanLine(0)。
因此,如果您检查scanLine()
的参考
如果您正在访问32 bpp的图像数据,请将返回的指针强制转换为QRgb*(QRgb大小为32位),并使用它来读取/写入像素值。不能直接使用uchar*指针,因为像素格式取决于底层平台上的字节顺序。使用qRed()、qGreen()、qclue()和qAlpha()访问像素。
另一种选择可能是pixel()
成员函数。
希望能有所帮助。
使用bits()函数的问题之一是需要知道原始图像的格式。您应该使用convertToFormat将其转换为RGB。
img = img.convertToFormat(QImage::Format_RGB888);
现在,当您调用bits()时,数据将采用RGB格式,并进行适当的数据对齐。
uchar *bits = img.bits();
for (int i = 0; i < (img.width() * img.height() * 3); i++)
{
std::cout << (int) bits[i] << std::endl;
}
在Qt 5.6中引入了QColor QImage::pixelColor(int x, int y)
方法,可以直接从图像像素中获取颜色信息。
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在 C++ 中将 RGB 更改为灰度
- 在C++中使用重载提取运算符时出现问题
- 在RAW图像中提取RGB通道值的库或方法
- 提取肮脏的RGB像素缓冲区数据Directx
- 如何从窗口中获取像素数据像素缓冲区并提取RGB
- QT QImage,如何提取RGB
- 从JPEG图像中提取RGB,使用libjpeg c++库
- 在c++下在OpenCV中提取RGB通道