Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,72 @@ Mat CalculateBackgroundMedian (Capture* capture, float startTime, float endTime,
}


Mat CalculateBackgroundMax (Capture* capture, float startTime, float endTime, unsigned int framesCount)
{
int width = capture->width;
int height = capture->height;
Mat background = Mat::zeros(height, width, CV_8UC3);

// do not calc bg for an image...
if (capture->type == Capture::IMAGE) return background;

// if endtime is very small, consider that we want to use the entire capture duration. In case
// this a webcam, give up calculation
if (endTime < 0.01)
{
if (capture->type == Capture::VIDEO || capture->type == Capture::MULTI_VIDEO)
{
endTime = ((double)capture->GetFrameCount()) / capture->fps;
}
else
return background;
}

cerr << "Calculating max background" << endl;

capture->Stop();
capture->Play();

unsigned int readCount = 0;

float intervalTime = (endTime - startTime) / framesCount;
while (readCount < framesCount)
{
capture->GetFrame (startTime + intervalTime * readCount);

double msgTime = capture->GetTime();
cerr << "using frame at time " << msgTime << endl;

if (capture->frame.empty())
break;

for (int y = 0; y < height; y++)
{
unsigned char* frameRow = capture->frame.ptr<unsigned char>(y);
unsigned char* bgRow = background.ptr<unsigned char>(y);

for (int x = 0; x < width; x++)
{
if(bgRow[x * 3] < frameRow[x * 3])
bgRow[x * 3] = frameRow[x * 3];
if(bgRow[x * 3 + 1] < frameRow[x * 3 + 1])
bgRow[x * 3 + 1] = frameRow[x * 3 + 1];
if(bgRow[x * 3 + 2] < frameRow[x * 3 + 2])
bgRow[x * 3 + 2] = frameRow[x * 3 + 2];
}
}
readCount++;
}

capture->Stop();

cerr << "Background calculated" << endl;

return background;
}




Mat CalculateBackgroundMean (Capture* capture, float startTime, float endTime, unsigned int framesCount)
{
Expand Down
2 changes: 1 addition & 1 deletion Background.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ using namespace cv;
#include <vector>
#include <iostream>


Mat CalculateBackgroundMax (Capture* capture, float startTime, float endTime, unsigned int framesCount);
Mat CalculateBackgroundMedian (Capture* capture, float startTime, float endTime, unsigned int framesCount);
Mat CalculateBackgroundMean (Capture* capture, float startTime, float endTime, unsigned int framesCount);
6 changes: 5 additions & 1 deletion ImageProcessingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ void ImageProcessingEngine::Reset(Parameters& parameters)
{
if (bgCalcType == BG_MEDIAN)
background = CalculateBackgroundMedian (capture, bgStartTime, bgEndTime, bgFrames);
else if (bgCalcType == BG_MAX)
background = CalculateBackgroundMax (capture, bgStartTime, bgEndTime, bgFrames);
else if (bgCalcType == BG_MEAN)
background = CalculateBackgroundMean (capture, bgStartTime, bgEndTime, bgFrames);
else
Expand Down Expand Up @@ -277,7 +279,7 @@ void ImageProcessingEngine::LoadXML(FileNode& fn)
string bt = (string)fn["BackgroundCalcType"];
if (bt == "median") bgCalcType = BG_MEDIAN;
else if (bt == "mean") bgCalcType = BG_MEAN;

else if (bt == "max") bgCalcType = BG_MAX;
zonesFilename = (string)fn["ZonesFilename"];
}
}
Expand All @@ -300,6 +302,8 @@ void ImageProcessingEngine::SaveXML(FileStorage& fs)
fs << "BackgroundCalcType" << "median";
else if (bgCalcType == BG_MEAN)
fs << "BackgroundCalcType" << "mean";
else if (bgCalcType == BG_MAX)
fs << "BackgroundCalcType" << "max";
fs << "ZonesFilename" << zonesFilename;
}

Expand Down
2 changes: 1 addition & 1 deletion ImageProcessingEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct ImageProcessingEngine


