Ask Your Question

how sent a live stream(UDP) of frames captured by opencv

asked 2013-10-06 09:04:29 -0500

naeeem gravatar image

updated 2013-10-08 11:05:13 -0500

i created these programs as client and server for broadcasting(UDP) of frames. i got error as segmentation error while running client. code for client

#include <iostream>
#include <stdio.h>
//opencv libraries
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
//socket libraries
#define SERVERPORT "4950"   // the port users will be connecting to

using namespace std;
using namespace cv;

void detectAndDisplay( Mat frame , Mat frame_edit);// function for detecting face
int talker(Mat frame);
/** Global variables */
String face_cascade_name = "lbpcascade_frontalface.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;

/** @function main */
int main( void ) 
//body for image capture from camera 
    //calling face detect function
detectAndDisplay( frame_edit, frame );
    int c = waitKey(5);
    if( (char)c == 27 ) { break; } // escape
return 0;

 * @function detectAndDisplay
 void detectAndDisplay( Mat frame ,Mat frame_edit)
// function body for detecting face

imshow( "face detection client", frame );

    //calling talker to sent the data..
int ret=talker(frame);


int talker(Mat frame)

int sockfd;
const char *argv[1]={"localhost"};
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
frame = (frame.reshape(0,1)); // to make it continuous
int  imgSize =*frame.elemSize();  
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;

if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) 
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
    // loop through all the results and make a socket

for(p = servinfo; p != NULL; p = p->ai_next) 
    if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) 
        perror("talker: socket");
if (p == NULL)
    fprintf(stderr, "talker: failed to bind socket\n");
    return 2;
if ((numbytes = sendto(sockfd,, imgSize, 0,p->ai_addr, p->ai_addrlen)) == -1)
    perror("talker: sendto");
cout<<"senting frame succesfull"<<endl;
return 0;
edit retag flag offensive close merge delete


hi, can you please cut down the code to the essential? (the whole face-detect cruft is not nessecary here)

also, you don't want to loop though all possible protocols, and take the 1st one that works,but specify a distinct one, like socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

berak gravatar imageberak ( 2013-10-08 10:37:25 -0500 )edit

besides, sending raw images over udp might be a bad idea (packet fracturing, loss)

remember, that you can only stuff 64k into one packet!

tcp would at least fracture it automatically, and write several packets.

berak gravatar imageberak ( 2013-10-08 10:42:18 -0500 )edit

so what do you suggest...i got programs that sent frames captured using v4l2...besides that program can be easily obtained using vlc on server...i want a similar program but with opencv...will it be efficient with tcp ? i heard it causes more delay in there a way to sent frames in more efficient and faster in UDP??

naeeem gravatar imagenaeeem ( 2013-10-08 11:08:54 -0500 )edit

if you must do your own networking, at least start with tcp, get that to run, later improve. handling udp correctly is much more difficult than tcp, since you seem to be an absolute noob to network progging, avoid it at all costs!

oh, and yes, outsourcing all of it to gstreamer/libvlc or such is the better idea!

berak gravatar imageberak ( 2013-10-08 11:25:33 -0500 )edit

well you are correct about me because to your information i started working on networking since 1 month..and i am completely exhuasted since i dont have anyone for guiding me..other than opecv and stack overflow forums..whatever... can we link gstreamer and opencv??

naeeem gravatar imagenaeeem ( 2013-10-08 11:38:23 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-10-07 01:43:37 -0500

Haris gravatar image

updated 2013-10-07 01:46:06 -0500

Get and directly send to the socket, the data format is like BGR BGR BGR.... On the receiving side you should know the size of image you are going to receive. After receiving just assign the received buffer(BGR BGR... array) to a Mat of size you already know.


Mat frame;
frame = (frame.reshape(0,1)); // to make it continuous

int  imgSize =*frame.elemSize();

// Send data here
bytes = send(clientSock,, imgSize, 0))


   Mat  img = Mat::zeros( height,width, CV_8UC3);
   int  imgSize =*img.elemSize();
   uchar sockData[imgSize];

 //Receive data here

   for (int i = 0; i < imgSize; i += bytes) {
   if ((bytes = recv(connectSock, sockData +i, imgSize  - i, 0)) == -1) {
     quit("recv failed", 1);

 // Assign pixel value to img

 int ptr=0;
 for (int i = 0;  i < img.rows; i++) {
  for (int j = 0; j < img.cols; j++) {                               <cv::Vec3b>(i,j) = cv::Vec3b(sockData[ptr+ 0],sockData[ptr+1],sockData[ptr+2]);
edit flag offensive delete link more


i have edited program with your recommendations still there is more thing personel i am also a malayalee from guruvayur...:)

naeeem gravatar imagenaeeem ( 2013-10-08 10:17:22 -0500 )edit

Hi you can try this link which send and receives opencv image over network using tcp. And I am from kannur....

Haris gravatar imageHaris ( 2013-10-09 00:11:36 -0500 )edit

that solved a major issue for me...can i get ur personal mail id?

naeeem gravatar imagenaeeem ( 2013-10-16 10:21:47 -0500 )edit

No encoding at all?

Francesco Boi gravatar imageFrancesco Boi ( 2019-02-02 12:18:59 -0500 )edit

Question Tools



Asked: 2013-10-06 09:04:29 -0500

Seen: 24,348 times

Last updated: Oct 08 '13