2011年3月28日 星期一

Introduction to cvBlobslib Library


前言

在影像處理中,為了找尋目標或是物件(Objects)Labeling 是最常用的方法,藉此得知目前影像中物件的數目、大小、位置..等等重要的參數。

在早期 Labeling的程式往往都需要自行撰寫,不管是用Recursive或者是二元樹搜尋法的方式來實現4-neighbor(8-neighbor),都是相當累人的事情


。OpenCV雖然有提供找尋物件軌跡的Functions以及範例程式,但是並沒有直接提供LabelingFunction,直到後來由第三方開發的cvBlobslib 的函式庫(Library)


//<----------------------------------------------------------->//


以下資料為官方文件的翻譯 及 作者的使用心得
<http://opencv.willowgarage.com/wiki/cvBlobsLib:>



Blob extraction library
Blob 函式庫是用於二值化影像,相連物件的Labeling,這相當於Matlab regionprops() 函式。Blob 函式庫也提供影像處理的函式,如Filter以及由擷取物件得到預知的結果或特性。(ex. Labeling 後得知有多少個物件)

Features
Blob函式庫提供兩個主要的功能:

1.      擷取二值或灰階圖片中相連(8-connected)物件(Blobs)

2.      對擷取到的BlobsFilter 的動作,用來獲得在影像中你想要求得的的物件。由Blob 函式庫中的CBlobResult,執行使用Filter 的方法。

How to use the cvBloblib
1.      下載 cvblobslib_OpenCV_v8_3 的壓縮檔(http://opencv.willowgarage.com/wiki/cvBlobsLib#Codeexamples)
2.      解壓縮之後打開 VC++6 WorkSpace,如下圖所示:


按下Ctrl+F5 ,之後會發現原來放置cvBlobslib的資料夾會多出debug的資料夾,debug資料夾內,有檔案cvblobslib.lib,這是函式庫的檔案
Note: 
cvblobslib.lib 會隨著你的IDE 不同而有所不同,比如說你在某台電腦用VS2008 Complier後得到cvblobslib.lib,如果你將此.lib檔拿到VS2005使用將會出錯( mfc90.dll ) ,在此建議,如果換新的環境,最保險的方法就是重新Complier一次。



 然後在VC++2005(2008) IDEOptions->Projects and Solutions ->VC++Directories
Show directories for 欄位的Library files,新增之前的cvBlobslib.lib 
 Project -> Properties ->Configuration Properties -> Link -> Input 添加 cvblobslib.lib





 cvblobslib_OpenCV_v8_3裡的Headed file(紅框內所示) 複製並且貼於程式的專案資料夾中然後在主程式 寫上 #include "BlobResult.h"





Example of cvBloblib

#include <stdio.h>

#include <stdlib.h>
#include <cv.h>                 //OpenCV Library
#include <highgui.h>
#include "BlobResult.h"  // cvBlobslib Library
//-------------------------------------------------------------------------------//
int main(void)
{
         IplImage * frame;   
            IplImage  *blobs_img ;
            CBlobResult blobs;
            CBlob *currentBlob;

//* Create Window //*/
           cvNamedWindow("Blob",0);
           cvResizeWindow("Blob",400,300);
           cvMoveWindow("Blob",0,0);
//----------------------------------------------------------------------//


//* LoadImage //*/

           frame=cvLoadImage("Test1.bmp",0);
           CvSize Size = cvGetSize(frame);
           blobs_img = cvCreateImage(Size,IPL_DEPTH_8U,3);
           blobs = CBlobResult(frame, NULL,0);

           blobs.Filter( blobs, B_EXCLUDE,  
                                CBlobGetArea(),B_LESS,5,1
                              );

           for (int i = 0;  i < blobs.GetNumBlobs(); i++ )
               {    currentBlob = blobs.GetBlob(i);
                     currentBlob->FillBlob( blobs_img,      
                     CV_RGB(255,0,0));
               }
           cvShowImage("Blob",blobs_img);
//* Show Properties //*/
     printf("Numer of Objects : %d\n",blobs.GetNumBlobs());
     cvWaitKey(0);
     cvReleaseImage(&frame);
     cvDestroyWindow("Blob");
}

//--------------------------------------------------------------------------------------------//
Option :Pseudo-Steps of a Blob Detection Algorithm
  • Image Filtering: Filter your image using cvBlur or cvSmooth. Use Gaussian filter.
  • Image Segmentation: Segment your image with the color your want.
  • Detect blobs: From the segmented image, connect all 4/8-connected image pixels. For every pixel, check the value of its pixel at its North, East, West and South. If it is also segmented include it in your blob. In this way, all disjointed patches can be detected as blobs.
  • Filter blobs: Use some filter like area/roundness/compact or the blob detected to get the blob you are looking for.
  • Track blobs: If you are detecting blobs in video, try to track blobs from one frame to the next. This will ensure robustness. A simple approach is to detect mean of the current blob and in the next image search for the mean near the current mean.
  • For your robots, you can fix up to 2/3 round colorful circles on your robot and try to track them. Pick colors that you can easily filter. You can also use OpenCv's circle detection algorithm for the Filter blobs stage.
//--------------------------------------------------------------------------------------------//




沒有留言:

張貼留言