// background
enum BgCalcType {BG_MEAN, BG_MEDIAN};
enum BgCalcType {BG_MEAN, BG_MEDIAN, BG_MAX};
BgCalcType bgCalcType = BG_MEAN;
std::string bgFilename;
bool bgRecalculate = false;
Expand Down
11 changes: 8 additions & 3 deletions MainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,13 @@ MainFrame::MainFrame(wxWindow* parent,wxWindowID id)
FlexGridSizer5->Add(SpinCtrlBgFrames, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
StaticText5 = new wxStaticText(BackgroundTab, ID_STATICTEXT5, _("Method"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT5"));
FlexGridSizer5->Add(StaticText5, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
wxString __wxRadioBoxChoices_1[2] =
wxString __wxRadioBoxChoices_1[3] =
{
_("mean"),
_("median")
_("median"),
_("max")
};
RadioBoxMethod = new wxRadioBox(BackgroundTab, ID_RADIOBOX1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 2, __wxRadioBoxChoices_1, 2, 0, wxDefaultValidator, _T("ID_RADIOBOX1"));
RadioBoxMethod = new wxRadioBox(BackgroundTab, ID_RADIOBOX1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 3, __wxRadioBoxChoices_1, 2, 0, wxDefaultValidator, _T("ID_RADIOBOX1"));
RadioBoxMethod->SetSelection(0);
FlexGridSizer5->Add(RadioBoxMethod, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
StaticText4 = new wxStaticText(BackgroundTab, ID_STATICTEXT4, _("When loading"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
Expand Down Expand Up @@ -1299,6 +1300,8 @@ void MainFrame::OnbuttonBgRecalculateClick(wxCommandEvent& event)
Mat newBg;
if (ipEngine.bgCalcType == ImageProcessingEngine::BG_MEDIAN)
newBg = CalculateBackgroundMedian (ipEngine.capture, ipEngine.bgStartTime, ipEngine.bgEndTime, ipEngine.bgFrames);
else if (ipEngine.bgCalcType == ImageProcessingEngine::BG_MAX)
newBg = CalculateBackgroundMax (ipEngine.capture, ipEngine.bgStartTime, ipEngine.bgEndTime, ipEngine.bgFrames);
else if (ipEngine.bgCalcType == ImageProcessingEngine::BG_MEAN)
newBg = CalculateBackgroundMean (ipEngine.capture, ipEngine.bgStartTime, ipEngine.bgEndTime, ipEngine.bgFrames);
newBg.copyTo(ipEngine.background);
Expand Down Expand Up @@ -2220,6 +2223,7 @@ void MainFrame::OnRadioBoxMethodSelect(wxCommandEvent& event)
{
if (sel == "median") ipEngine.bgCalcType = ImageProcessingEngine::BG_MEDIAN;
if (sel == "mean") ipEngine.bgCalcType = ImageProcessingEngine::BG_MEAN;
if (sel == "max") ipEngine.bgCalcType = ImageProcessingEngine::BG_MAX;
}
}

Expand All @@ -2231,6 +2235,7 @@ void MainFrame::UpdateUI ()
CheckBoxRecalculate->SetValue(ipEngine.bgRecalculate);
if (ipEngine.bgCalcType == ImageProcessingEngine::BG_MEDIAN) RadioBoxMethod->SetStringSelection("median");
if (ipEngine.bgCalcType == ImageProcessingEngine::BG_MEAN) RadioBoxMethod->SetStringSelection("mean");
if (ipEngine.bgCalcType == ImageProcessingEngine::BG_MAX) RadioBoxMethod->SetStringSelection("max");

SpinCtrlStartTime->SetValue(ipEngine.startTime);
SpinCtrlDuration->SetValue(ipEngine.durationTime);
Expand Down
4 changes: 3 additions & 1 deletion wxsmith/Mainframe.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@
<content>
<item>mean</item>
<item>median</item>
<item>max</item>
</content>
<dimension>2</dimension>
<default>1</default>
<dimension>3</dimension>
<handler function="OnRadioBoxMethodSelect" entry="EVT_RADIOBOX" />
</object>
<flag>wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
Expand Down