当前位置:开发频道首页 » 嵌入式开发 »

基于Video4Linux 的USB摄像头图像采集实现 (2)

2007-12-28 22:42:29  作者:  来源:华星在线  文字大小:【+】【-
简介:基于Video4Linux 的USB摄像头图像采集实现
关 键 字: V4L video Linux 摄像头 图像


七彩谷商城

二 Video4linux 编程

2.1 Video4linux 简介

Video4Linux是为市场现在常见的电视捕获卡和并口及USB口的摄像头提供统一的编程接口。同时也提供无线电通信和文字电视广播解码和垂直消隐的数据接口。本文主要针对USB摄像头设备文件/dev/video0,进行视频图像采集方面的程序设计。

2.2 Video4linux 编程指南

1.视频编程的流程

(1)打开视频设备:

(2)读取设备信息

(3)更改设备当前设置(可以不做)

(4)进行视频采集,两种方法:

a.内存映射

b.直接从设备读取

(5)对采集的视频进行处理

(6)关闭视频设备。

定义的数据结构及使用函数

struct _v4l_struct
{
	int fd;
	struct video_capability capability;
	struct video_buffer buffer;
	struct video_window window;
	struct video_channel channel[8];
	struct video_picture picture;
	struct video_mmap mmap;
	struct video_mbuf mbuf;
	unsigned char *map;
};

typedef struct _v4l_struct v4l_device;
extern int v4l_open(char *, v4l_device *);
extern int v4l_close(v4l_device *);
extern int v4l_get_capability(v4l_device *);
extern int v4l_set_norm(v4l_device *, int);
extern int v4l_get_picture(v4l_device *);
extern int v4l_grab_init(v4l_device *, int, int);
extern int v4l_grab_frame(v4l_device *, int);
extern int v4l_grab_sync(v4l_device *);
extern int v4l_mmap_init(v4l_device *);
extern int v4l_get_mbuf(v4l_device *);
extern int v4l_get_picture(v4l_device *);
extern int v4l_grab_picture(v4l_device *, unsigned int);
extern int v4l_set_buffer(v4l_device *);
extern int v4l_get_buffer(v4l_device *);
extern int v4l_switch_channel(v4l_device *, int);

3.Video4linux支持的数据结构及其用途

(1)video_capability 包含设备的基本信息(设备名称、支持的最大最小分辨率、信号源信息等)

name[32] 设备名称

maxwidth

maxheight

minwidth

minheight

Channels 信号源个数

type 是否能capture , 彩色还是黑白, 是否能裁剪等等。值如

VID_TYPE_CAPTURE等

(2)video_picture 设备采集的图象的各种属性

Brightness 0~65535

hue

colour

contrast

whiteness

depth 8 16 24 32

palette VIDEO_PALETTE_RGB24 | VIDEO_PALETTE_RGB565|

VIDEO_PALETTE_JPEG| VIDEO_PALETTE_RGB32

(3)video_channel 关于各个信号源的属性

Channel 信号源的编号

name

tuners

Type VIDEO_TYPE_TV | IDEO_TYPE_CAMERA

Norm 制式 PAL|NSTC|SECAM|AUTO

(4)video_window 包含关于capture area的信息

x x windows 中的坐标.

y y windows 中的坐标.

width The width of the image capture.

height The height of the image capture.

chromakey A host order RGB32 value for the chroma key.

flags Additional capture flags.

clips A list of clipping rectangles. (Set only)

clipcount The number of clipping rectangles. (Set only)

(5)video_mbuf 利用mmap进行映射的帧的信息

size 每帧大小

Frames 最多支持的帧数

Offsets 每帧相对基址的偏移

(6)video_mmap 用于mmap

4.关键步骤介绍

【注】接多个摄像头。方法如下:买一个usb hub接到开发板的usb host上。cat /proc/devices可以知道video capture device的major是81,再ls –l /dev看到video0 的次设备号是0。两个摄像头当然要两个设备号,所以mknod /dev/video1 c 81 1,如果接4个,就mknod /dev/video2 c 81 2,mknod /dev/video3c 81 3。依次类推。

(1)打开视频:

int v4l_open(char *dev, v4l_device *vd)
{
	if (!dev)
		dev = ”/dev/video0”;

	if ((vd ->fd = open(dev, O_RDWR)) < 0) {
		perror("v4l_open:");
		return -1;
	}

	if (v4l_get_capability(vd))
		return -1;

	if (v4l_get_picture(vd))
		return -1;
		
	return 0;
}

(2)读video_capability 中信息

int v4l_get_capability(v4l_device *vd)
{
	if (ioctl(vd ->fd, VIDIOCGCAP, &(vd->capability)) < 0) {
		perror("v4l_get_capability:");
		return -1;
	}
	return 0;
}

成功后可读取vd->capability各分量

(3)读video_picture中信息

int v4l_get_picture(v4l_device *vd)
{
	if (ioctl(vd ->fd, VIDIOCGPICT, &(vd->picture)) < 0) {
		perror("v4l_get_picture:");
		return -1;
	}

	return 0;
}

成功后可读取图像的属性

(4)改变video_picture中分量的值 (可以不做的)

先为分量赋新值,再调用VIDIOCSPICT

vd->picture.colour = 65535;
if(ioctl(vd->fd, VIDIOCSPICT, &(vd->picture)) < 0)
{
	perror("VIDIOCSPICT");
	return -1;
}

(5)初始化channel (可以不做的)

必须先做得到vd->capability中的信息

int v4l_get_channels(v4l_device *vd)
{
	int i;
	for (i = 0; i < vd ->capability.channels; i++) {
		vd ->channel[i].channel = i;
		if (ioctl(vd ->fd, VIDIOCGCHAN, &(vd->channel[i])) < 0) {
			perror("v4l_get_channel:");
			return -1;
		}
	}
	return 0;
}

(6)关闭设备

int v4l_close(v4l_device *vd)
{
	close(vd ->fd);
	return 0;
}
1 2 3 4 5
七彩谷商城
 

最新文章

更多

推荐文章

更多

热点文章

更多

相关文章

更多

· Linux 操作系统讲义
· 利用Video4Linux获取摄像头数据
· 在你的Ubuntu Linux桌面上嵌入终端窗口
· 嵌入式Linux系统中I/O端口需要注意的问题