简体   繁体   中英

OpenCV very slow - webcam

I'm using OpenCV to send camera bytes via UDP to another computer.

The problem is, that the framerate of the camera is only 15fps. If I send a picture, it works with over 200fps.

My camera supports 30fps, so does anyone know why this happen?

Here's my code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <fstream>
#include <arpa/inet.h>

#include <unistd.h>

using namespace std;
using namespace cv;

void createSocket(int port, const char* ip);
void sendData(vector<uchar> buff);
int _socket;

struct sockaddr_in serverAdress,clientAdress;


// argv[1] = Bild
int main(int argc, char** argv)
{

    if(argc != 5)
    {
        cout << "Error\n";
        return -1;
    }

    createSocket(atoi(argv[2]),argv[1]);

    vector<uchar> buff;
    vector<int> param = vector<int>(2);


    param[0] = CV_IMWRITE_JPEG_QUALITY;
    param[1] = atoi(argv[3]);


    // VideoCapture
    VideoCapture cap(1);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);


    if(!cap.isOpened())
    {
        cout << "Error\n";
        return -1;
    }


    Mat frame;

    while(true){

        cap >> frame;

        cvtColor(frame, frame, CV_8U);

        imencode(".jpeg",frame, buff,param);
        cout<<"coded file size(jpg)"<<buff.size()<<endl;
        sendData(buff);
    }


    return 0;
}


void createSocket(int port, const char* ip){
    _socket = socket(AF_INET,SOCK_DGRAM,0);

    serverAdress.sin_family = AF_INET;
    serverAdress.sin_addr.s_addr= inet_addr(ip);
    serverAdress.sin_port=htons(port);

    if(_socket == -1){
        cout << "Error";
        return;
    }

}


void sendData(vector<uchar> buff){

    char data[buff.size()];
    for(int i = 0; i < buff.size(); i++){
        data[i] = buff.at(i);
    }

    sendto(_socket,data,buff.size(),0,(struct sockaddr *)&serverAdress,sizeof(serverAdress));
}

While 30 FPS might be a theoretical maximum for your camera, it might be reachable on a sunny day, outside. I have a sneaking suspicion that the sub-par acquisition frequency might be caused by insufficient lighting. It just needs more time to catch enough photons to produce image of desired quality. I'm not sure what your acquisition conditions are, but things might improve significantly with more intense light. If you have access to the internal settings of the camera, you could also try reducing the exposure time. It would however require an icrease of gain to keep proper contrast and the images will exhibit more noise.

I believe UDP as a maximum theorotical data size per packet of approximately 65KB, but most protocols limit this to a few hundred bytes for safer transmission.Seeing as the video is being recorded at a 640 by 360 resolution without any compression, i can guarantee you that each frame significantly exceeds 65KB. So it is very unrealistic to expect the frame rate of your transmission in a constrained pipe to be equal to the frame rate at which the camera records at. If you want a faster frame rate at the receiver, either reduce the resolution of your video, or used an appropriate library like gstreamer which offers h.264 compression of the video before transmission.

Another solution would be to perform some processing on each frame via opencv (such as as simple filter), this should help reduce the size of each frame

Also what do you mean by if you send a picture, it works with over 200fps? This statement does not appear to make any sense

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM