Blob分析
BLOB是圖像中灰度塊的一種專業稱呼,更加變通一點的可以說它跟我們前面二值圖像分析的聯通組件類似,通過特征提取實現常見的各種灰度BLOB對象組件檢測與分離。使用該檢測器的時候,可以根據需要輸入不同參數,得到的結果跟輸入的參數息息相關。
Blob分析函數與演示
OpenCV中的Blob分析函數為SimpleBlobDetector,OpenCV中支持實現常見的BLOB分析過濾,如下所示:
-根據BLOB面積過濾 -根據灰度/顏色值過濾 -根據圓度過濾 -根據長軸與短軸過濾 -根據凹凸進行過濾
對應的參數列表如下:
SimpleBlobDetector::Params() bool filterByArea bool filterByCircularity bool filterByColor bool filterByConvexity bool filterByInertia float maxArea float maxCircularity float maxConvexity float maxInertiaRatio float maxThreshold float minArea float minCircularity float minConvexity float minDistBetweenBlobs float minInertiaRatioOpenCV中Blob檢測示例代碼如下:
#include"opencv2/opencv.hpp" #includeusingnamespacecv; usingnamespacestd; intmain(intargc,char**argv) { //加載圖像 Matsrc=imread("D:/lena.jpg"); Matgray; cvtColor(src,gray,COLOR_BGR2GRAY); cv::imshow("輸入圖像",src); //初始化參數設置 SimpleBlobDetector::Paramsparams; params.minThreshold=10; params.maxThreshold=240; params.filterByArea=true; params.minArea=50; params.filterByCircularity=true; params.minCircularity=0.1; params.filterByConvexity=true; params.minConvexity=0.5; params.filterByInertia=true; params.minInertiaRatio=0.5; //創建BLOBDetetor Ptr detector=SimpleBlobDetector::create(params); //BLOB分析與顯示 Matresult; vector keypoints; detector->detect(gray,keypoints); for(autokpt:keypoints){ std::cout<"key?point?radius:?"?< 演示效果如下:
特殊使用技巧
SimpleBlobDetector 函數有兩個很詭異的地方。 第一個是實現的默認參數支持與參數檢查 OpenCV中SimpleBlobDetector函數默認的參數值如下:
thresholdStep = 10; minThreshold = 50; maxThreshold = 220; minRepeatability = 2; minDistBetweenBlobs = 10; filterByColor = true; blobColor=0; filterByArea = true; minArea = 25; maxArea=5000; filterByCircularity = false; minCircularity = 0.8f; maxCircularity=std::numeric_limits每次執行之前都會進行斷言檢查,但是OpenCV中同時提供了是否啟用Blob各種過濾開關選項,但是無論開關選項是否啟用,這個斷言檢查都是檢查全部屬性值,這個就導致你設置選項為false的時候,必須填寫對應選項的選項值,否則就無法執行Blob檢測函數。對應的源碼文件 blobdetector.cpp發現了這段代碼作為佐證:::max(); filterByInertia = true; //minInertiaRatio = 0.6; minInertiaRatio = 0.1f; maxInertiaRatio=std::numeric_limits ::max(); filterByConvexity = true; //minConvexity = 0.8; minConvexity = 0.95f; maxConvexity = std::numeric_limits ::max(); collectContours = false; staticvoidvalidateParameters(constSimpleBlobDetector::Params&p) { if(p.thresholdStep<=?0) ??????CV_Error(Error::StsBadArg,?"thresholdStep>0"); if(p.minThreshold>p.maxThreshold||p.minThreshold0) ??????CV_Error(Error::StsBadArg,?"0<=minThreshold<=maxThreshold"); ??if?(p.minDistBetweenBlobs?<=0?) ??????CV_Error(Error::StsBadArg,?"minDistBetweenBlobs>0"); if(p.minArea>p.maxArea||p.minArea<=0) ??????CV_Error(Error::StsBadArg,?"0p.maxCircularity||p.minCircularity<=?0) ??????CV_Error(Error::StsBadArg,?"0 p.maxInertiaRatio||p.minInertiaRatio<=?0) ??????CV_Error(Error::StsBadArg,?"0 p.maxConvexity||p.minConvexity<=?0) ??????CV_Error(Error::StsBadArg,?"0 第二個是現實的默認輸入圖像的背景必須是白色 如果是黑色背景圖像輸入,Blob檢測所有的參數就直接失效了,但是官方開發教程示例代碼與函數文檔都沒有說明這點,導致很多新手小白不明所以就直接掉坑了,然后就放棄使用這個函數了。 審核編輯:黃飛
-
函數
+關注
關注
3文章
4379瀏覽量
64640 -
OpenCV
+關注
關注
32文章
642瀏覽量
42727 -
圖像分析
+關注
關注
0文章
82瀏覽量
18917 -
BLOB
+關注
關注
0文章
13瀏覽量
10285
原文標題:OpenCV4圖像分析之BLOB特征分析
文章出處:【微信號:CVSCHOOL,微信公眾號:OpenCV學堂】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
Vivado HLS實現OpenCV圖像處理的設計流程與分析
CMake在Linux 6.1.1-1.0.0中搜索包opencv損壞了嗎?
基于OpenCV的圖像特征智能識別系統設計
基于OpenCV如何提取中心線
基于opencv4和Yolo-Fastest,實現PC和單片機通信,控制步進電機捕獲目標

如何在Raspberry Pi 3上安裝OpenCV4庫

評論