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

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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.