Ask Your Question
0

How to direct BlobDetection script to local video file, instead of cam

asked 2014-09-06 07:06:12 -0600

LauraK gravatar image

updated 2014-09-06 07:26:12 -0600

berak gravatar image

Hi, I am new to OpenCV and using BlobDetection library but need to direct the script to use a local video file instead of the camera. Here is the original script. Would love someone's help please. Thank you.

Using MacOS 64bit

// - Super Fast Blur v1.1 by Mario Klingemann http://incubator.quasimondo.com // - BlobDetection library

import processing.video.; import blobDetection.;

Capture cam; BlobDetection theBlobDetection; PImage img; boolean newFrame=false;

// ================================================== // setup() // ================================================== void setup() { // Size of applet size(640, 480); // Capture cam = new Capture(this, 404, 304, 15); // Comment the following line if you use Processing 1.5 cam.start();

// BlobDetection
// img which will be sent to detection (a smaller copy of the cam frame);
img = new PImage(80,60); 
theBlobDetection = new BlobDetection(img.width, img.height);
theBlobDetection.setPosDiscrimination(true);
theBlobDetection.setThreshold(0.9f); // will detect bright areas whose luminosity > 0.2f;

}

// ================================================== // captureEvent() // ================================================== void captureEvent(Capture cam) { cam.read(); newFrame = true; }

// ================================================== // draw() // ================================================== void draw() { if (newFrame) { newFrame=false; image(cam,0,0,width,height); img.copy(cam, 0, 0, cam.width, cam.height, 0, 0, img.width, img.height); fastblur(img, 2); theBlobDetection.computeBlobs(img.pixels); drawBlobsAndEdges(true,true); } }

// ================================================== // drawBlobsAndEdges() // ================================================== void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges) { noFill(); Blob b; EdgeVertex eA,eB; for (int n=0 ; n<theblobdetection.getblobnb() ;="" n++)="" {="" b="theBlobDetection.getBlob(n);" if="" (b!="null)" {="" edges="" if="" (drawedges)="" {="" strokeweight(3);="" stroke(0,255,0);="" for="" (int="" m="0;m&lt;b.getEdgeNb();m++)" {="" ea="b.getEdgeVertexA(m);" eb="b.getEdgeVertexB(m);" if="" (ea="" !="null" &amp;&amp;="" eb="" !="null)" line(="" ea.x<em="">width, eA.yheight, eB.xwidth, eB.yheight ); } }

        // Blobs
        if (drawBlobs)
        {
            strokeWeight(1);
            stroke(255,0,0);
            rect(
                b.xMin*width,b.yMin*height,
                b.w*width,b.h*height
                );
        }

    }

  }

}

// ================================================== // Super Fast Blur v1.1 // by Mario Klingemann // http://incubator.quasimondo.com // ================================================== void fastblur(PImage img,int radius) { if (radius<1){ return; } int w=img.width; int h=img.height; int wm=w-1; int hm=h-1; int wh=wh; int div=radius+radius+1; int r[]=new int[wh]; int g[]=new int[wh]; int b[]=new int[wh]; int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw; int vmin[] = new int[max(w,h)]; int vmax[] = new int[max(w,h)]; int[] pix=img.pixels; int dv[]=new int[256div]; for (i=0;i<256*div;i++){ dv[i]=(i/div); }

yw=yi=0;

for (y=0;y<h;y++){ rsum="gsum=bsum=0;" for(i="-radius;i&lt;=radius;i++){" p="pix[yi+min(wm,max(i,0))];" rsum+="(p" &amp;="" 0xff0000)&gt;&gt;16;="" gsum+="(p" &amp;="" 0x00ff00)&gt;&gt;8;="" bsum+="p" &amp;="" 0x0000ff;="" }="" for="" (x="0;x&lt;w;x++){&lt;/p">

  r[yi]=dv[rsum];
  g[yi]=dv[gsum];
  b[yi]=dv[bsum];

  if(y==0){
    vmin[x]=min(x+radius+1,wm);
    vmax[x]=max(x-radius,0);
  }
  p1=pix[yw+vmin[x]];
  p2=pix[yw+vmax[x]];

  rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
  gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
  bsum+= (p1 ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-09-06 09:46:35 -0600

Why do you copy all of Marios Code if you are only interested in the capture? It's hard to find your problem if you hide it deep within irrelevant code.

The line you have to look at is this one:

Capture cam = new Capture(this, 404, 304, 15);

Just open a file as described here: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-videocapture

edit flag offensive delete link more

Comments

Hi FooBar,

Thanks for you help, but as I mentioned I am new to this and the part I am most interested in keeping is the blob detection in a video (which is most of the script). The link you suggest has instructions for pyhton and C++ but I am not familiar with either and using processing. Which will work for me?

LauraK gravatar imageLauraK ( 2014-09-06 11:06:40 -0600 )edit

Oh sorry, I thought this was OpenCV-Code. Your problem has nothing to do with OpenCV but only with Processing! I think you will have to use the Movie Class in Processing: http://www.processing.org/reference/libraries/video/ It also gives you a read function to get the next frame, just like the Capture-Class.

FooBar gravatar imageFooBar ( 2014-09-06 11:11:04 -0600 )edit

Thank you, that makes sense! I will look and hopefully find an example that also does Blob detection, within Video which is what I am really looking for.

LauraK gravatar imageLauraK ( 2014-09-06 11:39:55 -0600 )edit

If that solved your problem, you could also accept/upvote the answer

FooBar gravatar imageFooBar ( 2014-09-07 07:18:00 -0600 )edit

Question Tools

Stats

Asked: 2014-09-06 07:06:12 -0600

Seen: 317 times

Last updated: Sep 06 '14