Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>kim's algorithm supposedly has a cache layer, i did not dig deep enough into opencv implementation to know how to have a workable version that works with exposure problem. </p> <h3>notes</h3> <ul> <li>opencv book(which i havent read) should have the info you need if it's there at all</li> <li>this is only 1 channel and not in yuv although it can be extended</li> <li>needs more work on speed/testing (you can of course turn on optimization in compiler)</li> <li>background subtraction is a buzzy word. try "change detection"</li> </ul> <p>bgfg_cb.h</p> <pre><code>#ifndef __bgfg_cb_h__ #define __bgfg_cb_h__ //http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.148.9778&amp;rep=rep1&amp;type=pdf #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include &lt;iostream&gt; #include &lt;time.h&gt; using namespace cv; using namespace std; struct codeword { float min; float max; float f; float l; int first; int last; bool isStale; }; extern int alpha ; extern float beta ; extern int Tdel ,Tadd , Th; void initializeCodebook(int w,int h); void update_cb(Mat&amp; frame); void fg_cb(Mat&amp; frame,Mat&amp; fg); #endif </code></pre> <p>bgfg_cb.cpp</p> <pre><code>#include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include &lt;iostream&gt; #include &lt;time.h&gt; #include "bgfg_cb.h" using namespace cv; using namespace std; vector&lt;codeword&gt; **cbMain; vector&lt;codeword&gt; **cbCache; int t=0; int alpha = 10;//knob float beta =1; int Tdel = 200,Tadd = 150, Th= 200;//knobs void initializeCodebook(int w,int h) { cbMain = new vector&lt;codeword&gt;*[w]; for(int i = 0; i &lt; w; ++i) cbMain[i] = new vector&lt;codeword&gt;[h]; cbCache = new vector&lt;codeword&gt;*[w]; for(int i = 0; i &lt; w; ++i) cbCache[i] = new vector&lt;codeword&gt;[h]; } void update_cb(Mat&amp; frame) { if(t&gt;10) return; for(int i=0;i&lt;frame.rows;i++) { for(int j=0;j&lt;frame.cols;j++) { int pix = frame.at&lt;uchar&gt;(i,j); vector&lt;codeword&gt;&amp; cm =cbMain[i][j]; bool found = false; for(int k=0;k&lt;cm.size();k++) { if(cm[k].min&lt;=pix &amp;&amp; pix&lt;=cm[k].max &amp;&amp; !found) { found=true; cm[k].min = ((pix-alpha)+(cm[k].f*cm[k].min))/(cm[k].f+1); cm[k].max = ((pix+alpha)+(cm[k].f*cm[k].max))/(cm[k].f+1); cm[k].l=0; cm[k].last=t; cm[k].f++; }else { cm[k].l++; } } if(!found) { codeword n={}; n.min=max(0,pix-alpha); n.max=min(255,pix+alpha); n.f=1; n.l=0; n.first=t; n.last=t; cm.push_back(n); } } } t++; } void fg_cb(Mat&amp; frame,Mat&amp; fg) { fg=Mat::zeros(frame.size(),CV_8UC1); if(cbMain==0) initializeCodebook(frame.rows,frame.cols); if(t&lt;10) { update_cb(frame); return; } for(int i=0;i&lt;frame.rows;i++) { for(int j=0;j&lt;frame.cols;j++) { int pix = frame.at&lt;uchar&gt;(i,j); vector&lt;codeword&gt;&amp; cm = cbMain[i][j]; bool found = false; for(int k=0;k&lt;cm.size();k++) { if(cm[k].min&lt;=pix &amp;&amp; pix&lt;=cm[k].max &amp;&amp; !found) { cm[k].min = ((1-beta)*(pix-alpha)) + (beta*cm[k].min); cm[k].max = ((1-beta)*(pix+alpha)) + (beta*cm[k].max); cm[k].l=0; cm[k].first=t; cm[k].f++; found=true; }else { cm[k].l++; } } cm.erase( remove_if(cm.begin(), cm.end(), [](codeword&amp; c) { return c.l&gt;=Tdel;} ), cm.end() ); fg.at&lt;uchar&gt;(i,j) = found?0:255; if(found) continue; found = false; vector&lt;codeword&gt;&amp; cc = cbCache[i][j]; for(int k=0;k&lt;cc.size();k++) { if(cc[k].min&lt;=pix &amp;&amp; pix&lt;=cc[k].max &amp;&amp; !found) { cc[k].min = ((1-beta)*(pix-alpha)) + (beta*cc[k].min); cc[k].max = ((1-beta)*(pix+alpha)) + (beta*cc[k].max); cc[k].l=0; cc[k].first=t; cc[k].f++; found=true; }else { cc[k].l++; } } if(!found) { codeword n={}; n.min=max(0,pix-alpha); n.max=min(255,pix+alpha); n.f=1; n.l=0; n.first=t; n.last=t; cc.push_back(n); } cc.erase( remove_if(cc.begin(), cc.end(), [](codeword&amp; c) { return c.l&gt;=Th;} ), cc.end() ); for(vector&lt;codeword&gt;::iterator it=cc.begin();it!=cc.end();it++) { if(it-&gt;f&gt;Tadd) { cm.push_back(*it); } } cc.erase( remove_if(cc.begin(), cc.end(), [](codeword&amp; c) { return c.f&gt;Tadd;} ), cc.end() ); } } } </code></pre> <p>main.cpp</p> <pre><code>#include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "bgfg_cb.h" #include &lt;iostream&gt; using namespace cv; using namespace std; void proc() { Mat frame,fg,gray; VideoCapture cap("C:/Downloads/S2_L1.tar/S2_L1/Crowd_PETS09/S2/L1/Time_12-34/View_001/frame_%04d.jpg"); cap &gt;&gt; frame; initializeCodebook(frame.rows,frame.cols); for(;;) { cap&gt;&gt;frame; cvtColor(frame,gray,CV_BGR2GRAY); fg_cb(gray,fg); Mat cc; imshow("fg",fg); waitKey(1); } } int main(int argc, char ** argv) { proc(); cin.ignore(1); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload