OpenCV 圓與矩形識別

2021-08-14 19:29:54 字數 3012 閱讀 7738

最近乙個專案用到了影象識別,之前從未接觸過opencv,經過各種找教程,終於是搞懂了一些。

整個具體流程大概是獲取影象-->影象二值化,灰度圖(cvtcolor)-->影象降噪(gaussianblur)->輪廓識別(cvfindcontours)-->形狀判斷。

大多數教程很專業,各種引數分析看不懂,經過各種搜尋終於是搞懂了。

識別圓

在識別圓方面,opencv有內建的方法:霍夫圓變化:

houghcircles(edges, circles, cv_hough_gradient, 1.5, 10, 200, 100, 0, 0);
引數分析:

edges:灰度影象

circles: std::vectorcircles;陣列,用來儲存圓的座標資訊

cv_hough_gradient:hough 變換方式,目前只支援cv_hough_gradient, which is basically 21ht, described in [yuen03].預設用這個

1.5:累加器影象的解析度,1的時候是與獲取到的影象相同,1.5就是1.5倍

10:圓與圓的最小距離,兩個圓心距離如果在範圍內則被認定為1個圓

200:100-200兩個引數選就夠了

100:預設100,數值越低識別圓越不精確(圓的數量識別變多可能有個弧線就被識別是圓)

最後兩個引數分別是識別 圓的最小,最大的面積。

矩形識別矩形識別並沒有內建方法,需要自己手寫。最主要的方法是二值化。通過二值化來調節識別的強度。

cvthreshold(tgray, gray, 75, 250, cv_thresh_binary);
引數分析:

src:原始

陣列 (單通道 , 8-bit of 32-bit 浮點數)。

dst:輸出陣列,必須與 src 的型別一致,或者為 8-bit。

threshold:閾值

max_value:使用 cv_thresh_binary 和 cv_thresh_binary_inv 的最大值。

threshold_type:閾值型別

threshold_type=cv_thresh_binary:如果 src(x,y)>threshold ,dst(x,y) = max_value; 否則,dst(x,y)=0;

threshold_type=cv_thresh_binary_inv:如果 src(x,y)>threshold,dst(x,y) = 0; 否則,dst(x,y) = max_value.

threshold_type=cv_thresh_trunc:如果 src(x,y)>threshold,dst(x,y) = max_value; 否則dst(x,y) = src(x,y).

threshold_type=cv_thresh_tozero:如果src(x,y)>threshold,dst(x,y) = src(x,y) ; 否則 dst(x,y) = 0。

threshold_type=cv_thresh_tozero_inv:如果 src(x,y)>threshold,dst(x,y) = 0 ; 否則dst(x,y) = src(x,y).

效果圖如下:

在矩形識別裡面的二值化圖:

圓識別:

原始碼:

#include #include #include #include#include#include #include #include#include #pragma comment(lib,"ws2_32.lib")

#includeusing namespace cv;

////函式功能:用向量來做cosα=兩向量之積/兩向量模的乘積求兩條線段夾角

//輸入: 線段3個點座標pt1,pt2,pt0,最後乙個引數為公共點

//輸出: 線段夾角,單位為角度

//double angle(cvpoint* pt1, cvpoint* pt2, cvpoint* pt0)

////函式功能:採用多邊形檢測,通過約束條件尋找矩形

//輸入: img 原影象

// storage 儲存

// minarea,maxarea 檢測矩形的最小/最大面積

// minangle,maxangle 檢測矩形邊夾角範圍,單位為角度

//輸出: 矩形序列

//cvseq* findsquares4(iplimage* img, cvmemstorage* storage, int minarea, int maxarea, int minangle, int maxangle, int(&temp)[30])

}//這裡的s為直角判定條件 單位為角度

if (s > minangle && s < maxangle)

}contours = contours->h_next;

}} std::cout << "圓的數量是"

cvshowimage("22", cpy);

cvreleaseimage(&cpy);

}void sendmessageone()

imshow("【效果圖】", frame);

waitkey(30); }}

int main()

OpenCV識別圓(複雜背景下的圓)

參考 hsl powerpoint中顏色模式之一 即色相 飽和度 亮度 英語 hue,saturation,lightness hsv opencv中的顏色模式 即色相 飽和度 明度 英語 hue,saturation,value 又稱hsb,其中b即英語 brightness。由於這裡使用open...

OpenCV之圓的檢測識別

整個具體流程大概是獲取影象 影象二值化,灰度圖 cvtcolor 影象降噪 gaussianblur 輪廓識別 cvfindcontours 形狀判斷 在識別圓方面,opencv有內建的方法即霍夫圓變化 houghcircles edges,circles,cv hough gradient,1.5...

Opencv實現最小外接矩形和圓

步驟 將一幅影象先轉灰度,再canny邊緣檢測得到二值化邊緣影象,再尋找輪廓,輪廓是由一系列點構成的,要想獲得輪廓的最小外接矩形,首先需要得到輪廓的近似多邊形,用程式設計客棧道格拉斯 普克抽稀 dp 演算法,道格拉斯 普克抽稀演算法,是將曲線近似表示為一系列點,並減少點的數量的一種演算法。該演算法實...