【发布时间】:2020-06-05 13:20:23
【问题描述】:
我有一个 csv 文件(128 列和 2815 行),其中的值范围从 0 到 1。我想将其显示为灰度图像。我该怎么办?在 matlab 中,我可以读取 csv 文件并调用 imagesc 函数并将颜色图设置为灰色以将其显示为灰度图像,同样也可以在 Opencv 中完成。在 Qt 中,我应该怎么做才能在 Qgraphicsview 中显示它。我是 Qt 新手,谁能用最简单的形式描述它。
这是我尝试实现的代码。
imshow("image", ImageOutf);
QImage imageObject3(ImageOutf.data, ImageOutf.cols, ImageOutf.rows, ImageOutf.step, QImage::Format_Grayscale16);
scene1 = new QGraphicsScene(this);
image = QPixmap::fromImage(imageObject3);
scene1->addPixmap(QPixmap::fromImage(imageObject3));
ui->graphicsView->setScene(scene1);
ui->graphicsView->setBackgroundBrush(QBrush(QColor(16,16,16), Qt::SolidPattern));
ui->graphicsView->fitInView((scene1->itemsBoundingRect()));
"ImageOutf" 是 cv Mat 对象,它有 128 列和 2815 行。 我附上了我得到的输出的截图。我没有得到相同的图像。谁能解释为什么它不同。
** 注意:我的原始文件是一个 csv 文件。我首先将其转换为向量或矩阵的向量,然后将矩阵转换为 opencv mat 对象,然后将 mat 转换为 QImage,然后我尝试显示 qimage。我在下面发布代码:
1) CSV 转矩阵
using vec = vector<float>;
using matrix = vector<vec>;
matrix readCSV(string filename)
{
matrix M;
ifstream in(filename);
string line;
while (getline(in, line)) // read a whole line of the file
{
stringstream ss(line); // put it in a stringstream (internal stream)
vec row;
string data;
while (getline(ss, data, ',')) // read (string) items up to a comma
{
row.push_back(stof(data));
}
if (row.size() > 0) M.push_back(row); // add non-empty rows to matrix
}
return M;
}
2) 矩阵转矩阵
Mat matrixtoMAT(const matrix& M, int R, int C)
Mat Img_Data;
Img_Data = Mat::zeros(R, C, CV_32FC1);
for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < M[i].size(); j++)
{
Img_Data.at<float>(i, j) = M[i][j];
}
}
return Img_Data;
}
3) Mat to QImage
QImage imageObject3(ImageOutf.data, ImageOutf.cols, ImageOutf.rows,ImageOutf.step, QImage::Format_Indexed8);
【问题讨论】:
标签: matlab qt opencv opencv3.0 qimage