SimpleBlob分析OpenCV实现

Blob在机器视觉中是指图像中的具有相似颜色、纹理等特征所组成的一块连通区域。Blob分析就是对这一块连通区域进行几何分析得到一些重要的几何特征,例如:区域的面积、中心点坐标、质心坐标、最小外接矩形、主轴等。

Blob分析的一般步骤:

1、图像分割
  • 分离出前景和背景,例如阈值化操作
2、连通性分析
  • 根据目标的连通性对目标区域 进行标记,或者叫拓扑性分析。
3、特征量计算
  • 描述了区域的几何特征,这些 几何特征不依赖与灰度值。

经典目标检测分离方法

1、背景差分法

在检测运动目标时,如果背景是静止的,利用当前图像与预存的背景图像作差分,再利用阈值来检测运动区域的一种动态目标识别技术。 背景差分算法适用于背景已知的情况,但难点是如何自动获得长久的静态背景模型。 matlab中单纯的背景差分直接是函数imabsdiff(X,Y)就可以。

2、帧差分法

利用视频序列中连续的两帧或几帧图像的差来进行目标检测和提取。在运动的检测过程中,该方法利用时间信息,通过比较图像中若干连续帧获得对应像素点的灰度差值,如果均大于一定的阈值T2,则可以判断该位置存在运动的目标。
较适合于动态变化场景。

3、光流场法

利用相邻两帧中对应像素的灰度保持原理来评估二维图像的变化。能够较好的从背景中检测到相关前景目标,甚至是运动屋里中的部分运动目标,适用于摄像机运动过程中相对运动目标的检测。
开口问题、光流场约束方程的解的不唯一性问题。不能正确的表示实际的运动场。

SimpleBlob 分析

1、OpenCV源码文档
@brief Class for extracting blobs from an image. :
The class implements a simple algorithm for extracting blobs from an image:

1.  Convert the source image to binary images by applying thresholding with several thresholds from
    minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
    neighboring thresholds.
2.  Extract connected components from every binary image by findContours and calculate their
    centers.
3.  Group centers from several binary images by their coordinates. Close centers form one group that
    corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
4.  From the groups, estimate final centers of blobs and their radiuses and return as locations and
    sizes of keypoints.

This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
to turn on/off corresponding filtration. Available filtrations:

-   **By color**. This filter compares the intensity of a binary image at the center of a blob to
blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
and blobColor = 255 to extract light blobs.
-   **By area**. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
-   **By circularity**. Extracted blobs have circularity
(\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) between minCircularity (inclusive) and
maxCircularity (exclusive).
-   **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio
between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
-   **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between
minConvexity (inclusive) and maxConvexity (exclusive).

Default values of parameters are tuned to extract dark circular blobs.
2、SimpleBlob:Params 配置
class CV_EXPORTS_W SimpleBlobDetector : public Feature2D
{
public:
  struct CV_EXPORTS_W_SIMPLE Params
  {
      CV_WRAP Params();
      CV_PROP_RW float thresholdStep;
      CV_PROP_RW float minThreshold;
      CV_PROP_RW float maxThreshold;
      CV_PROP_RW size_t minRepeatability;
      CV_PROP_RW float minDistBetweenBlobs;

      CV_PROP_RW bool filterByColor;
      CV_PROP_RW uchar blobColor;

      CV_PROP_RW bool filterByArea;
      CV_PROP_RW float minArea, maxArea;

      CV_PROP_RW bool filterByCircularity;
      CV_PROP_RW float minCircularity, maxCircularity;

      CV_PROP_RW bool filterByInertia;
      CV_PROP_RW float minInertiaRatio, maxInertiaRatio;

      CV_PROP_RW bool filterByConvexity;
      CV_PROP_RW float minConvexity, maxConvexity;

