views:

33

answers:

2

I've tried writing a simple application thats supposed to detect pixel differences from the /dev/video device. Like motion does.

I don't know how the /dev/video device works, so most of it was guesswork. What I found is that it seems like the data ( from the specific webcam ) can be divided into sections of 8192 bytes. I assume each represents a frame. The first +-600 bytes from each "frame" is identical to the previous frame.

How can i interpret that data into a understandable matrice of pixels?

The program code for refference:

#!/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
+2  A: 

Reading from /dev/video is not straightforward. I suggest using an especific library for this. Maybe you can try OpenCV lib. It has an easy interface to raw pixels in webcams and cameras.

jartieda
+1. opencv is cool
NixNinja
+1  A: 

I know nothing about the topic, but maybe this applies?
There is a documentation explaining what each byte means, and code samples in C:

http://v4l2spec.bytesex.org

Nicolas Raoul
linked to exactly what i need. much thanks
NixNinja