Change background subtraction algorithm

This commit is contained in:
Mario Fleischmann 2021-05-17 14:10:09 +02:00
parent cdd4148156
commit 4b0d468c4f
6 changed files with 1767 additions and 1697 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">
@ -180,8 +180,8 @@
<PostBuildEvent /> <PostBuildEvent />
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\bpRecognizer.cpp" />
<ClCompile Include="src\main.cpp" /> <ClCompile Include="src\main.cpp" />
<ClCompile Include="src\ofApp.cpp" />
<ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxBaseGui.cpp" /> <ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxBaseGui.cpp" />
<ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxButton.cpp" /> <ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxButton.cpp" />
<ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxColorPicker.cpp" /> <ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxColorPicker.cpp" />
@ -199,8 +199,10 @@
<ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvHaarFinder.cpp" /> <ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvHaarFinder.cpp" />
<ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvImage.cpp" /> <ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvImage.cpp" />
<ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvShortImage.cpp" /> <ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvShortImage.cpp" />
<ClCompile Include="src\ofApp.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\bpRecognizer.h" />
<ClInclude Include="src\ofApp.h" /> <ClInclude Include="src\ofApp.h" />
<ClInclude Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxBaseGui.h" /> <ClInclude Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxBaseGui.h" />
<ClInclude Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxButton.h" /> <ClInclude Include="..\..\of_v0.11.2_vs2017_release\addons\ofxGui\src\ofxButton.h" />

View File

@ -1,9 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <ItemGroup>
<ClCompile Include="src\ofApp.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\main.cpp"> <ClCompile Include="src\main.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
@ -64,6 +61,12 @@
<ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvShortImage.cpp"> <ClCompile Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\src\ofxCvShortImage.cpp">
<Filter>addons\ofxOpenCv\src</Filter> <Filter>addons\ofxOpenCv\src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\ofApp.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\bpRecognizer.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="src"> <Filter Include="src">
@ -1142,6 +1145,9 @@
<ClInclude Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\libs\opencv\include\opencv2\world.hpp"> <ClInclude Include="..\..\of_v0.11.2_vs2017_release\addons\ofxOpenCv\libs\opencv\include\opencv2\world.hpp">
<Filter>addons\ofxOpenCv\libs\opencv\include\opencv2</Filter> <Filter>addons\ofxOpenCv\libs\opencv\include\opencv2</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\bpRecognizer.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="icon.rc" /> <ResourceCompile Include="icon.rc" />

View File

