Results 1 to 17 of 17

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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.


Similar Threads

  1. Save log file to "all user\application data\"
    By stella1016 in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2011, 16:01
  2. QImage::scaled cause "raster"/"grid"
    By wesley in forum Qt Programming
    Replies: 24
    Last Post: 26th April 2010, 09:42
  3. How to save file as ".pdf" from QPrinter
    By nifei in forum Qt Programming
    Replies: 3
    Last Post: 5th March 2009, 06:33
  4. Replies: 3
    Last Post: 8th July 2008, 19:37
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19: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.