From ebf33ce21306ee6a545b35c906d817352008ac22 Mon Sep 17 00:00:00 2001 From: pernafrost Date: Thu, 31 Mar 2016 17:17:27 +0200 Subject: [PATCH 1/2] Added files via upload --- Background.cpp | 66 +++++++++++++++++++++++++++++++++++++++ Background.h | 2 +- ImageProcessingEngine.cpp | 6 +++- ImageProcessingEngine.h | 2 +- MainFrame.cpp | 11 +++++-- 5 files changed, 81 insertions(+), 6 deletions(-) diff --git a/Background.cpp b/Background.cpp index fd37605..5c8600b 100644 --- a/Background.cpp +++ b/Background.cpp @@ -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(y); + unsigned char* bgRow = background.ptr(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) { diff --git a/Background.h b/Background.h index 098fc60..40b42a7 100644 --- a/Background.h +++ b/Background.h @@ -8,6 +8,6 @@ using namespace cv; #include #include - +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); diff --git a/ImageProcessingEngine.cpp b/ImageProcessingEngine.cpp index 91f019d..ab0e9a9 100644 --- a/ImageProcessingEngine.cpp +++ b/ImageProcessingEngine.cpp @@ -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 @@ -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"]; } } @@ -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; } diff --git a/ImageProcessingEngine.h b/ImageProcessingEngine.h index 735b7ad..61e8127 100644 --- a/ImageProcessingEngine.h +++ b/ImageProcessingEngine.h @@ -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; diff --git a/MainFrame.cpp b/MainFrame.cpp index 81d8323..7102f6e 100644 --- a/MainFrame.cpp +++ b/MainFrame.cpp @@ -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")); @@ -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); @@ -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; } } @@ -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); From 220ef0b343e55e63c14ea2894b21b1ac9c743ea3 Mon Sep 17 00:00:00 2001 From: pernafrost Date: Thu, 31 Mar 2016 17:18:11 +0200 Subject: [PATCH 2/2] Added files via upload --- wxsmith/Mainframe.wxs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wxsmith/Mainframe.wxs b/wxsmith/Mainframe.wxs index 43941da..9daaac9 100644 --- a/wxsmith/Mainframe.wxs +++ b/wxsmith/Mainframe.wxs @@ -256,8 +256,10 @@ mean median + max - 2 + 1 + 3 wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL