[solved]How to save pixmap's transformation matrix as ".txt"



  • Hi,

    I would like to save the transformation matrix of a pixmap from a QGraphicsScene in a .txt file, i was trying to find a method to implement it but, i couldnt find a working way. This is the code i tried.

    @

    QTransform tr;
    QPixmap::trueMatrix(tr,width,height);
    QFile file("/Users/venkateshpadmanabhan/Desktop/out.txt");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out << (QTransform)tr;

    @

    i also tried to implement through

    @

    QPixmap p(scn3->width(),scn3->height());
    QPainter paint(&p);
    paint.setRenderHint(QPainter::Antialiasing);
    paint.setRenderHint(QPainter::SmoothPixmapTransform);
    scn3->render(&paint);
    QImage img=p.toImage();
    int width=img.width();
    int height=img.height();

          int *matrix=new int [width*height];
    
    
            for(int j=0;j<height;j++)
            {
                for (int i=0;i<width;i++)
                {
                    matrix[j*width+i]=qGray(img.pixel(i,j));
                  
                }
            }
            QFile file&#40;"/Users/venkateshpadmanabhan/Desktop/out.txt"&#41;;
             file.open(QIODevice::WriteOnly);
             QDataStream out(&file);
             out << (qint64)matrix;
    delete[]matrix;
    

    @

    But i couldn't get the matrix as output in both the codes :( would love to have your suggestions for implementation.

    Thanks...


  • Lifetime Qt Champion

    [quote author="venkatesh" date="1378821027"]Hi,

    @
    QTransform tr;
    QPixmap::trueMatrix(tr,width,height);
    QFile file("/Users/venkateshpadmanabhan/Desktop/out.txt");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out << (QTransform)tr;

    @
    [/quote]

    The things I see:
    You don't retrieve the result from trueMatrix.
    tr is a default constructed QTransform that you are sending through a QDataStream in a text file. You should rather use QTextStream.

    I would also recommend to change the name of the tr variable since there's also the tr function in QObject)



  • hi,
    Thanks for response. I am not using it actually. I understood

    @
    QPixmap::trueMatrix @ is for updating the transform which has been changed. But what i wanted to do here is save the pixmap or QImage as 2D Matrix into a .txt file. So i altered the code as following, but the application quits after execution

    @

    QPixmap p(scn3->width(),scn3->height());
            QPainter paint(&p);
            paint.setRenderHint(QPainter::Antialiasing);
            paint.setRenderHint(QPainter::SmoothPixmapTransform);
            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&#40;"/Users/venkateshpadmanabhan/Desktop/out.txt"&#41;;
                        file.open(QIODevice::WriteOnly);
                        QTextStream tex(&file);
                        tex<<(qint64)matrix;
                       // out <<mat(&matrix[width][height]) ;
                        file.close();
    

    }

    @


  • Lifetime Qt Champion

    Are you trying to stream a two dimensional int array by casting it as a qint64 ?

    You should rather send the values one after the other in the your double loop (don't forget the end of line)



  • hi,
    I have tried the following inserting the expression inside the for loop. But again the application gets crashed when i execute. I am puzzled where i am getting wrong.

    @

    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);
                           }
    
                       }
    

    @


  • Lifetime Qt Champion

    Line 5: why the * ?

    Anyway: why the matrix variable ? It's useless since you get the pixel value from the image.

    Also why do you reopen the file on each iteration ?

    And why the call to endl(tex) after the file has been closed ?



  • Hi,

    Thanks for your response, i could get a text file which has the color information of the pixels. The code

    @

    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<<s;

                           endl(tex);
    

    //
    }

                       }
    
            file.close();
    

    @

    This works fine, but what i wanted to get is @ TransformationMatrix @ of the pixmap in a scene and convert it as a readable format like int and save it to a .txt file. But i couldn't trace how to convert the transformation matrix into Int or anything which could be accepted by @ QTextStream @


  • Lifetime Qt Champion

    Again, what is the use of your matrix variable ?

    Do you set that transformation somewhere ?



  • Hi,

    Sorry i forgot to comment the line for the matrix, i have done that. But the problem comes when i access the transformation which i have done in

    @QGraphicsPixmapItem@

    In this case i try to access it by

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

    But the problem is i am not sure how to set the @QTransform@ into the Text Stream for storing the transformation as @Matrix@ in ".txt" file, as i did for the color information of a pixmap


  • Lifetime Qt Champion

    You have to do it by hand. Meaning create your text representation of each element of the QTransform (you can get inspiration from the qDebug() stream or QDataStream operator of QTransform)



  • Hi,
    Can you suggest me some example?... I tried to find some reference idea how to implement it. But i couldn't find it any where :( I just tried

    @
    QDataStream<<QTransform(trans);
    @

    Is there anyway to convert the @QTransform@ into int? or any other data format to push it into QTextStream.


  • Lifetime Qt Champion

    Not int, qreal or you'll loose information.

    You can retrieve all the transform parameters using the mXX functions



  • Hi,

    I tried to convert the QTransform into QVariant and again into qreal and pass it into QTextStream but some how it doesn't seam to work. I guess my casting is wrong ....
    @

    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);
    @

    Is this conversion method wrong?


  • Lifetime Qt Champion

    Yes it is, you are expecting a matrix to be somehow transformed in one qreal value.

    As I wrote before, take each parameter of the QTransform and write it to your text file.



  • Hi,

    I have done as per your suggestion. But i am not sure whether it is correct. Is there any method to verify its reliability??

    @

    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();

    @


  • Lifetime Qt Champion

    Why are you converting everything back and forth ?

    just do:

    @data << mat.m11()
    << mat.m12 etc...@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.