Results 1 to 17 of 17

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

  1. #1
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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...

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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?

  5. #5
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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....

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  7. #7
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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...

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?

    Qt Code:
    1. for(int row = 0; row < image.height(); ++row) {
    2. for(int col = 0; col < image.width(); ++col) {
    3. QRgb px = image.pixel(col, row);
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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


    Qt Code:
    1. QTransform mat=itm2->transform();
    2. mat=QPixmap::trueMatrix(mat,scn3->width(),scn3->height());
    3. QFile file("/Users/venkateshpadmanabhan/Desktop/out2.txt");
    4. file.open(QIODevice::WriteOnly);
    5. QDataStream data(&file);
    6. data<<QTransform(mat);
    To copy to clipboard, switch view to plain text mode 


    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 ...


    Qt Code:
    1. QTransform mat=itm2->transform();
    2. mat=QPixmap::trueMatrix(mat,scn3->width(),scn3->height());
    3. QVariant qv(mat);
    4. qreal re=qv.toReal();
    5. QFile file("/Users/venkateshpadmanabhan/Desktop/out2.txt");
    6. file.open(QIODevice::WriteOnly | QIODevice::Text);
    7. QTextStream data(&file);
    8. data<<re;
    9. endl(data);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Venkatesh Padmanabhan; 11th September 2013 at 17:15.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"

    Quote Originally Posted by Venkatesh Padmanabhan View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    Venkatesh Padmanabhan (11th September 2013)

  15. #14
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"

    Last edited by Venkatesh Padmanabhan; 11th September 2013 at 18:07.

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"

    Quote Originally Posted by Venkatesh Padmanabhan View Post
    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.
    Qt Code:
    1. QTransform tform = ...;
    2. QTextStream str(...);
    3. str << tform.m11() << tform.m12() << tform.m13();
    4. str << tform.m21() << tform.m22() << tform.m23();
    5. str << tform.m31() << tform.m32() << tform.m33();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to retrieve matrix of an QImage and save it as 2 Dimensional array in".txt"

    Quote Originally Posted by ChrisW67 View Post
    @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
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Save log file to "all user\application data\"
    By stella1016 in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2011, 17:01
  2. QImage::scaled cause "raster"/"grid"
    By wesley in forum Qt Programming
    Replies: 24
    Last Post: 26th April 2010, 10:42
  3. How to save file as ".pdf" from QPrinter
    By nifei in forum Qt Programming
    Replies: 3
    Last Post: 5th March 2009, 07:33
  4. Replies: 3
    Last Post: 8th July 2008, 20:37
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.