歡迎您光臨本站 註冊首頁

如何利用Video4Linux獲取攝像頭數據

←手機掃碼閱讀     火星人 @ 2014-03-24 , reply:0

  Video4Linux是Linux下用於獲取視頻和音頻數據的API介面,在這篇文章中,著重闡述如何利用Video4Linux獲取攝像頭數據,以實現連續影像的播放。

1. 攝像頭的安裝

  在Linux下常用的攝像頭驅動是spca5xx, 這是一個通用驅動,讀者可以在以下網站下到這個驅動 http://mxhaard.free.fr/download.html。這個網站還給出了這款驅動支持的攝像頭的種類。另外,ov511晶元直接就支持Linux,使用者款晶元的攝像頭有網眼V2000。我使用的是網眼V2000的攝像頭,和Z-Star 301p+現代7131R晶元的攝像頭。后一種需要spca5xx的驅動。關於spca5xx的安裝方法,網上有很多介紹,這裡就不說了。

2. 攝像頭的調試

  安裝好攝像頭后,為了測試攝像頭能否正常工作,可以用一下軟體。比較著名的是xawtv,在網上搜以下可以下載到。安裝好后,打開xawtv則可以調試攝像頭。

3. Video4Linux 編程獲取數據

  現有的video4linux有兩個版本,v4l和v4l2。本文主要是關於v4l的編程。利用v4l API獲取視頻圖像一般有以下幾步:

a> 打開設備

b> 設置設備的屬性,比如圖像的亮度,對比度等等

c> 設定傳輸格式和傳輸方式

d> 開始傳輸數據,一般是一個循環,用以連續的傳輸數據

e> 關閉設備

  下面具體介紹v4l編程的過程。首先指出,在video4linux編程時要包含頭文件,其中包含了video4linux的數據結構和函數定義。

1)v4l的數據結構

  在video4linux API中定義了如下數據結構,詳細的數據結構定義可以參考v4l API的文檔,這裡就編程中經常使用的數據結構作出說明。

  首先我們定義一個描述設備的數據結構,它包含了v4l中定義的所有數據結構:
CODE:
typedef struct _v4ldevice
{int fd;//設備號
struct video_capability capability;
struct video_channel channel[10];
struct video_picture picture;
struct video_clip clip;
struct video_window window;
struct video_capture capture;
struct video_buffer buffer;
struct video_mmap mmap;
struct video_mbuf mbuf;
struct video_unit unit;
unsigned char *map;//mmap方式獲取數據時,數據的首地址
pthread_mutex_t mutex;
int frame;
int framestat[2];
int overlay;
}v4ldevice;

下面解釋上面這個數據結構中包含的數據結構,這些結構的定義都在中。
CODE:
* struct video_capability
name[32] Canonical name for this interface
type Type of interface
channels Number of radio/tv channels if appropriate
audios Number of audio devices if appropriate
maxwidth Maximum capture width in pixels
maxheight Maximum capture height in pixels
minwidth Minimum capture width in pixels
minheight Minimum capture height in pixels

  這一個數據結構是包含了攝像頭的屬性,name是攝像頭的名字,maxwidth maxheight是攝像頭所能獲取的最大圖像大小,用像素作單位。

  在程序中,通過ioctl函數的VIDIOCGCAP控制命令讀寫設備通道已獲取這個結構,有關ioctl的使用,比較複雜,這裡就不說了。下面列出獲取這一數據結構的代碼:
CODE:
int v4lgetcapability(v4ldevice *vd)
{
if(ioctl(vd->fd, VIDIOCGCAP, &(vd->capability)) < 0) {
v4lperror("v4lopen:VIDIOCGCAP");
return -1;
}
return 0;
}
* struct video_picture
brightness Picture brightness
hue Picture hue (colour only)
colour Picture colour (colour only)
contrast Picture contrast
whiteness The whiteness (greyscale only)
depth The capture depth (may need to match the frame buffer depth)
palette Reports the palette that should be used for this image

  這個數據結構主要定義了圖像的屬性,諸如亮度,對比度,等等。這一結構的獲取通過ioctl發出VIDIOCGPICT控制命令獲取。
CODE:
* struct video_mbuf
size The number of bytes to map
frames The number of frames
offsets The offset of each frame

