Search code examples
opencvvideoprocessingvideo-captureimagefilter

How to store image from cam?


I am just started with processing.js.

The goal of my program is adept image filter(opencv) to video frame.

So I thought (However I found out it does not working in this way :<) :

  • get video steam from Capture object which in processing.video package.
  • Store current Image(I hope it can store as PImage Object).
  • adept OpenCV image Filter
  • call image method with filtered PImage Object.

I found out how to get video stream from cam, but do not know how to store this.

import processing.video.*;
import gab.opencv.*;

Capture cap;
OpenCV opencv;

public void setup(){
  //size(800, 600);
  size(640, 480);
  colorMode(RGB, 255, 255, 255, 100);

  cap = new Capture(this, width, height);
  opencv = new OpenCV(this, cap);
  cap.start();
  background(0);
}

public void draw(){
  if(cap.available()){
    //return void 
    cap.read();
  }

  image(cap, 0, 0);
}

this code get video stream and show what it gets. However, I can not store single frame since Capture.read() returns 'void'.

After Store current frame I would like to transform PImage s with OpenCV like :

 PImage gray = opencv.getSnapshot();

  opencv.threshold(80);
  thresh = opencv.getSnapshot();

  opencv.loadImage(gray);
  opencv.blur(12);  
  blur = opencv.getSnapshot();

  opencv.loadImage(gray);
  opencv.adaptiveThreshold(591, 1);
  adaptive = opencv.getSnapshot();

Is there any decent way to store and transform current frame? (I think my way - this means show frame after save current image and transform - uses lots of resources depend on frame rate)

Thanks for answer :D


Solution

  • not sure what you want to do, I'm sure you solved it already, but this could be useful for someone anyway...

    It seems you can just write the Capture object's name directly and it returns a PImage:

     cap = new Capture(this, width, height);
    
    //Code starting and reading capture in here
    
    PImage snapshot = cap;
    //Then you can do whatever you wanted to do with the PImage
    snapshot.save("snapshot.jpg");
    //Actually this seems to work fine too
    cap.save("snapshot.jpg");