我有点绝望。我需要在QT中可视化一个矩阵(在本例中是一个简单的int矩阵,它使用armadillo库,将值保持在0到240之间)。
然而,在类中使用下面的简短过程,我总是得到一个倾斜的图像。
首先,这里是我的代码
QImage matImage::matToIMage(const arma::imat &theMatrix)
{
const auto numRows(theMatrix.n_rows);
const auto numCols(theMatrix.n_cols);
uchar *datap = new uchar[numCols*numRows];
//int *datai = new int[numCols*numRows];
int idx(0);
for (auto y = 0; y < numRows; ++y)
{
for (auto x=0; x < numCols; ++x)
{
datap[idx] = static_cast<char>(theMatrix(y, x));
//datai[idx] = theMatrix(y, x);
++idx;
}
}
QVector<QRgb> grayscale;
for (size_t i = 0; i < 256; ++i)
{
grayscale.append( qRgb(i, i, i) );
}
QImage image(datap, numCols, numRows, QImage::Format_Indexed8);
image.setColorTable(grayscale);
return image;
}这里是60x82矩阵的图像,它有两个块子矩阵(确实有,数据是正确的.)

为什么图像倾斜?看起来列的数量是不正确的,但我只是看不到任何错误。
期待着得到帮助,
非常感谢,G。
P.S.:当图像放大时,所有的图像都有三个不同的灰度块。然而,他们是很难看到的。倾斜只能看到最明亮的颜色。
编辑:当从Indexed8切换到RGB32图像格式并使用setPixel时,所有操作都很好。然而,为了我的目的,我更喜欢8但是图片..。
发布于 2015-11-12 08:11:56
因为行不对齐..。QImage文档说图像数据必须对齐32位,每一行也必须对齐32位。
https://stackoverflow.com/questions/29014775
复制相似问题