Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Convert IplImage to QByteArray

    General and Desktop
    4
    7
    6071
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      soroush last edited by

      Hello

      I need to send IplImages fetched from camera with UDP socket. I'm using Qt. The application has two parts. The server should fetch images from digital camera and send them to a specific port via upd (broadcasting). Clients are gui applications. they will receive images and display them on a QLabel.

      I'm using openCV for images, so my input of camera is an IplImage. I know how to convert it to QImage/QPixmap and display it on a label. But can't find a way to send IplImage through a udp socket... QUdpSocet::writeDatagram only accepts QByteArray so I neet to convert IplImage to QByteArray and this should be very fast (It's not appropriate to convert images on server).

      1 Reply Last reply Reply Quote 0
      • Q
        qxoz last edited by

        Try this
        @QByteArray bArray;
        QBuffer buff(&bArray);
        QImage img_Tasvir;
        img_Tasvir.save(&buff,"PNG");@

        1 Reply Last reply Reply Quote 0
        • S
          soroush last edited by

          [quote author="qxoz" date="1322728572"]Try this
          @QByteArray bArray;
          QBuffer buff(&bArray);
          QImage img_Tasvir;
          img_Tasvir.save(&buff,"PNG");@[/quote]

          I don't want to convert IpliImage to QImage on the server side. It's too time-consuming. It should process at least 20 frames per second on a 1GHz Vortex86XD...

          1 Reply Last reply Reply Quote 0
          • S
            soroush last edited by

            Ok I tried this:
            @
            int main(int argc, char *argv[])
            {
            QCoreApplication a(argc, argv);
            QUdpSocket socket;
            quint16 port = atoi(argv[1]);
            cout << "Connecting to UDP socket..." << endl;
            cout << "Detecting Digital Camera..." << endl;
            CvCapture * camera = cvCreateCameraCapture(0);
            assert(camera);
            cout << "Image transmition started..." << endl;
            cout << "Press Ctrl+C to end program" << endl;
            cout << port << endl;
            while(true)
            {
            IplImage * image=cvQueryFrame(camera);
            assert(image);
            cvResize(image,image,2);
            QByteArray array(image->imageData);
            cout<<"size: " << array.size() << endl;
            if(socket.writeDatagram(array,QHostAddress::Broadcast,port)==-1)
            {
            cout<<socket.errorString().toAscii().data() << endl;
            exit(0);
            }
            }
            return a.exec();
            }
            @
            There output is:
            @
            size: 348789
            Datagram was too large to send
            @

            1 Reply Last reply Reply Quote 0
            • G
              goetz last edited by

              Two errors here:

              First:

              @
              QByteArray(image->imageData);
              @

              is likely to truncate your data, as it stops at the first null byte.

              change it to

              @
              QByteArray(image->imageData, image->imageSize);
              @

              Second:
              An UDP datagram can only contain 8192 bytes, but you discovered that already in that "other thread":/forums/viewthread/12127/.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply Reply Quote 0
              • S
                soroush last edited by

                bq. 1
                QByteArray(image->imageData);
                is likely to truncate your data, as it stops at the first null byte.
                change it to

                Yes, i noticed about that. forgot to correct above code :)

                bq. An UDP datagram can only contain 8192 bytes, but you discovered that already in that other thread.

                Although with TCP I have no chance to send 30 fps video by individual images on LAN Ethernet cable... I found that sending Images is not a good idea. It should be an AVI stream with compression.

                Thanks

                1 Reply Last reply Reply Quote 0
                • R
                  randyclark Banned last edited by

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post