728x90
반응형



OpenCV에 내장되어있는 SimpleBlobDetector() 이용하여 간단한 Blob Detection을 수행해 보았다. 이 클래스는 features2d.hpp 에 내장되어있다. 그리고 Params 변수를 조절하여 원하는 값을 넣을 수 있고, 그 값으로는 다음과 같다. 자세한 사항은 레퍼러스를 참조해 보도록 한다.


  • 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 ( 4πAreaperimeterperimeter) 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).



아래와 같은 식으로 선언하여 값을 조정해 사용 할 수 있다.



SimpleBlobDetector::Params params;
 
// Change thresholds
params.minThreshold = 10;
params.maxThreshold = 200;
 
// Filter by Area.
params.filterByArea = true;
params.minArea = 1500;
 
// Filter by Circularity
params.filterByCircularity = true;
params.minCircularity = 0.1;
 
// Filter by Convexity
params.filterByConvexity = true;
params.minConvexity = 0.87;
 
// Filter by Inertia
params.filterByInertia = true;
params.minInertiaRatio = 0.01;



#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

void main(){
	Mat im = imread("image/test.jpg", IMREAD_GRAYSCALE);

	imshow("src", im);
	
	//SimpleBlobDetector::Params params;
	//params.filterByArea = true;
	//params.minArea = 4;
	//params.maxArea = 1000;
	//params.filterByColor = true;
	//params.blobColor = 0; // 어두운 얼룩 추출 : 0, 밝은 얼룩 추출 : 255
	//params.maxThreshold = 170;
	//params.filterByCircularity = 10;
	//Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
	
	Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create();

	vector<KeyPoint> keypoints;
	detector->detect(im, keypoints);

	Mat im_with_keypoints;

	drawKeypoints(im, keypoints, im_with_keypoints, Scalar(255, 0, 0),
		DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

	imshow("keypoints", im_with_keypoints);

	cout << "The size of keypoints vector is : " << keypoints.size();
	waitKey(0);
}





참고자료 1 : http://docs.opencv.org/trunk/d0/d7a/classcv_1_1SimpleBlobDetector.html

참고자료 2 : https://www.learnopencv.com/blob-detection-using-opencv-python-c/

728x90
반응형