How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt" file
Hi all,
I have a problem in saving QImage as 2Dimensional array in a ".txt" file. I have tried the following but it doesn't work. Would be honoured to have your guidance.
@
QPixmap p(scn3->width(),scn3->height());
QPainter paint(&p);
paint.setRenderHint(QPainter::Antialiasing);
paint.setRenderHint(QPainter::SmoothPixmapTransfor m);
scn3->render(&paint);
QImage img=p.toImage();
int width=img.width();
int height=img.height();
int matrix[width][height];
for(int j=0;j<width;j++)
{
for (int i=0;i<height;i++)
{
matrix[i][j]=qRed(img.pixel(i,j));
// matrix[j*width+i]=qGray(img.pixel(i,j));
}
}
//unsigned char* data=img.bits();
QFile f("/Users/venkateshpadmanabhan/Desktop/out1.txt");
f.open(QIODevice::WriteOnly | QIODevice::Append);
QDataStream s(&f);
s<<qint64(matrix[100][100]);
f.close();
@
I needed it to store in a file for image manipulation for medical image processing.
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Don't use QDataStream. It is not what you think.
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Hi,
Thanks for the reply, then how else can i implement it. I am struggling to find a way to store the QImage , or Pixmap as 2D Arrays which contains the co-ordinates into a ".txt" file. I have wasted my whole day to figure out a way for it...
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Assuming you actually want human readable text in your ".txt" file.... what part of the class name QDataStream implies it is in the slightest bit useful for writing a text file? Have you looked at QTextStream? Have you tried anything else? Have you thought about how the matrix elements should be arrangement in the file? Row major, column major, one element per line, integers, floating point... Have you considered that there may not be a single-line doWhatVenkateshIsThinking() function?
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Hi,
I tried QTextStream
QPixmap p(scn3->width(),scn3->height());
QPainter paint(&p);
paint.setRenderHint(QPainter::Antialiasing);
paint.setRenderHint(QPainter::SmoothPixmapTransfor m);
scn3->render(&paint);
QImage img=p.toImage();
int width=img.width();
int height=img.height();
int *matrix[width][height];
for(int j=0;j<height;j++)
{
for (int i=0;i<width;i++)
{
*matrix[width][height]=qGray(img.pixel(i,j));
QFile file("/Users/venkateshpadmanabhan/Desktop/out1.txt");
file.open(QIODevice::WriteOnly);
QTextStream tex(&file);
tex<<matrix;
file.close();
endl(tex);
}
But the application gets crashing....
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Yes. Cannot say I am surprised... you never initialise the pointer "matrix", so using it is doomed to fail. Nothing to do with QTextStream though. The matrix is entirely unnecessary for writing this file, so unless you need it for some other purpose...
Other observations (if it didn't crash):
- You open and overwrite the file once for every pixel
- You write the address of the matrix in hexadecimal to the file. This has nothing to do with the data pointed to by that pointer.
- The line "endl(tex)" makes no sense at all.
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Hi,
Yeah i observed it is pretty silly. I have rectified all the mistakes.
QPixmap p(scn3->width(),scn3->height());
QPainter paint(&p);
scn3->render(&paint);
QImage img=p.toImage();
int width=img.width();
int height=img.height();
QFile file("/Users/venkateshpadmanabhan/Desktop/out1.txt");
file.open(QIODevice::WriteOnly);
QTextStream tex(&file);
for(int j=0;j<height;j++)
{
for (int i=0;i<width;i++)
{
int s=img.pixel(i,j);
//matrix[width][height]=qGray(img.pixel(i,j));
tex<<mat;
endl(tex);
//
}
}
file.close();
I could get the output, which is the color of the pixel value. But what i wanted is, the transformation matrix of the Pixmap. I couldnt trace a way to save the Transformation Matrix of the in QTextStream. Your guidance would be really helpful...
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
You have a file full of whatever value "mat" is, nothing to do the colour of the pixel as far I can see in your code.
Neither QPixmap nor QImage has a transform matrix to extract.
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
I'm surprised that I'm doing this, however the thread has come to such point that I can't see how any progress can be made.
Two facts:
1. there is this strange QImage::pixel() call that surprisingly according to the documentation returns the colour value of a particular pixel of the image.
2. There is this weird QFile::write() function where the name suggests that it writes something in a file.
Based on those two facts it should be trivial to iterate over the rows and columns of the image, retrieve pixel colour and store it in a file. The only difficulty is to decide about the way the colour should be stored. Let's start it with the loop itself, shall we?
Code:
for(int row = 0; row < image.height(); ++row) {
for(int col = 0; col < image.width(); ++col) {
QRgb px = image.pixel(col, row);
}
}
The next step is to be able to write something in a file, for that the file needs to be open. QFile docs contains an example how to open a text file for reading so your homework is to convert the example to open the file for writing.
Finally you have to write the pixel data by putting appropriate code in the for loop from the snippet above. I have no idea what format you need the data to be and it seems you didn't bother thinking about it so far so now sit back, relax, close your eyes and think about it. Then implement it using QFile::write() call.
If you cannot assemble a working code from all my hints, I strongly encourage you to switch jobs and become e.g. a fire fighter, farmer or car salesman.
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Hi,
Thank you for your detailed response. It was wonderful learning curve for me. As i posted in my previous reply. The difference we had was
storing the Rgb value in int. Which i understood has no sense. I just altered it, and tried to store the Rgb value using name() directly into a .txt file which i get something like
[RESULT]
#676767
#595959
#646464
#5e5e5e
#737373
#7f7f7f
#9d9d9d
#b2b2b2
#a6a6a6
#afafaf
#939393
#989898
#7d7d7d
#848484[/RESULT]
Which i think suggest the values of "RRGGBB". But my actual aim was completely different. I wanted to grab the QTransform from a QGraphicsPixmapItem and write the transformation matrix as .txt file. I tried it but i am stuck here
Code:
QTransform mat=itm2->transform();
mat
=QPixmap::trueMatrix(mat,scn3
->width
(),scn3
->height
());
QFile file("/Users/venkateshpadmanabhan/Desktop/out2.txt");
data<<QTransform(mat);
I wanted to store the data as a .txt file, as you know we can't store the QTransform directly using
QTextStream s(&file);
s<<mat;
I am trying to find a method to store the transform matrix into .txt file. It would be great help if you could show lights on this issue..
Added after 18 minutes:
Hi,
I tried the following code, to convert the transform into QVariant and again convert it into qreal to save it into .txt file. But it doesn't work :( what going wrong with this ...
Code:
QTransform mat=itm2->transform();
mat
=QPixmap::trueMatrix(mat,scn3
->width
(),scn3
->height
());
qreal re=qv.toReal();
QFile file("/Users/venkateshpadmanabhan/Desktop/out2.txt");
data<<re;
endl(data);
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Quote:
Originally Posted by
Venkatesh Padmanabhan
The difference we had was storing the Rgb value in int. Which i understood has no sense.
Of course it makes sense. What you have below is also an int, just prefixed with a hash and written as a string.
Quote:
I wanted to grab the QTransform from a QGraphicsPixmapItem and write the transformation matrix as .txt file.
And what is that QTransform supposed to mean?
Quote:
I tried the following code, to convert the transform into QVariant and again convert it into qreal to save it into .txt file. But it doesn't work what going wrong with this ...
QTransform mat=itm2->transform();
mat=QPixmap::trueMatrix(mat,scn3->width(),scn3->height());
QVariant qv(mat);
qreal re=qv.toReal();
What mathematical operation do you want to perform on a matrix to convert it into a scalar?
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
The QTransform is the transformation of the QGraphicsPixmapItem of a scene where some scaling and rotating is done. I want the output as the transformation matrix of the process happening for further image processing for medical image registration application. The problem is i am not sure how to get the matrix as output in a .txt file
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
It's a regular 3x3 matrix. You can obtain its values with m11() to m33() calls.
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Quote:
Originally Posted by
Venkatesh Padmanabhan
It would be very helpful if you could explain a bit more how to implement it and push the data to QTextStream, as i couldnt find any help anywhere else.
Code:
QTransform tform = ...;
str << tform.m11() << tform.m12() << tform.m13();
str << tform.m21() << tform.m22() << tform.m23();
str << tform.m31() << tform.m32() << tform.m33();
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
@wysota: You, Sir, have more patience than I have lately. Perhaps I should give up and become a fire truck driver. ;)
Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"
Quote:
Originally Posted by
ChrisW67
@wysota: You, Sir, have more patience than I have lately.
It depends on the wind direction. You often have more patience than I do. I admit commiting oneself to a nice flame war sometimes serves me very well.
Quote:
Perhaps I should give up and become a fire truck driver. ;)
Been there, done that ;)