最近在研究mjpg_streamer,发现这是个好东西!关于mjpg_streamer就不做具体介绍了,总之它是在Linux上运行的视频服务器,可以将摄像头采集到的视频数据通过网络传输到客户端,实现视频监控,mjpg_streamer是开源项目。
首先简要的分析一下mjpg_streamer的源码及其工作过程。可以参考这里:http://blog.csdn.net/zhengqijun_/article/details/72473177。mjpg_streamer主要由三部分构成,主函数mjpg_streamer.c 和输入、输出组件,其中输入、输出组件通常是input_uvc.so和output_http.so,它们是以动态链接库的形式在主函数中调用的。
主函数的主要功能有:1. 解析命令行的各个输入参数。2. 判断程序是否需要成为守护进程,如果需要,则执行相应的操作。3. 初始化global全局变量。4. 打开并配置输入、输出插件。5. 运行输入、输出插件。
输入插件将采集到的视频送到全局缓存中,输出插件则从全局缓存中读取数据并发送。输出插件的实现是一个http服务器,这里就不介绍了。输入插件的视频采集功能主要是通过Linux的V4L2接口实现的,主要是4个函数input_init()、 input_stop()、 input_run()和 input_cmd()。其中iniput_run()函数创建了线程cma_thread(),这个线程很重要,该函数的作用是抓取一帧的图像,并复制到全局缓冲区。具体的代码说明请参考链接中的分析。
mjpg_streamer的功能的确强大,但是由于我的主要研究方向是图像处理,因此也想到了一些问题。现在的网络视频监控系统除了传统的视频传输功能外,大多还有视频分析的能力,即在图像捕获和图像传输之间加上了图像处理的能力,例如能够检测移动目标,这样视频监控服务器的功能就大大增强了。mjpg_streamer是一个很好的视频服务器框架,那么它否能够通过修改而拥有视频图像处理能力呢?为此我也查阅了很多资料,同时发现了另一个开源项目motion。关于motion的详细信息可以参考这里:http://www.lavrsen.dk/foswiki/bin/view/Motion。motion的移植可以参考这里:http://blog.csdn.net/kangear/article/details/8763790。
motion实际上功能类似于mjpg_streamer,但是正如它的名字,除了能够捕获和传输视频外,它还拥有运动检测的功能,能够将检测到的运动物体标识出来,并且检测到移动物体后可以执行用户脚本,实现报警等功能,非常强大。但是motion也有他的局限性,它只能单纯的检测移动物体,如果想要做更复杂的或者有针对性的算法就没有办法了,比如车辆检测、火灾监测、人脸识别等等。鉴于mjpg_streamer的源代码比较易读,思路清晰,我准备修改它的源码来支持图像处理功能。通过前面mjpg_streamer的源码分析可知要实现图像处理功能,应该从输入组件input_uvc.so下手。
在input_uvc.c文件中有一个线程是cam_thread()前面已经提到过了,其作用是抓取一帧的图像,并复制到全局缓冲区。如果在抓取一帧图像后先进行处理,在复制到全局缓冲区就能实现目标。下面是cam_thread()的代码:
void *cam_thread( void *arg ) {
/* set cleanup handler to cleanup allocated ressources */
pthread_cleanup_push(cam_cleanup, NULL);
while( !pglobal->stop ) {
/* grab a frame */
if( uvcGrab(videoIn) < 0 ) {
IPRINT("Error grabbing frames\n");
exit(EXIT_FAILURE);
}
DBG("received frame of size: %d\n", videoIn->buf.bytesused);
/*
* Workaround for broken, corrupted frames:
* Under low light conditions corrupted frames may get captured.
* The good thing is such frames are quite small compared to the regular pictures.
* For example a VGA (640x480) webcam picture is normally >= 8kByte large,
* corrupted frames are smaller.
*/
if ( videoIn->buf.bytesused < minimum_size ) {
DBG("dropping too small frame, assuming it as broken\n");
continue;
}
/* copy JPG picture to global buffer */
pthread_mutex_lock( &pglobal->db );
/*
* If capturing in YUV mode convert to JPEG now.
* This compression requires many CPU cycles, so try to avoid YUV format.
* Getting JPEGs straight from the webcam, is one of the major advantages of
* Linux-UVC compatible devices.
*/
if (videoIn->formatIn == V4L2_PIX_FMT_YUYV) {
DBG("compressing frame\n");
pglobal->size = compress_yuyv_to_jpeg(videoIn, pglobal->buf, videoIn->framesizeIn, gquality);
}
else {
DBG("copying frame\n");
pglobal->size = memcpy_picture(pglobal->buf, videoIn->tmpbuffer, videoIn->buf.bytesused);
}
#if 0
/* motion detection can be done just by comparing the picture size, but it is not very accurate!! */
if ( (prev_size - global->size)*(prev_size - global->size) > 4*1024*1024 ) {
DBG("motion detected (delta: %d kB)\n", (prev_size - global->size) / 1024);
}
prev_size = global->size;
#endif
/* signal fresh_frame */
pthread_cond_broadcast(&pglobal->db_update);
pthread_mutex_unlock( &pglobal->db );
DBG("waiting for next frame\n");
/* only use usleep if the fps is below 5, otherwise the overhead is too long */
if ( videoIn->fps < 5 ) {
usleep(1000*1000/videoIn->fps);
}
}
DBG("leaving input thread, calling cleanup function now\n");
pthread_cleanup_pop(1);
return NULL;
}
22行—37行的代码实现了将图像数据拷贝到全局缓冲区,if语句针对的是输出YUYV格式的摄像头,在拷贝数据之前还要进行数据转换,将YUYV格式利用libjpeg库转换成jpg格式再进行拷贝。关于libjpeg库的移植方法可以参考这里:http://blog.chinaunix.net/uid-11765716-id-172491.html。else语句针对输出格式为jpg的摄像头,它直接将数据拷贝到全局缓冲区,无需转换。
由于我使用的摄像头是中星微zc301,直接输出jpg格式,因此这里只讨论这种情况的修改方法。从上面代码中我们可以看出,我们只需要在21行与22行之间增加处理算法就能实现相应的功能。
要进行图像处理,首先要将采集到的jpg图像转换为RGB或YUV,然后再由RGB或YUV转换为灰度,对灰度图像进行处理,完成之后再转换为jpg格式,最后拷贝到全局缓冲。而jpg与RGB或YUV格式的转换还要靠libjpeg库来实现。但是libjpeg库有个缺陷:它只能对文件进行转换,而我们的数据在内存中,因此需要对libjpeg库进行一定的修改。幸运的是我在motion项目中发现了jpg与YUV转换的代码,经过简单修改后可以直接使用。相应的文件是jpegutils.h和jpegutils.c,将他们放入mjpg-streamer/plugins/input_uvc/中即可。这是经过修改的文件:jpegutils。要用到的两个函数如下,分别是将jpg数据解码为YUV和将YUV编码成jpg。
int decode_jpeg_raw(unsigned char *jpeg_data, int len,
int itype, int ctype, unsigned int width,
unsigned int height, unsigned char *raw0,
unsigned char *raw1, unsigned char *raw2);
int encode_jpeg_raw(unsigned char *jpeg_data, int len, int quality,
int itype, int ctype, unsigned int width,
unsigned int height, unsigned char *raw0,
unsigned char *raw1, unsigned char *raw2);
将jpg数据解码为YUV数据后,由于Y代表亮度,即灰度。我们只需要对Y分量做处理即可,最后再编码为jpg数据。下面附上修改后的input_uvc.c文件:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "../../utils.h"
#include "../../mjpg_streamer.h"
#include "v4l2uvc.h"
#include "huffman.h"
#include "jpeg_utils.h"
#include "jpegutils.h"
#include "dynctrl.h"
#define INPUT_PLUGIN_NAME "UVC webcam grabber"
#define MAX_ARGUMENTS 32
/*
* UVC resolutions mentioned at: (at least for some webcams)
* http://www.quickcamteam.net/hcl/frame-format-matrix/
*/
static const struct {
const char *string;
const int width, height;
} resolutions[] = {
{ "QSIF", 160, 120 },
{ "QCIF", 176, 144 },
{ "CGA", 320, 200 },
{ "QVGA", 320, 240 },
{ "CIF", 352, 288 },
{ "VGA", 640, 480 },
{ "SVGA", 800, 600 },
{ "XGA", 1024, 768 },
{ "SXGA", 1280, 1024 }
};
/* private functions and variables to this plugin */
pthread_t cam;
pthread_mutex_t controls_mutex;
struct vdIn *videoIn;
static globals *pglobal;
static int gquality = 80;
static unsigned int minimum_size = 0;
static int dynctrls = 1;
//jpeg压缩、解压缩
unsigned char *y,*u,*v,*processed_jpeg_data;
void *cam_thread( void *);
void cam_cleanup(void *);
void help(void);
int input_cmd(in_cmd_type, int);
int input_init(input_parameter *param) {
/////////////////此处省略n行/////////////
void *cam_thread( void *arg ) {
/************* 利用libjpeg库实现jpeg图像段压缩、解压缩 *************/
int i,j;
int length=sizeof(unsigned char)*videoIn->width*videoIn->height;
y = (unsigned char *)malloc(length);
u = (unsigned char *)malloc(length/4);
v = (unsigned char *)malloc(length/4);
processed_jpeg_data = (unsigned char *)malloc(length);
memset(y,0,length);
memset(u,0,length/4);
memset(v,0,length/4);
memset(processed_jpeg_data,0,length);
/* set cleanup handler to cleanup allocated ressources */
pthread_cleanup_push(cam_cleanup, NULL);
while( !pglobal->stop ) {
/* grab a frame */
if( uvcGrab(videoIn) < 0 ) {
IPRINT("Error grabbing frames\n");
exit(EXIT_FAILURE);
}
DBG("received frame of size: %d\n", videoIn->buf.bytesused);
/*
* Workaround for broken, corrupted frames:
* Under low light conditions corrupted frames may get captured.
* The good thing is such frames are quite small compared to the regular pictures.
* For example a VGA (640x480) webcam picture is normally >= 8kByte large,
* corrupted frames are smaller.
*/
if ( videoIn->buf.bytesused < minimum_size ) {
DBG("dropping too small frame, assuming it as broken\n");
continue;
}
//将输入的jpeg图像解压成YUV分量
decode_jpeg_raw(videoIn->tmpbuffer,videoIn->buf.bytesused,0,420,videoIn->width,videoIn->height,y,u,v);
//这里可以对Y分量进行更改加入图像处理算法
/* 二值化 */
/*for(i=0;iheight;i++)
for(j=0;jwidth;j++) {
if (*(y+i*videoIn->width+j)>128)
*(y+i*videoIn->width+j)=250;
else
*(y+i*videoIn->width+j)=0;}*/
/* 负片 */
for(i=0;iheight;i++)
for(j=0;jwidth;j++)
*(y+i*videoIn->width+j)=255-*(y+i*videoIn->width+j);
/* sobel滤波 */
/*
int m,n,edge;//m,n为x,y方向上的梯度
for(i=1;iheight-1;i++)
for(j=1;jwidth;j++)
{
m=*(y+(i-1)*videoIn->width+(j+1))+*(y+i*videoIn->width+(j+1))*2+*(y+(i+1)*videoIn->width+(j+1))\
-*(y+(i-1)*videoIn->width+(j-1))-*(y+i*videoIn->width+(j-1))*2-*(y+(i+1)*videoIn->width+(j-1));
n=*(y+(i-1)*videoIn->width+(j-1))+*(y+(i-1)*videoIn->width+j)*2+*(y+(i-1)*videoIn->width+(j+1))\
-*(y+(i+1)*videoIn->width+(j-1))-*(y+(i+1)*videoIn->width+j)*2-*(y+(i+1)*videoIn->width+(j+1));
edge=(int)sqrt((float)m*m+(float)n*n)+0.5;
//if(edge>255)
//edge=255;
*(y+i*videoIn->width+j)=edge> 450?250 : 20;
}
*/
//将UV分量设置为128,压缩后为灰度图像 memset(u,128,length/4); memset(v,128,length/4);
//将YUV分量压缩成jpeg encode_jpeg_raw(processed_jpeg_data,length,80,0,420,videoIn->width,videoIn->height,y,u,v);
//free()在后面不要忘了!
/* copy JPG picture to global buffer */
pthread_mutex_lock( &pglobal->db );
/*
* If capturing in YUV mode convert to JPEG now.
* This compression requires many CPU cycles, so try to avoid YUV format.
* Getting JPEGs straight from the webcam, is one of the major advantages of
* Linux-UVC compatible devices.
*/
if (videoIn->formatIn == V4L2_PIX_FMT_YUYV)
{
DBG("compressing frame\n");
pglobal->size = compress_yuyv_to_jpeg(videoIn, pglobal->buf, videoIn->framesizeIn, gquality);
}
else
{
DBG("copying frame\n");
//pglobal->size = memcpy_picture(pglobal->buf, videoIn->tmpbuffer, videoIn->buf.bytesused);
//将新压缩的jpeg图像复制到全局缓冲区
pglobal->size = memcpy_picture(pglobal->buf,processed_jpeg_data, videoIn->width*videoIn->height);
}
#if 0
/* motion detection can be done just by comparing the picture size, but it is not very accurate!! */
if ( (prev_size - global->size)*(prev_size - global->size) > 4*1024*1024 )
{
DBG("motion detected (delta: %d kB)\n", (prev_size - global->size) / 1024);
}
prev_size = global->size;
#endif
/* signal fresh_frame */
pthread_cond_broadcast(&pglobal->db_update);
pthread_mutex_unlock( &pglobal->db );
DBG("waiting for next frame\n");
/* only use usleep if the fps is below 5, otherwise the overhead is too long */
if ( videoIn->fps < 5 )
{
usleep(1000*1000/videoIn->fps);
}
}
DBG("leaving input thread, calling cleanup function now\n");
pthread_cleanup_pop(1);
return NULL;
}
void cam_cleanup(void *arg)
{
static unsigned char first_run = 1;
if ( !first_run )
{
DBG("already cleaned up ressources\n");
return;
}
first_run = 0;
IPRINT("cleaning up ressources allocated by input thread\n");
/* restore behaviour of the LED to auto */
input_cmd(IN_CMD_LED_AUTO, 0);
close_v4l2(videoIn);
if (videoIn->tmpbuffer != NULL) free(videoIn->tmpbuffer);
if (videoIn != NULL) free(videoIn);
if (pglobal->buf != NULL) free(pglobal->buf);
//释放jpeg压缩、解压缩缓存 if (y != NULL) free(y);
if (u != NULL) free(u);
if (v != NULL) free(v);
if (processed_jpeg_data != NULL) free(processed_jpeg_data);
}
程序中实现了二值化、负片和sobel滤波效果。
###############################################################
#
# Purpose: Makefile for "M-JPEG Streamer"
# Author.: Tom Stoeveken (TST)
# Version: 0.3
# License: GPL
#
###############################################################
CC = arm-linux-gcc
OTHER_HEADERS = ../../mjpg_streamer.h ../../utils.h ../output.h ../input.h
CFLAGS += -O2 -DLINUX -D_GNU_SOURCE -Wall -shared -fPIC
#CFLAGS += -DDEBUG
LFLAGS += -ljpeg -lm #for math.h
all: input_uvc.so
clean:
rm -f *.a *.o core *~ *.so *.lo
input_uvc.so: $(OTHER_HEADERS) input_uvc.c v4l2uvc.lo dynctrl.lo jpeg_utils.lo jpegutils.lo
$(CC) $(CFLAGS) $(LFLAGS) -o $@ input_uvc.c v4l2uvc.lo dynctrl.lo jpeg_utils.lo jpegutils.lo
v4l2uvc.lo: huffman.h uvc_compat.h uvcvideo.h v4l2uvc.c v4l2uvc.h
$(CC) -c $(CFLAGS) -o $@ v4l2uvc.c
jpeg_utils.lo: jpeg_utils.c jpeg_utils.h
$(CC) -c $(CFLAGS) -o $@ jpeg_utils.c
dynctrl.lo: dynctrl.c dynctrl.h uvcvideo.h
$(CC) -c $(CFLAGS) -o $@ dynctrl.c
jpegutils.lo: jpegutils.c jpegutils.h
$(CC) -c $(CFLAGS) -o $@ jpegutils.c
编译完成后通过TQ2440进行测试,在终端中启动mjpg_streamer,客户端输出结果如下:
至此修改终于成功了。客户端软件看这里:http://www.armbbs.net/forum.php?mod=viewthread&tid=17042。除了使用客户端软件,还可以直接使用浏览器查看。
但是修改还没有结束,以上使用了自己编写代码的方法实现了图像处理功能,OpenCV是图像处理利器,如果能进一步将OpenCV应用到这里,能够大大简化我们的代码量,同时能够实现更复杂的算法。Opencv在x86 Linux和arm_linux上的移植我已经成功实现了,看我的另一篇博文:Opencv2.3.1在ubuntu10.04和TQ2440 arm-linux上的移植与测试。
这里我们进行背景差分算法的实现。主要参考了Opencv官网中的读视频文件和运动检测范例,此外涉及到IplImage类型与unsigned char*类型数据转换的问题,参考了BMP与IplImage相互转换范例。
使用Opencv需要加入头文件:
#include "cv.h"
#include "highgui.h"
主要算法如下:
//将输入的jpeg图像解压成YUV分量
decode_jpeg_raw(videoIn->tmpbuffer,videoIn->buf.bytesused,0,420,videoIn->width,videoIn->height,y,u,v);
//这里可以对Y分量进行更改加入图像处理算法
framenumber++;
pFrame->imageData=(char *)y;
//Canny算子
//cvCanny(pImg, pCannyImg, 50, 150, 3);
if(framenumber == 1)
{
pBkImg = cvCreateImage(cvSize(videoIn->width,videoIn->height), IPL_DEPTH_8U,1);
pFrImg = cvCreateImage(cvSize(videoIn->width,videoIn->height), IPL_DEPTH_8U,1);
pBkMat = cvCreateMat(videoIn->height, videoIn->width, CV_32FC1);
pFrMat = cvCreateMat(videoIn->height, videoIn->width, CV_32FC1);
pFrameMat = cvCreateMat(videoIn->height, videoIn->width, CV_32FC1);
//转化成单通道图像再处理
pBkImg=pFrame;//当前帧为背景
pFrImg=pFrame;//当前帧转为灰度并作为前景
cvConvert(pFrImg, pFrameMat);//图像转为矩阵,以便计算
cvConvert(pFrImg, pFrMat);
cvConvert(pFrImg, pBkMat);
}
else //从第2帧开始
{
pFrImg=pFrame;//将当前帧作为前景并转灰度
cvConvert(pFrImg, pFrameMat);
//高斯滤波先,以平滑图像
cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0,0);
//当前帧跟背景图相减
cvAbsDiff(pFrameMat, pBkMat, pFrMat);//计算当前帧与背景的差的绝对值,作为前景
//二值化前景图
cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);
//进行形态学滤波,去掉噪音
cvErode(pFrImg, pFrImg, 0, 1);
cvDilate(pFrImg, pFrImg, 0, 1);
//更新背景,背景自动更新,权值0.003
cvRunningAvg(pFrameMat, pBkMat, 0.03, 0);
//将UV分量设置为128,压缩后为灰度图像
memset(u,128,length/4);
memset(v,128,length/4);
//将YUV分量压缩成jpeg
encode_jpeg_raw(processed_jpeg_data,length,80,0,420,videoIn->width,videoIn->height,(unsigned char *)pFrImg->imageData,u,v);
}
附上使用Opencv的Makefile:
###############################################################
#
# Purpose: Makefile for "M-JPEG Streamer"
# Author.: Tom Stoeveken (TST)
# Version: 0.3
# License: GPL
#
###############################################################
CC = arm-linux-gcc
OTHER_HEADERS = ../../mjpg_streamer.h ../../utils.h ../output.h ../input.h
#################OpenCV Package Information for pkg-config##############
prefix=/opt/EmbedSky/opencv_arm
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
#
#Name: OpenCV
#Description: Open Source Computer Vision Library
#Version: 2.3.1
#Libs: -L${libdir} -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -#lopencv_contrib -lopencv_legacy -lopencv_flann -lpthread -lrt
#Cflags: -I${includedir_old} -I${includedir_new}
CV_LFLAGS += -ljpeg -L${libdir} -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -lpthread -lrt
CV_CFLAGS += -O2 -DLINUX -D_GNU_SOURCE -Wall -shared -fPIC -I${includedir_old} -I${includedir_new}
##########end#############################################
CFLAGS += -O2 -DLINUX -D_GNU_SOURCE -Wall -shared -fPIC
#CFLAGS += -DDEBUG
LFLAGS += -ljpeg -lm #for math.h
all: input_uvc.so
clean:
rm -f *.a *.o core *~ *.so *.lo
input_uvc.so: $(OTHER_HEADERS) input_uvc.c v4l2uvc.lo dynctrl.lo jpeg_utils.lo jpegutils.lo
$(CC) $(CV_CFLAGS) $(CV_LFLAGS) -o $@ input_uvc.c v4l2uvc.lo dynctrl.lo jpeg_utils.lo jpegutils.lo
v4l2uvc.lo: huffman.h uvc_compat.h uvcvideo.h v4l2uvc.c v4l2uvc.h
$(CC) -c $(CFLAGS) -o $@ v4l2uvc.c
jpeg_utils.lo: jpeg_utils.c jpeg_utils.h
$(CC) -c $(CFLAGS) -o $@ jpeg_utils.c
dynctrl.lo: dynctrl.c dynctrl.h uvcvideo.h
$(CC) -c $(CFLAGS) -o $@ dynctrl.c
jpegutils.lo: jpegutils.c jpegutils.h
$(CC) -c $(CFLAGS) -o $@ jpegutils.c
编译成功后放到开发板验证,结果如下:
前两张图是建立背景,第三张是背景差分结果,从图中可以看出背景差分效果很好。
至此,我们已经能够成功将Opencv应用于mjpg_streamer项目中,为实现更复杂的算法打下了基础。值得一提的是,此次我使用的验证平台是TQ2440,虽然修改后的mjpg_streamer能够成功运行,但是速度实在不敢恭维。如果能够使用更高端的芯片验证,相信效果会更流畅。
最后附上修改后的项目源码:my_mjpg_streamer