fanfuhan OpenCV 教學109 ~ opencv-109-BLOB特徵分析(simpleblobdetector使用) [元件 檢測/標記/定位/偵測/分割/抓取]
fanfuhan OpenCV 教學109 ~ opencv-109-BLOB特徵分析(simpleblobdetector使用) [元件 檢測/標記/定位/偵測/分割/抓取]
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/05/22/opencv-109/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
BLOB是圖像中灰度塊的一種專業稱呼,更加變通一點的可以說它跟我們前面二值圖像分析的聯通組件類似,通過特徵提取中的SimpleBlobDetector可以實現常見的各種灰度BLOB對象組件檢測與分離。使用該檢測器的時候,可以根據需要輸入不同參數,得到的結果跟輸入的參數息息相關。
常見的BLOB分析支持如下:
~根據BLOB面積(大小)過濾
~根據灰度/顏色值過濾
~根據圓度過濾
~根據長軸與短軸過濾
~根據凹凸進行過濾
C++
#include "opencv2/opencv.hpp" #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { // 加载图像 Mat src = imread("D:/images/blob2.png"); Mat gray; cvtColor(src, gray, COLOR_BGR2GRAY); // 初始化参数设置 SimpleBlobDetector::Params params; params.minThreshold = 10; params.maxThreshold = 200; params.filterByArea = true; params.minArea = 100; params.filterByCircularity = true; params.minCircularity = 0.1; params.filterByConvexity = true; params.minConvexity = 0.87; params.filterByInertia = true; params.minInertiaRatio = 0.01; // 创建BLOB Detetor Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params); // BLOB分析与显示 Mat result; vector<KeyPoint> keypoints; detector->detect(gray, keypoints); drawKeypoints(src, keypoints, result, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS); imshow("Blob Detection Demo", result); waitKey(0); }
Python
""" BLOB特征分析(simpleblobdetector使用) """ import cv2 as cv frame = cv.imread("images/zhifang_ball.png") cv.imshow("input", frame) gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) params = cv.SimpleBlobDetector_Params() # change thresholds params.minThreshold = 0 params.maxThreshold = 256 # filter by area params.filterByArea = True params.minArea = 100 # filter by circularity params.filterByCircularity = True params.minCircularity = 0.1 # Filter by Convexity params.filterByConvexity = True params.minConvexity = 0.5 # Filter by Inertia params.filterByInertia = True params.minInertiaRatio = 0.5 # 提取关键点 detector = cv.SimpleBlobDetector_create(params) keypoints = detector.detect(gray) for marker in keypoints: result = cv.drawMarker(frame, tuple(int(i) for i in marker.pt), color=(0, 255, 0)) cv.imshow("result", result) cv.waitKey(0) cv.destroyAllWindows()