SimpleBlob 分析OpenCV 實現


Blob在機器視覺中是指圖像中的具有相似顏色、紋理等特征所組成的一塊連通區域。Blob分析就是對這一塊連通區域進行幾何分析得到一些重要的幾何特征,例如:區域的面積、中心點坐標、質心坐標、最小外接矩形、主軸等。

Blob分析的一般步驟:

1、圖像分割
  • 分離出前景和背景,例如閾值化操作
2、連通性分析
  • 根據目標的連通性對目標區域 進行標記,或者叫拓撲性分析。
3、特征量計算
  • 描述了區域的幾何特征,這些 幾何特征不依賴與灰度值。

經典目標檢測分離方法

1、背景差分法

在檢測運動目標時,如果背景是靜止的,利用當前圖像與預存的背景圖像作差分,再利用閾值來檢測運動區域的一種動態目標識別技術。 背景差分算法適用於背景已知的情況,但難點是如何自動獲得長久的靜態背景模型。 matlab中單純的背景差分直接是函數imabsdiff(X,Y)就可以。

2、幀差分法

利用視頻序列中連續的兩幀或幾幀圖像的差來進行目標檢測和提取。在運動的檢測過程中,該方法利用時間信息,通過比較圖像中若干連續幀獲得對應像素點的灰度差值,如果均大於一定的閾值T2,則可以判斷該位置存在運動的目標。 較適合於動態變化場景。

3、光流場法

利用相鄰兩幀中對應像素的灰度保持原理來評估二維圖像的變化。能夠較好的從背景中檢測到相關前景目標,甚至是運動屋里中的部分運動目標,適用於攝像機運動過程中相對運動目標的檢測。 開口問題、光流場約束方程的解的不唯一性問題。不能正確的表示實際的運動場。

SimpleBlob 分析

1、OpenCV源碼文檔

 

 1 @brief Class for extracting blobs from an image. :
 2 The class implements a simple algorithm for extracting blobs from an image:
 3  4 1.  Convert the source image to binary images by applying thresholding with several thresholds from
 5     minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
 6     neighboring thresholds.
 7 2.  Extract connected components from every binary image by findContours and calculate their
 8     centers.
 9 3.  Group centers from several binary images by their coordinates. Close centers form one group that
10     corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
11 4.  From the groups, estimate final centers of blobs and their radiuses and return as locations and
12     sizes of keypoints.
13 14 This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
15 to turn on/off corresponding filtration. Available filtrations:
16 17 -   **By color**. This filter compares the intensity of a binary image at the center of a blob to
18 blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
19 and blobColor = 255 to extract light blobs.
20 -   **By area**. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
21 -   **By circularity**. Extracted blobs have circularity
22 (\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) between minCircularity (inclusive) and
23 maxCircularity (exclusive).
24 -   **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio
25 between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
26 -   **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between
27 minConvexity (inclusive) and maxConvexity (exclusive).
28 29 Default values of parameters are tuned to extract dark circular blobs.

 

 

 

