node-opencv icon indicating copy to clipboard operation
node-opencv copied to clipboard

How do I read pixels from VideoCapture (was Matrix data garbage from VideoCapture)

Open mark-hahn opened this issue 7 years ago • 2 comments

I'm new to opencv so be forgiving. :-)

I am trying to capture images from VideoCapture and work with the pixels. The webcam example that opens a window works great. When I break with an image (Matrix) to look at I see garbage numbers using im.row(0). My webcam is putting out 640x480 YUYV ...

 $ v4l2-ctl --all
Driver Info (not using libv4l2):
	Driver name   : uvcvideo
	Card type     : USB2.0 PC CAMERA: USB2.0 PC CAM
	Bus info      : usb-0000:00:14.0-2
	Driver version: 4.14.0
	Capabilities  : 0x84200001
		Video Capture
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps   : 0x04200001
		Video Capture
		Streaming
		Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
	Width/Height      : 640/480
	Pixel Format      : 'YUYV'
	Field             : None
	Bytes per Line    : 1280
	Size Image        : 614400
	Colorspace        : Default
	Transfer Function : Default
	YCbCr Encoding    : Default
	Quantization      : Default
	Flags             : 
Crop Capability Video Capture:
	Bounds      : Left 0, Top 0, Width 640, Height 480
	Default     : Left 0, Top 0, Width 640, Height 480
	Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 640, Height 480
Selection: crop_bounds, Left 0, Top 0, Width 640, Height 480
Streaming Parameters Video Capture:
	Capabilities     : timeperframe
	Frames per second: 30.000 (30/1)
	Read buffers     : 0
                     brightness (int)    : min=0 max=255 step=1 default=128 value=128
                       contrast (int)    : min=0 max=255 step=1 default=135 value=135
                     saturation (int)    : min=0 max=255 step=1 default=75 value=75
                            hue (int)    : min=-127 max=127 step=1 default=0 value=0
                          gamma (int)    : min=1 max=8 step=1 default=4 value=4
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
                      sharpness (int)    : min=0 max=15 step=1 default=5 value=5
         backlight_compensation (int)    : min=1 max=5 step=1 default=1 value=1

This is my code.

var cv = require('opencv');

try {
  var camera = new cv.VideoCapture(0);
  // var window = new cv.NamedWindow('Video', 0)
  let fc = 0;
  intvl = setInterval(function() {
    camera.read(function(err, im) {
      if (err) throw err;
      console.log(im.size())
      if (im.size()[0] > 0 && im.size()[1] > 0){
        // window.show(im);
        im.row(0).forEach( (val, idx) => {
          if(idx > 9)  process.exit(0);
          console.log(idx, val.toFixed(1));
        });
      }
      // window.blockingWaitKey(0, 50);
    });
  }, 20);
  
} catch (e){
  console.log("Couldn't start camera:", e)
}

And this is the output ...

Array(2) [480, 640]
0 -0.0
1 155099213.3
2 -0.0
3 2.892715739312047e+295
4 3.4603124953166786e+51
5 -0.0
6 -0.0
7 3.6396356834125945e+27
8 -0.0
9 -0.0

Note that different rows have similar garbage data. Any idea where I'm going wrong?

mark-hahn avatar Nov 13 '18 12:11 mark-hahn

I tried var buff = im.toBuffer(); and the data in the buffer looks reasonable with a quick inspection. So I can work with a buffer. I'll close this but I'm still curious why I can't just use values from im.row(0).

mark-hahn avatar Nov 13 '18 12:11 mark-hahn

I can't make any sense out of the data in the buffer. I've renamed this thread to indicate I am trying to read the pixels from a matrix and re-opening it. Can someone help?

mark-hahn avatar Nov 13 '18 12:11 mark-hahn