I have a csv file(128 columns and 2815 rows), where the values ranges from 0 to 1. I want to display it as a grey scaled image. What should I do? In matlab i can just read the csv file and call imagesc function and set the colormap to grey to display it as greyscale image and similarly it can also be done in Opencv. In Qt what should i do to display it in Qgraphicsview. I am new to Qt can anyone describe it in the simplest form.
Here is the code i have tried to implement.
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" is the cv Mat object which has 128 columns and 2815 rows.
I am attaching the screenshot of the outputs i am getting. I am not getting the same image. Can anyone explain why it is different.
** Note : My raw file is a csv file. I first converted it to vector of vector or matrix then the matrix to opencv mat object then mat to QImage and then i tried to display the qimage. I am posting the codes below:
1) CSV to Matrix
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) Matrix to Mat
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);