@ -0,0 +1,30 @@
#include "bpRecognizer.h"
void bpRecognizer::update(ofxCvGrayscaleImage image, int video_width, int video_height) {
//subtracted.absDiff(image, background);
subtracted.allocate(video_width, video_height);
for (int i = 0; i < video_width * video_height; i++)
{
subtracted.getPixels()[i] = image.getPixels()[i] > background.getPixels()[i] + threshhold ? 255 : 0;
}
binarized = subtracted;
//binarized.threshold(threshhold);
contourFinder.findContours(
binarized,
10,
video_height * video_width / 10,
10,
true);
}
void bpRecognizer::setThreshhold(int threshhold) {
this->threshhold = threshhold;
}
void bpRecognizer::setBackground(ofxCvGrayscaleImage image) {
this->background = image;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "ofxOpenCv.h"
class bpRecognizer
{
public:
void update(ofxCvGrayscaleImage image, int video_width, int video_height);
void setThreshhold(int threshhold);
void setBackground(ofxCvGrayscaleImage image);
ofxCvGrayscaleImage currentImage;
ofxCvGrayscaleImage background;
ofxCvGrayscaleImage subtracted;
ofxCvGrayscaleImage binarized;
ofxCvContourFinder contourFinder;
int threshhold;
};

View File

@ -1,11 +1,11 @@
#include "ofApp.h" #include "ofApp.h"
#define VIDEO_WIDTH 568 #define VIDEO_WIDTH 360
#define VIDEO_HEIGHT 320 #define VIDEO_HEIGHT 640
//-------------------------------------------------------------- //--------------------------------------------------------------
void ofApp::setup(){ void ofApp::setup(){
vidPlayer.load("beer.mkv"); // in /bin/data/ vidPlayer.load("video.mkv"); // in /bin/data/
vidPlayer.play(); vidPlayer.play();
vidPlayer.setLoopState(OF_LOOP_NORMAL); vidPlayer.setLoopState(OF_LOOP_NORMAL);
@ -23,25 +23,23 @@ void ofApp::update(){
vidPlayer.update(); vidPlayer.update();
colorVideo.setFromPixels(vidPlayer.getPixels()); colorVideo.setFromPixels(vidPlayer.getPixels());
grayVideo = colorVideo; // Convert Color to Grayscale grayVideo = colorVideo; // Convert Color to Grayscale
//cvInRange();
//colorVideo.convertToGrayscalePlanarImage(red, 0);
if (learnBackground){ if (learnBackground){
background = grayVideo; recognizer.setBackground(grayVideo);
learnBackground = false; learnBackground = false;
} }
// Background subtraction recognizer.setThreshhold(threshhold);
binarized.absDiff(background, grayVideo); recognizer.update(grayVideo, VIDEO_WIDTH, VIDEO_HEIGHT);
// Binarization
binarized.threshold(threshhold);
contourFinder.findContours( background = recognizer.background;
binarized, subtracted = recognizer.subtracted;
10, binarized = recognizer.binarized;
VIDEO_HEIGHT * VIDEO_WIDTH / 10, contourFinder = recognizer.contourFinder;
10,
true);
} }
//-------------------------------------------------------------- //--------------------------------------------------------------
@ -50,12 +48,20 @@ void ofApp::draw(){
ofSetHexColor(0xffffff); ofSetHexColor(0xffffff);
colorVideo.draw(20, 20); colorVideo.draw(20, 20);
grayVideo.draw(40 + VIDEO_WIDTH, 20); grayVideo.draw(40 + VIDEO_WIDTH, 20);
background.draw(20, 40 + VIDEO_HEIGHT); subtracted.draw(60 + 2 * VIDEO_WIDTH, 20);
binarized.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT); //binarized.draw(80 + 3 * VIDEO_WIDTH, 20);
//colorVideo.draw(20, 20);
//grayVideo.draw(40 + VIDEO_WIDTH, 20);
//background.draw(20, 40 + VIDEO_HEIGHT);
//binarized.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT);
//red.draw(40 + VIDEO_WIDTH, 20);
//green.draw(20, 40 + VIDEO_HEIGHT);
//blue.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT);
ofFill(); ofFill();
ofSetHexColor(0x333333); ofSetHexColor(0x333333);
ofDrawRectangle(40 + VIDEO_WIDTH, 60 + VIDEO_HEIGHT + VIDEO_HEIGHT, VIDEO_WIDTH, VIDEO_HEIGHT); ofDrawRectangle(80 + 3 * VIDEO_WIDTH, 20, VIDEO_WIDTH, VIDEO_HEIGHT);
ofSetHexColor(0xffffff); ofSetHexColor(0xffffff);
for (int i = 0; i < contourFinder.blobs.size(); i++) { for (int i = 0; i < contourFinder.blobs.size(); i++) {
@ -75,10 +81,10 @@ void ofApp::draw(){
if (aspect_ratio > 0.5 && aspect_ratio < 1.5) if (aspect_ratio > 0.5 && aspect_ratio < 1.5)
{ {
blob.draw(40 + VIDEO_WIDTH, 60 + VIDEO_HEIGHT + VIDEO_HEIGHT); blob.draw(80 + 3 * VIDEO_WIDTH, 20);
ofDrawBitmapString(sizeStr.str(), ofDrawBitmapString(sizeStr.str(),
contourFinder.blobs[i].boundingRect.getCenter().x + 40 + VIDEO_WIDTH, contourFinder.blobs[i].boundingRect.getCenter().x + 80 + 3 * VIDEO_WIDTH,
contourFinder.blobs[i].boundingRect.getCenter().y + 60 + VIDEO_HEIGHT + VIDEO_HEIGHT); contourFinder.blobs[i].boundingRect.getCenter().y + 20);
} }
} }

View File

@ -3,6 +3,7 @@
#include "ofMain.h" #include "ofMain.h"
#include "ofxOpenCv.h" #include "ofxOpenCv.h"
#include "ofxGui.h" #include "ofxGui.h"
#include "bpRecognizer.h"
class ofApp : public ofBaseApp{ class ofApp : public ofBaseApp{
@ -28,8 +29,10 @@ class ofApp : public ofBaseApp{
// Images displaying original video // Images displaying original video
ofxCvColorImage colorVideo; ofxCvColorImage colorVideo;
ofxCvGrayscaleImage grayVideo; ofxCvGrayscaleImage grayVideo;
bpRecognizer recognizer;
ofxCvGrayscaleImage background; ofxCvGrayscaleImage background;
ofxCvGrayscaleImage subtracted;
ofxCvGrayscaleImage binarized; ofxCvGrayscaleImage binarized;
ofxCvContourFinder contourFinder; ofxCvContourFinder contourFinder;
@ -39,4 +42,8 @@ class ofApp : public ofBaseApp{
ofxPanel gui; ofxPanel gui;
ofParameter<int> threshhold; ofParameter<int> threshhold;
ofxCvGrayscaleImage red;
ofxCvGrayscaleImage green;
ofxCvGrayscaleImage blue;
}; };