C++ QT QImage,如何提取RGB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12382301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QT QImage, how to extract RGB?
提问by HelloWorld
I want to extract RGB from each pixel in QImage. Ideally, I want to use the img.bits() function.
我想从 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;
}
}
How to manipulate the returned bits? I expected all red because the picture is a pure red image created in Paint. However, I get 36, 27, 237, 255, 36 etc...
如何操作返回的位?我希望全是红色,因为图片是在 Paint 中创建的纯红色图像。但是,我得到 36、27、237、255、36 等...
回答by Pie_Jesu
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;
}
}
However this example above does returns the color table. Color table does not includes same colors twice. They will be added once in order of appearance.
If you want to get each pixels color, you can use next lines:
然而,上面的这个例子确实返回了颜色表。颜色表不包括两次相同的颜色。它们将按出现顺序添加一次。
如果要获取每个像素的颜色,可以使用下一行:
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;
}
回答by jrok
Reference for bits()
says:
参考bits()
说:
Returns a pointer to the first pixel data. This is equivalent to scanLine(0).
返回指向第一个像素数据的指针。这等效于 scanLine(0)。
So if you check reference for scanLine()
所以如果你检查参考scanLine()
If you are accessing 32-bpp image data, cast the returned pointer to QRgb* (QRgb has a 32-bit size) and use it to read/write the pixel value. You cannot use the uchar* pointer directly, because the pixel format depends on the byte order on the underlying platform. Use qRed(), qGreen(), qBlue(), and qAlpha() to access the pixels.
如果您正在访问 32-bpp 图像数据,请将返回的指针转换为 QRgb*(QRgb 具有 32 位大小)并使用它来读取/写入像素值。您不能直接使用 uchar* 指针,因为像素格式取决于底层平台上的字节顺序。使用 qRed()、qGreen()、qBlue() 和 qAlpha() 访问像素。
One other option would probably be pixel()
member function.
另一种选择可能是pixel()
成员函数。
Hope that helps.
希望有帮助。
回答by Keith
One of the problems with using the bits() function is that you need to know the formatof the original image. You should convert it to RGB by using convertToFormat.
使用 bits() 函数的问题之一是您需要知道原始图像的格式。您应该使用convertToFormat将其转换为 RGB 。
img = img.convertToFormat(QImage::Format_RGB888);
Now, when you call bits(), the data will be in the RGB format with the proper data alignment.
现在,当您调用 bits() 时,数据将采用具有正确数据对齐的 RGB 格式。
uchar *bits = img.bits();
for (int i = 0; i < (img.width() * img.height() * 3); i++)
{
std::cout << (int) bits[i] << std::endl;
}