week 6 ++ video capture with openCV
|
|
getting openCV set up:
well, it took some work to get the openCV code for openFrameworks working.
I found the instructions that came with it a little unclear, so I relied more on my common sense
and finally got it working.
|
|
|
blob customization ( code) :
added some iterative erosion/dilation of the background subtracted image,
this effect seems prety similar to the 'median' filter in Photoshop.
Although I found that the first iteration helped, but after that it provided only neglible improvement,
at least for blob detection.
(Note the iteration is controlled by 'n' and 'm' keys.)
Also, took some work figuring out that the grayscaleImage had all the centroid properties I needed, and then overlayed those in blue.
|
void testApp::update(){
ofBackground(100,100,100);
vidGrabber.grabFrame();
if (vidGrabber.isFrameNew()){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage.setFromColorImage(colorImg);
if (bLearnBakground == true){
grayBg = grayImage;
bLearnBakground = false;
}
grayDiff.absDiff(grayBg, grayImage);
grayDiff.threshold(threshold);
for(int i = 0; i< iterate; i++){
grayDiff.erode_3x3();
grayDiff.dilate_3x3();
}
contourFinder.findContours(grayDiff, 20, (340*240)/3, 15, true);
}
}
/* excerpt from void testApp::draw(){ */
for(int b = 0; b < contourFinder.nBlobs; b++){
float cx = contourFinder.blobs[b].centroid.x;
float cy = contourFinder.blobs[b].centroid.y;
ofFill();
ofSetColor(0x0000ff);
ofRect(360 + cx, 540 + cy, 5, 5);
}
}
|
|
motion detector ( code) :
a very simple motion detector... it compares current frame to last frame with

|
void testApp::update(){
ofBackground(100,100,100);
vidGrabber.grabFrame();
if (vidGrabber.isFrameNew()){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage.setFromColorImage(colorImg);
grayDiff.absDiff(grayBg, grayImage);
grayBg = grayImage;
grayDiff.threshold(threshold);
for(int i = 0; i< iterate; i++){
grayDiff.erode_3x3();
grayDiff.dilate_3x3();
}
contourFinder.findContours(grayDiff, 20, (340*240)/3, 15, true); // find holes
}
}
|
|
motion histogram ( code) :
accumulate motion detection... well, I created two of these motion histograms.
the top one is a colorImage drawn in white (I wanted to progressively tint the frames over time,
so each frame would be a different color -- but couldn't figure out an easy way of tinting with the built-in methods,
seems like it would require working with the pixel values.)
the second histogram is a grayscaleImage drawn with changing colors.
so to get these working I found that I had to create at least a couple of temporary images (especially when dealing with the colorImage).
and I think this approach is probably a big resource hog, but it works.

|
void testApp::update(){
ofBackground(100,100,100);
vidGrabber.grabFrame();
if (vidGrabber.isFrameNew()){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage.setFromColorImage(colorImg);
grayDiff.absDiff(grayBg, grayImage);
grayBg = grayImage;
grayDiff.threshold(threshold);
for(int i = 0; i< iterate; i++){
grayDiff.erode_3x3();
grayDiff.dilate_3x3();
}
grayDiff2 -= 8;
grayDiff2 += grayDiff;
tempColor2.setFromGrayscaleImage(grayDiff);
colorDiff -= tempColor;
colorDiff += tempColor2;
contourFinder.findContours(grayDiff, 20, (340*240)/3, 15, true); // find holes
}
time += .2;
if(time > 255){
time = 0;
}
}
|
|