Blob分析
BLOB是圖像中灰度塊的一種專業(yè)稱呼,更加變通一點(diǎn)的可以說(shuō)它跟我們前面二值圖像分析的聯(lián)通組件類似,通過(guò)特征提取實(shí)現(xiàn)常見(jiàn)的各種灰度BLOB對(duì)象組件檢測(cè)與分離。使用該檢測(cè)器的時(shí)候,可以根據(jù)需要輸入不同參數(shù),得到的結(jié)果跟輸入的參數(shù)息息相關(guān)。
Blob分析函數(shù)與演示
OpenCV中的Blob分析函數(shù)為SimpleBlobDetector,OpenCV中支持實(shí)現(xiàn)常見(jiàn)的BLOB分析過(guò)濾,如下所示:
-根據(jù)BLOB面積過(guò)濾 -根據(jù)灰度/顏色值過(guò)濾 -根據(jù)圓度過(guò)濾 -根據(jù)長(zhǎng)軸與短軸過(guò)濾 -根據(jù)凹凸進(jìn)行過(guò)濾
對(duì)應(yīng)的參數(shù)列表如下:
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檢測(cè)示例代碼如下:
#include"opencv2/opencv.hpp" #includeusingnamespacecv; usingnamespacestd; intmain(intargc,char**argv) { //加載圖像 Matsrc=imread("D:/lena.jpg"); Matgray; cvtColor(src,gray,COLOR_BGR2GRAY); cv::imshow("輸入圖像",src); //初始化參數(shù)設(shè)置 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; //創(chuàng)建BLOBDetetor Ptr detector=SimpleBlobDetector::create(params); //BLOB分析與顯示 Matresult; vector keypoints; detector->detect(gray,keypoints); for(autokpt:keypoints){ std::cout<"key?point?radius:?"?< 演示效果如下:
特殊使用技巧
SimpleBlobDetector 函數(shù)有兩個(gè)很詭異的地方。 第一個(gè)是實(shí)現(xiàn)的默認(rèn)參數(shù)支持與參數(shù)檢查 OpenCV中SimpleBlobDetector函數(shù)默認(rèn)的參數(shù)值如下:
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每次執(zhí)行之前都會(huì)進(jìn)行斷言檢查,但是OpenCV中同時(shí)提供了是否啟用Blob各種過(guò)濾開(kāi)關(guān)選項(xiàng),但是無(wú)論開(kāi)關(guān)選項(xiàng)是否啟用,這個(gè)斷言檢查都是檢查全部屬性值,這個(gè)就導(dǎo)致你設(shè)置選項(xiàng)為false的時(shí)候,必須填寫(xiě)對(duì)應(yīng)選項(xiàng)的選項(xiàng)值,否則就無(wú)法執(zhí)行Blob檢測(cè)函數(shù)。對(duì)應(yīng)的源碼文件 blobdetector.cpp發(fā)現(xiàn)了這段代碼作為佐證:::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 第二個(gè)是現(xiàn)實(shí)的默認(rèn)輸入圖像的背景必須是白色 如果是黑色背景圖像輸入,Blob檢測(cè)所有的參數(shù)就直接失效了,但是官方開(kāi)發(fā)教程示例代碼與函數(shù)文檔都沒(méi)有說(shuō)明這點(diǎn),導(dǎo)致很多新手小白不明所以就直接掉坑了,然后就放棄使用這個(gè)函數(shù)了。 審核編輯:黃飛
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4400瀏覽量
66369 -
OpenCV
+關(guān)注
關(guān)注
33文章
650瀏覽量
43981 -
圖像分析
+關(guān)注
關(guān)注
0文章
82瀏覽量
19104 -
BLOB
+關(guān)注
關(guān)注
0文章
13瀏覽量
10365
原文標(biāo)題:OpenCV4圖像分析之BLOB特征分析
文章出處:【微信號(hào):CVSCHOOL,微信公眾號(hào):OpenCV學(xué)堂】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
OpenCV4.8 CUDA編程代碼教程

Vivado HLS實(shí)現(xiàn)OpenCV圖像處理的設(shè)計(jì)流程與分析
紋理特征分析及特征量計(jì)算

基于OpenCV的圖像特征智能識(shí)別系統(tǒng)設(shè)計(jì)
OpenCV3.1的使用教程之圖像特征描述的詳細(xì)資料說(shuō)明
基于OpenCV3.1的圖像特征描述功能實(shí)現(xiàn)
基于OpenCV如何提取中心線
基于opencv4和Yolo-Fastest,實(shí)現(xiàn)PC和單片機(jī)通信,控制步進(jìn)電機(jī)捕獲目標(biāo)

如何在Raspberry Pi 3上安裝OpenCV4庫(kù)

OpenCV4中SIFT算法概述
學(xué)習(xí)OpenCV4的系統(tǒng)化路線圖

評(píng)論