开发者

How to convert QPixmap into a Matrix

I have been looking for such answers.

I开发者_开发百科 found this method in QPixmap transformed() though I think it does not help me in my pursuit of searching a method to convert QPixmap Images (grayscale) into a Matrix...

Thanks for the help =)


The QMatrix in the QPixmap::transformed() function is specifically for warping images.

I think what you want to do is read the values from a QPixmap into some matrix. You don't specify what grayscale means but I presume that qGray(QRgb) is sufficient if the image is not already grayscale.

I think basically something like this is what you need:

QImage myimage = mypixmap.toImage();    // convert your QPixmap to QImage
int width = myimage.width();
int height = myimage.height();
int *matrix = new int [width*height];   // store 2-D data in 1-D vector

for(int j = 0; j < height; j++)
{
  for(int i = 0; i < width; i++)
  {
    matrix[j*width+i] = qGray(myimage.pixel(i,j));
  }
}

// ... do stuff ...

delete [] matrix;

You can easily change the matrix variable into some other layout in memory if you like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