      void read( const FileNode& fn );
      void write( FileStorage& fs ) const;
  };
3、C++ 实例
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main() {
	Mat img = imread("D://Images/cut/2.jpg", 0);

	/*
	SimpleBlobDetector::Params params;
	//阈值控制
	params.minThreshold = 10;
	params.maxThreshold = 200;
	//像素面积大小控制
	params.filterByArea = true;
	params.minArea = 1000;
	//形状(圆)
	params.filterByCircularity = false;
	params.minCircularity = 0.7;
	//形状(凸)
	params.filterByConvexity = true;
	params.minConvexity = 0.9;
	//形状(惯量)
	params.filterByInertia = false;
	params.minInertiaRatio = 0.5;
	*/

	Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create();
	vector<KeyPoint> keypoints;
	detector->detect(img, keypoints);
	Mat img_with_keypoints;
	drawKeypoints(img, keypoints, img_with_keypoints, Scalar(0, 0, 255), 		DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	imshow("keypoints", img_with_keypoints);
	waitKey(0);
	return 0;
}

4、Java 实例
		Mat dst=new Mat();
		//取单通道图像
		dst==Imgcodecs.imread("blob.jpg",0);	
		//存储结果信息MatofKeyPoint 包含7个通道,分别对应7项信息
		/**
		buff[_channels*i+0] = (float) kp.pt.x; 
        buff[_channels*i+1] = (float) kp.pt.y;
        buff[_channels*i+2] = kp.size;
        buff[_channels*i+3] = kp.angle;
        buff[_channels*i+4] = kp.response;
        buff[_channels*i+5] = kp.octave;
        buff[_channels*i+6] = kp.class_id;
        **/
		MatOfKeyPoint keypoints = new MatOfKeyPoint();
		Mat keyMat = new Mat();
		//FeatureDetector 第九项检测为SimpleBolb 分析
		/**
		 	FAST = 1,
         	STAR = 2,
         	SIFT = 3,
            SURF = 4,
            ORB = 5,
            MSER = 6,
            GFTT = 7,
            HARRIS = 8,
            SIMPLEBLOB = 9,
            DENSE = 10,
            BRISK = 11,
            AKAZE = 12,
            GRID_FAST = GRIDDETECTOR + FAST,
            GRID_STAR = GRIDDETECTOR + STAR,
            GRID_SIFT = GRIDDETECTOR + SIFT,
            GRID_SURF = GRIDDETECTOR + SURF,
            GRID_ORB = GRIDDETECTOR + ORB,
            GRID_MSER = GRIDDETECTOR + MSER,
            GRID_GFTT = GRIDDETECTOR + GFTT,
            GRID_HARRIS = GRIDDETECTOR + HARRIS,
            GRID_SIMPLEBLOB = GRIDDETECTOR + SIMPLEBLOB,
            GRID_DENSE = GRIDDETECTOR + DENSE,
            GRID_BRISK = GRIDDETECTOR + BRISK,
            GRID_AKAZE = GRIDDETECTOR + AKAZE,
            PYRAMID_FAST = PYRAMIDDETECTOR + FAST,
            PYRAMID_STAR = PYRAMIDDETECTOR + STAR,
            PYRAMID_SIFT = PYRAMIDDETECTOR + SIFT,
            PYRAMID_SURF = PYRAMIDDETECTOR + SURF,
            PYRAMID_ORB = PYRAMIDDETECTOR + ORB,
            PYRAMID_MSER = PYRAMIDDETECTOR + MSER,
            PYRAMID_GFTT = PYRAMIDDETECTOR + GFTT,
            PYRAMID_HARRIS = PYRAMIDDETECTOR + HARRIS,
            PYRAMID_SIMPLEBLOB = PYRAMIDDETECTOR + SIMPLEBLOB,
            PYRAMID_DENSE = PYRAMIDDETECTOR + DENSE,
            PYRAMID_BRISK = PYRAMIDDETECTOR + BRISK,
            PYRAMID_AKAZE = PYRAMIDDETECTOR + AKAZE,
            DYNAMIC_FAST = DYNAMICDETECTOR + FAST,
            DYNAMIC_STAR = DYNAMICDETECTOR + STAR,
            DYNAMIC_SIFT = DYNAMICDETECTOR + SIFT,
            DYNAMIC_SURF = DYNAMICDETECTOR + SURF,
            DYNAMIC_ORB = DYNAMICDETECTOR + ORB,
            DYNAMIC_MSER = DYNAMICDETECTOR + MSER,
            DYNAMIC_GFTT = DYNAMICDETECTOR + GFTT,
            DYNAMIC_HARRIS = DYNAMICDETECTOR + HARRIS,
            DYNAMIC_SIMPLEBLOB = DYNAMICDETECTOR + SIMPLEBLOB,
            DYNAMIC_DENSE = DYNAMICDETECTOR + DENSE,
            DYNAMIC_BRISK = DYNAMICDETECTOR + BRISK,
            DYNAMIC_AKAZE = DYNAMICDETECTOR + AKAZE;
			*/
		FeatureDetector featureDetector = FeatureDetector.create(9);
		//配置为xml文件,可以先生成默认xml文件,再进行修改数值
		featureDetector.read("params.xml");
		// 修改测试
		featureDetector.detect(dst, keypoints);

你可能感兴趣的:(OpenCV)