我已经尝试编写一个简单的应用程序来检测来自 /dev/video 设备的像素差异。喜欢 motion 确实如此。
我不知道/dev/video 设备是如何工作的,所以大部分都是猜测。我发现数据(来自特定网络摄像头)似乎可以分为 8192 字节的部分。我假设每个代表一个框架。每个“帧”的前 +-600 个字节与前一帧相同。
我如何将这些数据解释为可理解的像素矩阵?
引用程序代码:
#!/usr/bin/ruby
# calculates a percentage difference between two array's
def percentage_difference( arrayA, arrayB )
top_size = arrayA.size > arrayB.size ? arrayA.size : arrayB.size
diff_elements = 0;
0.upto( top_size - 1 ) do |i|
diff_elements += 1 unless arrayA[i] == arrayB[i]
end
( 1.0 * diffelements ) / top_size
end
cam = File.open( '/dev/video' );
lastframe = [];
while( true ) do
# reads a frame from the open video device ( cam ) and converts to bytes;
newframe = cam.readpartial( num_of_bytes_per_frame ).bytes.map { |b| b }
# prints the percentage difference between the two frames
puts percentage_difference( lastframe, newframe );
lastframe = newframe;
end
最佳答案
从/dev/video 读取数据并不简单。 我建议为此使用特定的库。 也许你可以试试 OpenCV 库。它有一个简单的接口(interface)来连接网络摄像头和相机中的原始像素。
关于ruby - 理解 "/dev/video"输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3474586/