這個數據結構在用mmap方式獲取數據時很重要:

size表示圖像的大小,如果是640*480的彩色圖像,size=640*480*3

frames表示幀數

offsets表示每一幀在內存中的偏移地址,通過這個值可以得到數據在圖像中的地址。

得到這個結構的數據可以用ioctl的VIDIOCGMBUF命令。源碼如下:
CODE:
int v4lgetmbuf(v4ldevice *vd)
{
if(ioctl(vd->fd, VIDIOCGMBUF, &(vd->mbuf))<0) {
v4lperror("v4lgetmbuf:VIDIOCGMBUF");
return -1;
}
return 0;
}

  而數據的地址可以有以下方式計算:
CODE:
unsigned char *v4lgetaddress(v4ldevice *vd)
{
return (vd->map + vd->mbuf.offsets[vd->frame]);
}

2)獲取影像mmap方式。

  在video4linux下獲取影像有兩種方式:overlay和mmap。由於我的攝像頭不支持overlay方式,所以這裡只談mmap方式。

  mmap方式是通過內存映射的方式獲取數據,系統調用ioctl的VIDIOCMCAPTURE后,將圖像映射到內存中,然後可以通過前面的v4lgetmbuf(vd)函數和v4lgetaddress(vd)函數獲得數據的首地址,這是李可以選擇是將它顯示出來還是放到別的什麼地方。

  下面給出獲取連續影像的最簡單的方法(為了簡化,將一些可去掉的屬性操作都去掉了):
CODE:
char* devicename="/dev/video0";
char* buffer;
v4ldevice device;
int width = 640;
int height = 480;
int frame = 0;
v4lopen("/dev/video0",&device);//打開設備
v4lgrabinit(&device,width,height);//初始化設備,定義獲取的影像的大小
v4lmmap(&device);//內存映射
v4lgrabstart(&device,frame);//開始獲取影像
while(1){
v4lsync(&device,frame);//等待傳完一幀
frame = (frame+1)%2;//下一幀的frame
v4lcapture(&device,frame);//獲取下一幀
buffer = (char*)v4lgetaddress(&device);//得到這一幀的地址
//buffer給出了圖像的首地址,你可以選擇將圖像顯示或保存......
//圖像的大小為 width*height*3
..........................
}

為了更好的理解源碼,這裡給出裡面的函數的實現,這裡為了簡化,將所有的出錯處理都去掉了。
CODE:
int v4lopen(char *name, v4ldevice *vd)
{
int i;
if((vd->fd = open(name,O_RDWR)) < 0) {
return -1;
}
if(v4lgetcapability(vd))
return -1;
}
int v4lgrabinit(v4ldevice *vd, int width, int height)
{
vd->mmap.width = width;
vd->mmap.height = height;
vd->mmap.format = vd->picture.palette;
vd->frame = 0;
vd->framestat[0] = 0;
vd->framestat[1] = 0;
return 0;
}
int v4lmmap(v4ldevice *vd)
{
if(v4lgetmbuf(vd)<0)
return -1;
if((vd->map = mmap(0, vd->mbuf.size, PROT_READ|PROT_WRITE, MAP_SHARED, vd->fd, 0)) < 0) {
return -1;
}
return 0;
}
int v4lgrabstart(v4ldevice *vd, int frame)
{
vd->mmap.frame = frame;
if(ioctl(vd->fd, VIDIOCMCAPTURE, &(vd->mmap)) < 0) {
return -1;
}
vd->framestat[frame] = 1;
return 0;
}
int v4lsync(v4ldevice *vd, int frame)
{
if(ioctl(vd->fd, VIDIOCSYNC, &frame) < 0) {
return -1;
}
vd->framestat[frame] = 0;
return 0;
}
int c4lcapture(v4ldevice *vd, int frame)
{
vd->mmap.frame = frame;
if(ioctl(vd->fd, VIDIOCMCAPTURE, &(vd->mmap)) < 0) {
return -1;
}
vd->framestat[frame] = 1;
return 0;


[火星人 ] 如何利用Video4Linux獲取攝像頭數據已經有465次圍觀

http://coctec.com/docs/linux/show-post-134954.html