Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert IplImage to QByteArray
QtWS25 Last Chance

Convert IplImage to QByteArray

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 6.4k Views
  • 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 Offline
    S Offline
    soroush
    wrote on last edited by
    #1

    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
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        soroush
        wrote on last edited by
        #3

        [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
        0
        • S Offline
          S Offline
          soroush
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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
            0
            • S Offline
              S Offline
              soroush
              wrote on last edited by
              #6

              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
              0
              • R Offline
                R Offline
                randyclark
                Banned
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved