0

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.enter image description here

** 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);
G93
  • 21
  • 1
  • 3

1 Answers1

0

I'm not familiar with imagesc but from what you are saying it's basically a colour map. It doesn't matter if it's Matlab or Qt, or OpenCV or whatever - you just need to create an image with the given dimensions (in your case 128x2815) and render it.

For how to display a QImage in a QGraphicsScene (note that a QGraphicsView is just the viewport that renders a visible region of a QGraphicsScene; you can have multiple views for the same scene) there are plenty of resources (example). As for how to create a QImage - the official documentation for it tells you how to do that. Note that just like in OpenCV etc. you have a one dimensional array for the data. That is why giving the dimensions is so important so that Qt would know how to distribute the one-dimensional data in two dimensions as pixel values. If you are looking for mapping values between 0 and 1 to 0 and 255 - this is a relatively simple mathematical task. ;)

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • Thanks for the replay. I have updated the questions with the code i tried to implement. The images displayed are not the same. can you please explain what went wrong. – G93 Feb 21 '20 at 06:46
  • @G93 It looks like a color format issue. I think you should use `QImage::Format_Indexed8` or similar. I also tend to explicitly cast the OpenCV image data to `(const unsigned char*)`. – rbaleksandar Feb 21 '20 at 06:54
  • Its not solving the problem. – G93 Feb 21 '20 at 15:22
  • Please post the image file itself. If you provide a minimal working example that reproduces your problem it would be even better. – rbaleksandar Feb 21 '20 at 18:37
  • Actually, My raw file is a csv file. I converted it to Opencv mat, then mat to qimage and then i tried to display it. I will update my post with the code. – G93 Feb 21 '20 at 19:01