QT QImage pixel manipulation

≡放荡痞女 提交于 2019-11-29 07:00:28

问题


I am building a QT GUI application and use QImage for opening images. My problem is that I can't figure out how to use QImage's bit() and scanline() methods to get access at per pixel level.

I've seen this post Qt QImage pixel manipulation problems but this is only for the first pixel of each row. Is this correct or I got it all wrong?

thanks in advance


回答1:


The scanlines correspond to the the height of image, the columns correspond to the width of the image.

According to the docs, the prototype looks like uchar* QImage::scanline(int i), or a similar const version.

But, as a commenter pointed out, because the data is dependent on the machine architecture and image, you should NOT use the uchar * directly. Instead, use something like the following:

QRgb *rowData = (QRgb*)img.scanLine(row);
QRgb pixelData = rowData[col];
int red = qRed(pixelData);



回答2:


It may not be immediately obvious from Kaleb's post, but the following works for setting a pixel on a Format_RGB32 image.

// Get the line we want
QRgb *line = (QRgb *)image->scanLine(row_index);

// Go to the pixel we want
line += col_index;

// Actually set the pixel
*line = qRgb(qRed(color), qGreen(color), qBlue(color));



回答3:


The answer did not work for me. It looks like, the data is not 32bit aligned on my system. To get the correct data, on my system i had to do this:

for(uint32_t Y = 0; Y < mHeight; ++Y)
{
    uint8_t* pPixel = Image.scanLine(Y);

    for(uint32_t X = 0; X < mWidth; ++X)
    {
      const int Blue = *pPixel++;
      const int Green = *pPixel++;
      const int Red = *pPixel++;

      uint8_t GrayscalePixel = (0.21f * Red) + (0.72f * Green) + (0.07 * Blue);
    }
}


来源:https://stackoverflow.com/questions/2095039/qt-qimage-pixel-manipulation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!