现在的位置: 首页 > 综合 > 正文

一些准备

2013年09月13日 ⁄ 综合 ⁄ 共 5416字 ⁄ 字号 评论关闭

最近要作一些事,平时看到的一些东西也许有用,先列在下面

SImple class for Axis Camera and OpenCV

I found not so simple to take a stream form an AXIS ip-camera using OpenCV 2.0.
So I looked around on the internet, I spent a lot of time trying different solutions and, at the end,  I found a code that works in  http://www.eecs.ucf.edu/~rpatrick/code/onelinksys.c .
The code works fine, but all the examples in the books or internet use cvQueryFrame, so a wrote a little class that give something similar. Here is the code:  (hope that it will help someone else):
The main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <highgui.h>
#include "../includes/AXISCam.h"  //or where you put the header...
IplImage*    g_image    = NULL;
int main(int argc, char *argv[])
{
    AXISCam *myCam;
    if (argc > 1)
    {
        myCam=new AXISCam(argv[1]);  //create an axis cam obj
    }
    else
        return 1;
   //While a key is not pressed
    int i = 0;
    while (cvWaitKey(2) < 0)
    {
        g_image = myCam->getFrame();   // get the frame
        cvShowImage( "Camera", g_image); 
        cvReleaseImage(&g_image);
    }
    return 0;
}
The AXISCam.h

/*

  This code captures video from a Linksys/Axis IP Camera's MJPEG stream using OpenCV, popen, and the program "curl"
  The libjpeg code was taken and adapted from code written by George V. Landon
  http://georgelandon.blogspot.com/2008/08/remote-capture-canon-xti.html
  
  The AXIS Cam code was taken from rpatrik 
  http://www.eecs.ucf.edu/~rpatrick/code/onelinksys.c 
  
*/  
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <highgui.h>
#include <jpeglib.h>
#include <jerror.h>
// AXIS Routine
//Start of libjpeg methods
typedef struct my_src_mgr my_src_mgr;
struct my_src_mgr
{
    struct jpeg_source_mgr pub;
    JOCTET eoi_buffer[2];
};
class AXISCam {
private:
    //No more than 66 chars worth of unused data is read at a time
    char garbage[80];
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    JSAMPROW row_pointer[1];
    char *raw_image;
    int location, i;
    char framesize[10];
    FILE *stream;
    FILE *startCurl(char *url) {
        char command[256]="";
        strcat(command,"curl -Ns ");
        strcat(command,url);
        strcat(command,"axis-cgi/mjpg/video.cgi?resolution=640x480&fps=5&.mjpg");
        FILE *stream = popen(command,"r");
        return stream;
    }
    static void init_source(j_decompress_ptr cinfo)
    {
    }
    static int fill_input_buffer(j_decompress_ptr cinfo)
    {
        return 1;
    }
    static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
    {
        my_src_mgr *src = (my_src_mgr *)cinfo->src;
        if (num_bytes > 0)
        {
            while (num_bytes > (long)src->pub.bytes_in_buffer)
            {
                num_bytes -= (long)src->pub.bytes_in_buffer;
                fill_input_buffer(cinfo);
            }
        }
        src->pub.next_input_byte += num_bytes;
        src->pub.bytes_in_buffer -= num_bytes;
    }
    static void term_source(j_decompress_ptr cinfo)
    {
    }
    void jpeg_memory_src(j_decompress_ptr cinfo, unsigned char const *buffer, size_t bufsize)
    {
        my_src_mgr *src;
        if (! cinfo->src)
        {
            cinfo->src = (struct jpeg_source_mgr*) (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(my_src_mgr));
        }
        src = (my_src_mgr *)cinfo->src;
        src->pub.init_source = init_source;
        src->pub.fill_input_buffer = fill_input_buffer;
        src->pub.skip_input_data = skip_input_data;
        src->pub.resync_to_restart = jpeg_resync_to_restart;
        src->pub.term_source = term_source;
        src->pub.next_input_byte = buffer;
        src->pub.bytes_in_buffer = bufsize;
    }
//End of libjpeg methods
public:
    AXISCam(char* url) {
        stream=startCurl(url);
        raw_image== NULL;
        cinfo.err = jpeg_std_error(&jerr);
        jpeg_create_decompress(&cinfo);
        //Reset and repeat
//    free(row_pointer[0]);
        row_pointer[0] = NULL;
        //The body of the multipart/x-mixed-replace HTTP response is parsed into pieces
        //Each frame's size as text
        CvSize size = cvSize(640,480);
        // Open capture device. 0 is /dev/video0, 1 is /dev/video1, etc.
    };
    ~AXISCam() {
        jpeg_destroy_decompress(&cinfo);
        pclose(stream);
    };
/* get frame from axis */
    IplImage* getFrame() {
        //Begin Parsing This Frame   //Begin Parsing This Frame
        //--ThisRandomString\r\n
        fgets(garbage,sizeof(garbage),stream);
        //"Content-type:...\n"
        fgets(garbage,sizeof(garbage),stream);
        //"Content-Length: "
        fread((void *)garbage,sizeof(char),16,stream);
        //Read the frame size
        fgets(framesize,sizeof(framesize),stream);
        //\n before frame
        fgets(garbage,sizeof(garbage),stream);
        //Create space to store the frame
        uchar frame[atoi(framesize)];
        //Read the frame
        fread((void *)frame,sizeof(char),atoi(framesize),stream);
        /*
                if (!frame)
                {
                    break;
                }
        */
        //\n before the next boundary
        fgets(garbage,sizeof(garbage),stream);
        //Finished Parsing This Frame
        //JPEG to IplImage
        IplImage *fRGB, *fBGR;
        location = 0;
        i = 0;
        jpeg_memory_src(&cinfo,frame,sizeof(frame));
        jpeg_read_header(&cinfo,TRUE);
        jpeg_start_decompress(&cinfo);
        //Create an empty IplImage to put data into
        fBGR = cvCreateImageHeader(cvSize(cinfo.output_width,cinfo.output_height), IPL_DEPTH_8U, cinfo.num_components);
        cvCreateData(fBGR);
        fRGB = cvCreateImageHeader(cvSize(cinfo.output_width,cinfo.output_height), IPL_DEPTH_8U, cinfo.num_components);
        cvCreateData(fRGB);
        raw_image = fBGR->imageData;
        row_pointer[0] = (unsigned char *)malloc(cinfo.output_width*cinfo.num_components);
        while (cinfo.output_scanline < cinfo.image_height)
        {
            jpeg_read_scanlines(&cinfo,row_pointer,1);
            for (i = 0; i < cinfo.image_width*cinfo.num_components; i++)
            {
                raw_image[location++] = row_pointer[0][i];
            }
        }
        jpeg_finish_decompress(&cinfo);
        //Convert the frame from BGR to RGB
        cvCvtColor(fBGR,fRGB,CV_BGR2RGB);
        //--Th /*
        // cvReleaseImage(&fRGB); deve tornare non la distruggo...
        cvReleaseImage(&fBGR);
        return(fRGB);
    };
};

占位

抱歉!评论已关闭.