【发布时间】:2011-02-09 16:20:34
【问题描述】:
我正在寻找一种在 OSX 下以编程方式分析来自外部 USB 网络摄像头的视频源的方法。
因为在我现在有点迷失从哪里开始之前,我还没有做过任何这样的低级编程。
如何访问网络摄像头源并获取图像数据以进行进一步处理? 在这一点上,我只是想了解基本概念,而不是寻找特定于语言的解决方案。 任何示例代码都将受到高度赞赏。
如果有人能指出我正确的方向并帮助我开始,我将不胜感激。
非常感谢您!
托马斯
【问题讨论】:
我正在寻找一种在 OSX 下以编程方式分析来自外部 USB 网络摄像头的视频源的方法。
因为在我现在有点迷失从哪里开始之前,我还没有做过任何这样的低级编程。
如何访问网络摄像头源并获取图像数据以进行进一步处理? 在这一点上,我只是想了解基本概念,而不是寻找特定于语言的解决方案。 任何示例代码都将受到高度赞赏。
如果有人能指出我正确的方向并帮助我开始,我将不胜感激。
非常感谢您!
托马斯
【问题讨论】:
使用OpenCV。
如果您正在寻找显示网络摄像头图像的代码示例,请查看我之前关于此主题的答案。它将视频源转换为灰度并将它们显示在窗口上:
如果您只想显示框架,则将 else 块替换为:
else
{
cvShowImage("Colored video", color_frame);
}
如果您正在徘徊如何操作帧的像素:
int width = color_frame->width;
int height = color_frame->height;
int bpp = color_frame->nChannels;
for (int i=0; i < width*height*bpp; i+=bpp)
{
if (!(i % (width*bpp))) // print empty line for better readability
std::cout << std::endl;
std::cout << std::dec << "R:" << (int) color_frame->imageData[i] <<
" G:" << (int) color_frame->imageData[i+1] <<
" B:" << (int) color_frame->imageData[i+2] << " ";
}
【讨论】:
要快速访问网络摄像头和处理像素数据,您可以使用 Processing 和 Video library - 最简单的开始方法是查看与 IDE 捆绑的示例。
Processing 是一种基于 java 的可视化语言,易于学习和使用,适用于 WIndows、MacOSX 和 Linux。我发现网络摄像头在我的 MacBook 上开箱即用。
这是一个示例脚本(基于捆绑在 IDE 中的示例),它加载网络摄像头提要并以灰度呈现像素。
import processing.video.*;
int numPixels;
Capture video;
void setup() {
// Change size to 320 x 240 if too slow at 640 x 480
size(640, 480, P2D);
video = new Capture(this, width, height, 24);
numPixels = video.width * video.height;
// Make the pixels[] array available for direct manipulation
loadPixels();
}
void draw() {
if (video.available()) {
video.read(); // Read a new video frame
video.loadPixels(); // Make the pixels of video available
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
// Make all the pixels grey if mouse is pressed
if (mousePressed) {
float greyVal = brightness(video.pixels[i]);
pixels[i] = color(greyVal);
} else {
// If mouse not pressed, show normal video
pixels[i] = video.pixels[i];
}
}
updatePixels(); // Notify that the pixels[] array has changed
}
}
此外,还有一个很棒的interface to OpenCV,可用于边缘检测等。
【讨论】: