PDA

View Full Version : How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt" file



Venkatesh Padmanabhan
10th September 2013, 18:51
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.

wysota
10th September 2013, 20:25
Don't use QDataStream. It is not what you think.

Venkatesh Padmanabhan
10th September 2013, 21:16
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...

ChrisW67
10th September 2013, 22:48
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?

Venkatesh Padmanabhan
10th September 2013, 22:56
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....

ChrisW67
11th September 2013, 00:36
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.

Venkatesh Padmanabhan
11th September 2013, 02:51
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...

ChrisW67
11th September 2013, 04:16
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.

wysota
11th September 2013, 06:48
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?


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.

Venkatesh Padmanabhan
11th September 2013, 13:26
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


#676767
#595959
#646464
#5e5e5e
#737373
#7f7f7f
#9d9d9d
#b2b2b2
#a6a6a6
#afafaf
#939393
#989898
#7d7d7d
#848484

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



QTransform mat=itm2->transform();
mat=QPixmap::trueMatrix(mat,scn3->width(),scn3->height());
QFile file("/Users/venkateshpadmanabhan/Desktop/out2.txt");
file.open(QIODevice::WriteOnly);
QDataStream data(&file);
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 ...



QTransform mat=itm2->transform();
mat=QPixmap::trueMatrix(mat,scn3->width(),scn3->height());
QVariant qv(mat);
qreal re=qv.toReal();
QFile file("/Users/venkateshpadmanabhan/Desktop/out2.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream data(&file);
data<<re;
endl(data);

wysota
11th September 2013, 13:57
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.


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?


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?

Venkatesh Padmanabhan
11th September 2013, 14:11
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

wysota
11th September 2013, 15:15
It's a regular 3x3 matrix. You can obtain its values with m11() to m33() calls.

Venkatesh Padmanabhan
11th September 2013, 17:07
Hi,

Thank you so much for your valuable guidance. 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. I am trying to find a solution for past two days....

Added after 44 minutes:

Hi,

I implemented it but i am not sure whether my code is correct. Is there any possibility to verify it ??

QTransform mat1=itm2->transform();
QTransform mat=QPixmap::trueMatrix(mat1,scn3->width(),scn3->height());

mat.dx();
mat.dy();

mat.m11();
mat.m12();
mat.m13();
mat.m21();
mat.m22();
mat.m23();
mat.m31();
mat.m32();
mat.m33();

QVariant QV(mat.dx());
QVariant QV1(mat.dy());

QVariant qv(mat.m11());
QVariant qv1(mat.m12());
QVariant qv2(mat.m13());
QVariant qv3(mat.m21());
QVariant qv4(mat.m22());
QVariant qv5(mat.m23());
QVariant qv6(mat.m31());
QVariant qv7(mat.m32());
QVariant qv8(mat.m33());
qreal re=qv.toReal();
qreal re1=qv1.toReal();
qreal re2=qv2.toReal();
qreal re3=qv3.toReal();
qreal re4=qv4.toReal();
qreal re5=qv5.toReal();
qreal re6=qv6.toReal();
qreal re7=qv7.toReal();
qreal re8=qv8.toReal();

qreal RE=QV.toReal();
qreal RE1=QV1.toReal();

QFile file("/Users/venkateshpadmanabhan/Desktop/out2.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream data(&file);
data<<re;
data<<re1;
data<<re2;

endl(data);

data<<re3;
data<<re4;
data<<re5;

endl(data);

data<<re6;
data<<re7;
data<<re8;

endl(data);


data<<RE;
data<<RE1;

endl(data);
file.close();

wysota
11th September 2013, 21:21
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.


QTransform tform = ...;
QTextStream str(...);
str << tform.m11() << tform.m12() << tform.m13();
str << tform.m21() << tform.m22() << tform.m23();
str << tform.m31() << tform.m32() << tform.m33();

ChrisW67
11th September 2013, 23:05
@wysota: You, Sir, have more patience than I have lately. Perhaps I should give up and become a fire truck driver. ;)

wysota
11th September 2013, 23:15
@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.


Perhaps I should give up and become a fire truck driver. ;)

Been there, done that ;)