在 OSX 下分析摄像头输入

发布于 2024-10-16 18:59:31 字数 236 浏览 0 评论 0原文

我正在寻找一种在 OSX 下以编程方式分析来自外部 USB 网络摄像头的视频源的方法。

因为我之前没有做过任何像这样的低级编程,所以我目前有点迷失从哪里开始。

如何访问网络摄像头并抓取图像数据以进行进一步处理? 此时我只是想理解基本概念,而不是寻找特定于语言的解决方案。 任何示例代码将不胜感激。

如果有人能指出我正确的方向并帮助我开始,我将非常感激。

预先非常感谢您!

托马斯

I am looking for a way to programmatically analyze a video feed from an external usb webcam under OSX.

Since I haven't done any low level programming like this before I am currently kind of lost on where to start.

How can I access a webcam feed and grab the image data to then process further?
At this point I am just trying to understand the basic concept and am not looking for language-specific solutions.
Any sample code would be highly appreciated.

I'd appreciate it very much if someone could point me in the right direction and help me get started.

Thank you very much in advance!

Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-10-23 18:59:31

使用OpenCV

如果您正在寻找显示网络摄像头图像的代码示例,请查看我之前关于此主题的答案。它将视频源转换为灰度并将其显示在窗口上:

OpenCV 2.1:运行时error

如果您只想显示帧,请将 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] << " "; 
}

Use OpenCV.

And check my previous answer on this subject if you are looking for a code example to display the webcam images. It converts the video feed to grayscale and displays them on a window:

OpenCV 2.1: Runtime error

If you just want to display the frames, then replace the else block by this:

else
{
    cvShowImage("Colored video", color_frame);
}

In case you are wandering how to manipulate the pixels of the 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] << " "; 
}
终陌 2024-10-23 18:59:31

要快速访问网络摄像头并操作像素数据,您可以将 Processing视频库 - 最简单的开始方法是查看与 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
  }
}

此外,还有一个很棒的OpenCV 接口,可用于边缘检测等。

For quick access to a webcam and for manipulation of pixel data, you can use Processing with the Video library - the easiest way to start is to check out the examples bundled with the IDE.

Processing is a java based visualisation language which is easy to learn and use and works on WIndows, MacOSX and Linux. I found the webcam stuff worked out of the box on my MacBook.

Here is an example script (based on an example bundled in the IDE) which loads a webcam feed and renders the pixels in greyscale.


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
  }
}

Moreover, there is a great interface to OpenCV which can be used for edge detection etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文