2、SimpleBlob:Params 配置
 1 class CV_EXPORTS_W SimpleBlobDetector : public Feature2D
 2 {
 3 public:
 4   struct CV_EXPORTS_W_SIMPLE Params
 5   {
 6       CV_WRAP Params();
 7       CV_PROP_RW float thresholdStep;
 8       CV_PROP_RW float minThreshold;
 9       CV_PROP_RW float maxThreshold;
10       CV_PROP_RW size_t minRepeatability;
11       CV_PROP_RW float minDistBetweenBlobs;
12 13       CV_PROP_RW bool filterByColor;
14       CV_PROP_RW uchar blobColor;
15 16       CV_PROP_RW bool filterByArea;
17       CV_PROP_RW float minArea, maxArea;
18 19       CV_PROP_RW bool filterByCircularity;
20       CV_PROP_RW float minCircularity, maxCircularity;
21 22       CV_PROP_RW bool filterByInertia;
23       CV_PROP_RW float minInertiaRatio, maxInertiaRatio;
24 25       CV_PROP_RW bool filterByConvexity;
26       CV_PROP_RW float minConvexity, maxConvexity;
27 28       void read( const FileNode& fn );
29       void write( FileStorage& fs ) const;
30   };

 

3、C++ 實例

 

 1 #include <opencv2/highgui.hpp>
 2 #include <opencv2/calib3d.hpp>
 3 #include <iostream>
 4  5 using namespace std;
 6 using namespace cv;
 7  8 int main() {
 9 Mat img = imread("D://Images/cut/2.jpg", 0);
10 11 /*
12 SimpleBlobDetector::Params params;
13 //閾值控制
14 params.minThreshold = 10;
15 params.maxThreshold = 200;
16 //像素面積大小控制
17 params.filterByArea = true;
18 params.minArea = 1000;
19 //形狀(凸)
20 params.filterByCircularity = false;
21 params.minCircularity = 0.7;
22 //形狀(凹)
23 params.filterByConvexity = true;
24 params.minConvexity = 0.9;
25 //形狀(圓)
26 params.filterByInertia = false;
27 params.minInertiaRatio = 0.5;
28 */
29 30 Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create();
31 vector<KeyPoint> keypoints;
32 detector->detect(img, keypoints);
33 Mat img_with_keypoints;
34 drawKeypoints(img, keypoints, img_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
35 imshow("keypoints", img_with_keypoints);
36 waitKey(0);
37 return 0;
38 }

 

 

 

4、Java 實例
 
        
 1 Mat dst=new Mat();
 2 //取單通道圖像
 3 dst==Imgcodecs.imread("blob.jpg",0);
 4 //存儲結果信息MatofKeyPoint 包含7個通道,分別對應7項信息
 5 /**
 6 buff[_channels*i+0] = (float) kp.pt.x; 
 7         buff[_channels*i+1] = (float) kp.pt.y;
 8         buff[_channels*i+2] = kp.size;
 9         buff[_channels*i+3] = kp.angle;
10         buff[_channels*i+4] = kp.response;
11         buff[_channels*i+5] = kp.octave;
12         buff[_channels*i+6] = kp.class_id;
13         **/
14 MatOfKeyPoint keypoints = new MatOfKeyPoint();
15 Mat keyMat = new Mat();
16 //FeatureDetector 第九項檢測為SimpleBolb 分析
17 /**
18  FAST = 1,
19          STAR = 2,
20          SIFT = 3,
21             SURF = 4,
22             ORB = 5,
23             MSER = 6,
24             GFTT = 7,
25             HARRIS = 8,
26             SIMPLEBLOB = 9,
27             DENSE = 10,
28             BRISK = 11,
29             AKAZE = 12,
30             GRID_FAST = GRIDDETECTOR + FAST,
31             GRID_STAR = GRIDDETECTOR + STAR,
32             GRID_SIFT = GRIDDETECTOR + SIFT,
33             GRID_SURF = GRIDDETECTOR + SURF,
34             GRID_ORB = GRIDDETECTOR + ORB,
35             GRID_MSER = GRIDDETECTOR + MSER,
36             GRID_GFTT = GRIDDETECTOR + GFTT,
37             GRID_HARRIS = GRIDDETECTOR + HARRIS,
38             GRID_SIMPLEBLOB = GRIDDETECTOR + SIMPLEBLOB,
39             GRID_DENSE = GRIDDETECTOR + DENSE,
40             GRID_BRISK = GRIDDETECTOR + BRISK,
41             GRID_AKAZE = GRIDDETECTOR + AKAZE,
42             PYRAMID_FAST = PYRAMIDDETECTOR + FAST,
43             PYRAMID_STAR = PYRAMIDDETECTOR + STAR,
44             PYRAMID_SIFT = PYRAMIDDETECTOR + SIFT,
45             PYRAMID_SURF = PYRAMIDDETECTOR + SURF,
46             PYRAMID_ORB = PYRAMIDDETECTOR + ORB,
47             PYRAMID_MSER = PYRAMIDDETECTOR + MSER,
48             PYRAMID_GFTT = PYRAMIDDETECTOR + GFTT,
49             PYRAMID_HARRIS = PYRAMIDDETECTOR + HARRIS,
50             PYRAMID_SIMPLEBLOB = PYRAMIDDETECTOR + SIMPLEBLOB,
51             PYRAMID_DENSE = PYRAMIDDETECTOR + DENSE,
52             PYRAMID_BRISK = PYRAMIDDETECTOR + BRISK,
53             PYRAMID_AKAZE = PYRAMIDDETECTOR + AKAZE,
54             DYNAMIC_FAST = DYNAMICDETECTOR + FAST,
55             DYNAMIC_STAR = DYNAMICDETECTOR + STAR,
56             DYNAMIC_SIFT = DYNAMICDETECTOR + SIFT,
57             DYNAMIC_SURF = DYNAMICDETECTOR + SURF,
58             DYNAMIC_ORB = DYNAMICDETECTOR + ORB,
59             DYNAMIC_MSER = DYNAMICDETECTOR + MSER,
60             DYNAMIC_GFTT = DYNAMICDETECTOR + GFTT,
61             DYNAMIC_HARRIS = DYNAMICDETECTOR + HARRIS,
62             DYNAMIC_SIMPLEBLOB = DYNAMICDETECTOR + SIMPLEBLOB,
63             DYNAMIC_DENSE = DYNAMICDETECTOR + DENSE,
64             DYNAMIC_BRISK = DYNAMICDETECTOR + BRISK,
65             DYNAMIC_AKAZE = DYNAMICDETECTOR + AKAZE;
66 */
67 FeatureDetector featureDetector = FeatureDetector.create(9);
68 //配置為xml文件,可以先生成默認xml文件,再進行修改數值
69 featureDetector.read("params.xml");
70 // 修改測試
71 featureDetector.detect(dst, keypoints);
 
        

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM