Meanshift object tracking with CUDA

Object tracking is all about locating objects in a video sequence or in real time(real time tracking) and following the movement of the object.

In this project, I will present you the meanshift tracking method and its improvement camshift with the CUDA acceleration.


1. Meanshift and Camshift

Briefly speaking, the classical meanshift method contains three steps:

  1. calculate meanshift vector
  2. move the density estimation window
  3. repeat the above process until it converge

As you can see that the whole process is about finding the densest center. I will not go through the mathematical detail behind, you can find a good reference here: Introducation to Mean shift.

Follow this method, I built my first version of object tracking(without CUDA):


It looks just OK, but I had found a big problem that is my program lost his target very easily. I found that the problem is deal to the fixed searching window size, which means the searching window was big so that takes some background into calculation:


Or it was too small that just contains a subset of object:


The tracking is not constant in both case because we didn't really taking the density of object. And this caused another problem which is the we can't change the depth level. If we move the object forwards or backwards to the camera, the changing object size will not fit the fixed searching window.

By solving this problem, I get to camshift (continuous adaptive meanshift) which is an improvement of meanshift introduced by Gary Bradski in 1998.

Briefly speaking, camshift applied meanshift first. Once the meanshift converged, it recalculate the size of searching window. And again it starts the meanshift with the new window and previous location. This process ends until it meets certain precision.

By recalculating the window size in each iteration, we could find the best fit searching window for target object. This insured that the stable and constant tracking:

2. Parallel programming with CUDA

A typical CUDA program contains the following 4 steps:

  1. CPU allocates storage on GPU
  2. CPU copies input data to GPU
  3. CPU launches kernels on GPU to process the data
  4. CPU copies results back form GPU to CPU

The principle of writing a CUDA program is firstly write your program as if it runs on one single thread and than the GPU will run that program on many threads.

But in case of parallelism, we have to face some unusual problems in procedural programming. The first problem is how the threads are communicating with each other through memory.

Parallel communication models

The simplest model is call "map", which means that tasks read from and write to specific data elements:


The second model is "gather" because the threads gather many input data:


And we also have "scatter" where the task computes where to write output:


Next is "stencil" where tasks read input from a fixed neighborhood in an array:


And finally we have "transpose" that re-order the data element:


Let's make a little conclusion:

  • map: one-to-one
  • gather: many-to-one
  • scatter: one-to-many
  • stencil: several-to-one
  • transpose: one-to-one

Memory model

CUDA has a 3 level memory model. A thread has it's own local memory. A block of threads has a shared memory which could be access by all threads inside the block. And the whole kernel has a global memory that is accessible for all contained threads.

Synchronization

As we define the block which contains many threads to compute part of the problem, we are facing the synchronization problem. In certain point of the program, threads should wait until others reached.

A simple technique CUDA applied is barrier, which the point that threads should stop ant wait. (syncthreads)

We've go through all important aspect of prallel programming, let's look at our Camshfit acceleration.

3. Camshift acceleration with CUDA

So the firstly thing we have to do, is to analyse which part of the program could be parallel. The process of Camshift is following:

  1. construct histogram
  2. back projection to obtain the color distribution
  3. meanshift util it converges
  4. recalculate the window size for next frame
  5. continue the process until no more frame

The histogram could be built simultaneously. Instead of iterate pixel by pixel, we could just launch many threads to compute the bin in the same time.

__global__ void rgb_to_xyY(
   float* d_r,
   float* d_g,
   float* d_b,
   float* d_x,
   float* d_y,
   float* d_log_Y,
   float  delta,
   int    num_pixels_y,
   int    num_pixels_x )
{
 int  ny             = num_pixels_y;
 int  nx             = num_pixels_x;
 int2 image_index_2d = make_int2( ( blockIdx.x * blockDim.x ) + threadIdx.x, ( blockIdx.y * blockDim.y ) + threadIdx.y );
 int  image_index_1d = ( nx * image_index_2d.y ) + image_index_2d.x;

 if ( image_index_2d.x < nx && image_index_2d.y < ny )
 {
   float r = d_r[ image_index_1d ];
   float g = d_g[ image_index_1d ];
   float b = d_b[ image_index_1d ];

   float X = ( r * 0.4124f ) + ( g * 0.3576f ) + ( b * 0.1805f );
   float Y = ( r * 0.2126f ) + ( g * 0.7152f ) + ( b * 0.0722f );
   float Z = ( r * 0.0193f ) + ( g * 0.1192f ) + ( b * 0.9505f );

   float L = X + Y + Z;
   float x = X / L;
   float y = Y / L;

   float log_Y = log10f( delta + Y );

   d_x[ image_index_1d ]     = x;
   d_y[ image_index_1d ]     = y;
   d_log_Y[ image_index_1d ] = log_Y;
 }
}

The moment computing process is also parallelism, but in my project I didn't get this right. Maybe I will get this done later.

So let's look at the result:


4. Conclusion

In this project, I applied Camshift object tracking method with (part of) CUDA acceleration. The result is convincing, but there are still problems for future work:

  1. The color space is not a robust feature to extract the object. The program could get lost with similar background color.
  2. The parallel programming is complex, so I didn't success all the aspect in this program.
  3. Deal to time limit, an performance evaluation is not held in this project.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容

  • 我的体重已经飙升到51公斤啦!这是一件多么可怕的事情啊,我要减肥,我要把身体上的没有用的大肥肉减掉,我不喜欢他们待...
    yyylllyyy阅读 253评论 0 0
  • 我想大多数的父母很羡慕学霸的吧?人家的孩子怎么就是学霸呢?人家大概是遗传或者是天赋吧!其实,学霸不是天赋,是基...
    徐心老师阅读 490评论 0 1
  • 已然二十有余,碌碌无为,频频思之,辗转反侧,难以入眠。 至今,难懂四书五经,不曾阅览四大名著,如是乎平平淡淡。 心...
    OnlyMorrison阅读 250评论 0 1
  • 【背景】 雨总:我明天要陪老板去和客户看一个学习的项目。这个客户是省某局一个领导的秘书,以前在机关幼儿园做出了很好...
    销帮夜话阅读 365评论 0 0