Convert IplImage to QByteArray



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



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



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



  • 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
    @



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



  • 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


  • Banned

    This post is deleted!

Log in to reply
 

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