如何将IplImage转换为标准Web图像格式以在url中显示?

时间:2012-10-01 22:18:02

标签: c opencv

所以我理解套接字编程的基础知识然而当我去localhost时我得到的是:8888是不可读的字符串而不是视频或图像帧。我想知道是否有人有关于如何在我的端口而不是字符串上显示图像的指针。我的代码如下:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* streamServer(void* arg);
void  quit(char* msg, int retval);

int main(int argc, char** argv)
{
    pthread_t   thread_s;
    int         key;

    if (argc == 2) {
        capture = cvCaptureFromFile(argv[1]);
    } else {
        capture = cvCaptureFromCAM(0);
    }

    if (!capture) {
        quit("cvCapture failed", 1);
    }

    img0 = cvQueryFrame(capture);
    img1 = cvCreateImage(cvGetSize(img0), IPL_DEPTH_8U, 1);

    cvZero(img1);
    cvNamedWindow("stream_server", CV_WINDOW_AUTOSIZE);

    /* print the width and height of the frame, needed by the client */
    fprintf(stdout, "width:  %d\nheight: %d\n\n", img0->width, img0->height);
    fprintf(stdout, "Press 'q' to quit.\n\n");

    /* run the streaming server as a separate thread */
    if (pthread_create(&thread_s, NULL, streamServer, NULL)) {
        quit("pthread_create failed.", 1);
    }

    while(key != 'q') {
        /* get a frame from camera */
        img0 = cvQueryFrame(capture);
        if (!img0) break;

        img0->origin = 0;
        cvFlip(img0, img0, 1); //-1);

        /**
         * convert to grayscale
         * note that the grayscaled image is the image to be sent to the client
         * so we enclose it with pthread_mutex_lock to make it thread safe
         */
        pthread_mutex_lock(&mutex);
        cvCvtColor(img0, img1, CV_BGR2GRAY);
        is_data_ready = 1;
        pthread_mutex_unlock(&mutex);

        /* also display the video here on server */
        cvShowImage("stream_server", img0);
        key = cvWaitKey(30);
    }

    /* user has pressed 'q', terminate the streaming server */
    if (pthread_cancel(thread_s)) {
        quit("pthread_cancel failed.", 1);
    }

    /* free memory */
    cvDestroyWindow("stream_server");
    quit(NULL, 0);
}

/**
 * This is the streaming server, run as a separate thread
 * This function waits for a client to connect, and send the grayscaled images
 */
void* streamServer(void* arg)
{
    struct sockaddr_in server;

    /* make this thread cancellable using pthread_cancel() */
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    /* open socket */
    if ((serversock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        quit("socket() failed", 1);
    }

    /* setup server's IP and port */
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.s_addr = INADDR_ANY;

    /* bind the socket */
    if (bind(serversock, (const void*)&server, sizeof(server)) == -1) {
        quit("bind() failed", 1);
    }

    /* wait for connection */
    if (listen(serversock, 10) == -1) {
        quit("listen() failed.", 1);
    }

    /* accept a client */
    if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
        quit("accept() failed", 1);
    }

    /* the size of the data to be sent */
    int imgsize = img1->imageSize;
    int bytes, i;

    /* start sending images */
    while(1)
    {
        /* send the grayscaled frame, thread safe */
        pthread_mutex_lock(&mutex);
        if (is_data_ready) {
            bytes = send(clientsock, img1->imageData, imgsize, 0);
            is_data_ready = 0;
        }
        pthread_mutex_unlock(&mutex);

        /* if something went wrong, restart the connection */
        if (bytes != imgsize) {
            fprintf(stderr, "Connection closed.\n");
            close(clientsock);

            if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
                quit("accept() failed", 1);
            }
        }

整个代码: http://pastebin.com/jnuZhx8C

2 个答案:

答案 0 :(得分:1)

除非img1-> imageData采用浏览器应该识别的格式(png,jpg,gif),否则您无法识别无法识别的文本。

答案 1 :(得分:1)

在我们进入技术细节之前,请问自己一个问题:什么是standard web image format

在您看来,它会是PNG吗? JPG? TIFF? BMP? GIF?所有这些?

像这样的图像格式存储的不仅仅是像素。如果您查看specification of PNG,您会发现除了像素之外,它还会存储大量关于图像的information

问题在于,当您通过网络发送像素时,就像您现在正在做的那样,无论是谁在接收的另一端接收数据都无法做任何有用的事情,因为它甚至不知道什么是图像的维度。

我们需要澄清的另一个误解是:没有display images on my port rather than strings 之类的东西。 What is a port?

跨网络传输数据是一回事。在连接的另一端对数据做一些有用的事情(比如在屏幕上显示)是完全不同的任务。

如果你需要一个实际的例子关于如何完成这些任务,我建议你看一下这个很棒的教程:Streaming OpenCV Videos Over the Network。我刚刚注意到您的问题中的代码来自那里,因此会特别注意 4. Implementation of the Client-side部分。