从QImage获取原始数据 [英] Get raw data from QImage

查看:6584
本文介绍了从QImage获取原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QImage,我从一个像素图建立一个像下面的:

  QPixmap fullPmap = topItem-& (); 
fullPmap = fullPmap.copy(isec.toRect());
QImage chip = fullPmap.toImage();

这基本上与屏幕上的矩形相交,以将图像裁切为大小不一的图像。 >

我现在需要获取表示从芯片返回的数据的字符数组。



我该如何做?



我试过这样:

  unsigned char * data = chip.bits(); 

当我显示数据时,我得到一个完全失真的图像, / p>

fullPmap是一个RGB图像,如果这很重要。我有一些代码,我使用它将其转换为灰度:

  QRgb col 
int gray;
for(int i = 0; i {
for(int j = 0; j {
col = chip.pixel(i,j);
gray = qGray(col);
chip.setPixel(i,j,qRgb(gray,gray,gray));
}
}

我不太喜欢,



显示从bits()返回的数据如下所示:

  imwidth = chip.width(); 
imheight = chip.height();
QImage * qi = new QImage(imwidth,imheight,QImage :: Format_RGB32);
// #pragma omp parallel for
for(int i = 0; i for(int j = 0; j< imwidth; j ++)
{
qi-> setPixel(j,i,qRgb(data [i * imwidth + j],data [i * imwidth + j],data [i * imwidth + j]
}


解决方案

,您需要注意一些基本问题:




  • 像素
    数据的格式或布局是什么。从
    转换的QImage QPixmap不一定总是在
    RGB888或RGB32。调用
    QImage :: format()并处理
    不同的布局。
    特别是对于RGB32系列,
    需要确保以
    右字节顺序(endian)访问它们。


  • 字节不总是布局
    正好是许多字节来保存
    像素的行。通常,
    行比
    需要更多的字节。使用QImage :: bytesPerLine()到
    找到并推进指针
    ,当到
    下一行时,许多字节。这可能是您在显示图片建立中的问题


  • QImage :: pixel()和setPixel真的慢。如果可能,从原始像素缓冲区获取/设置像素值



I have a QImage that I built from a pixmap something like the following:

QPixmap fullPmap = topItem->pixmap();
fullPmap = fullPmap.copy(isec.toRect());
QImage chip = fullPmap.toImage();

This is basically intersecting with a rectangle on screen to crop the image to a chipped size.

I now need to get the character array representing that data back from chip.

How can I do this?

I tried something like this:

  unsigned char * data = chip.bits();

And when I display "data" I get a completely distorted image, nothing like my actual chip.

fullPmap is an RGB image if that matters. I have some code that I am using to convert it to grayscale:

QRgb col;
int gray;
for (int i = 0; i < chip.width(); ++i)
{
    for (int j = 0; j < chip.height(); ++j)
    {
        col = chip.pixel(i, j);
        gray = qGray(col);
        chip.setPixel(i, j, qRgb(gray, gray, gray));
    }
}

Which I don't really like, but it seemed like the easiest way to do such a thing.

Displaying the data that is returned from bits() looks like this:

imwidth = chip.width();
imheight = chip.height();
QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB32);
//  #pragma omp parallel for
for (int i = 0 ; i < imheight ; i++)
    for (int j = 0 ; j < imwidth ; j++)
    {
        qi->setPixel(j,i,qRgb(data[i*imwidth + j],data[i*imwidth + j],data[i*imwidth + j]));
    }

解决方案

When dealing with raw image data, you need to be aware of some basic issues:

  • What is the format or layout of pixel data. The QImage converted from QPixmap is not necessarily always in RGB888 or RGB32. Call QImage::format() and deal with different layout differently. Especially with RGB32 family, you need to make sure you access them in right byte order (endian).

  • The bytes are not always layout exactly that many bytes to hold the pixels of the line. Typically a line will have more bytes than it's needed. Use QImage::bytesPerLine() to find out and advance the pointer by that many bytes when going to the next line. This is probably your issue in the building of the display image.

  • QImage::pixel() and setPixel() are really slow. Get/set pixel values from raw pixel buffer if possible.

这篇关于从QImage获取原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