1 前備知識
(1)圓度
圓度誤差:指包容同一正截面實際輪廓且半徑差為最小的兩同心圓間的距離:fm = Rmax - Rmin;
圓度公差帶:在同一正截面上半徑差為公差值的兩同心圓間的區域。
(2)偏心率
橢圓兩焦點間的距離和長軸長度的比值。即某一橢圓軌道與理想圓環的偏離,長橢圓軌道“偏心率”高,而近於圓形的軌道“偏心率”低。
離心率定義為橢圓兩焦點間的距離和長軸長度的比值。
偏心率用來描述軌道的形狀,用焦點間距離除以長軸的長度可以算出偏心率。偏心率一般用e表示。
當e=0時 圓
當0<e<1時 橢圓
當e=1時 拋物線
當e>1時雙曲線
(3)凸度
2 所用到的主要OpenCv API及類
(1)SimpleBlobDetector //TODO 從Opencv3 中查看中文解釋
/** @brief Class for extracting blobs from an image. :
The class implements a simple algorithm for extracting blobs from an image:
1. Convert the source image to binary images by applying thresholding with several thresholds from
minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
neighboring thresholds.//二值化
2. Extract connected components from every binary image by findContours and calculate their
centers.//從二值化圖像中提取連通域並計算該連通域的中心
3. Group centers from several binary images by their coordinates. Close centers form one group that
corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
4. From the groups, estimate final centers of blobs and their radiuses and return as locations and
sizes of keypoints.
This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
to turn on/off corresponding filtration. Available filtrations:
- **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
(\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) 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).
Default values of parameters are tuned to extract dark circular blobs.默認的參數是用於提取暗黑圓斑塊
*/
class CV_EXPORTS_W SimpleBlobDetector : public Feature2D { public: struct CV_EXPORTS_W_SIMPLE Params { CV_WRAP Params(); CV_PROP_RW float thresholdStep;//閾值步進值 CV_PROP_RW float minThreshold;//進行二值化時的最小閾值 CV_PROP_RW float maxThreshold;//進行二值化的最大閾值 CV_PROP_RW size_t minRepeatability; CV_PROP_RW float minDistBetweenBlobs; CV_PROP_RW bool filterByColor; CV_PROP_RW uchar blobColor; CV_PROP_RW bool filterByArea; CV_PROP_RW float minArea, maxArea; CV_PROP_RW bool filterByCircularity; CV_PROP_RW float minCircularity, maxCircularity; CV_PROP_RW bool filterByInertia; CV_PROP_RW float minInertiaRatio, maxInertiaRatio; CV_PROP_RW bool filterByConvexity; CV_PROP_RW float minConvexity, maxConvexity; void read( const FileNode& fn ); void write( FileStorage& fs ) const; }; CV_WRAP static Ptr<SimpleBlobDetector> create(const SimpleBlobDetector::Params ¶meters = SimpleBlobDetector::Params()); CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; };
(2)Function
/** @brief Draws keypoints.
@param image Source image.
@param keypoints Keypoints from the source image.
@param outImage Output image. Its content depends on the flags value defining what is drawn in the
output image. See possible flags bit values below.
@param color Color of keypoints.
@param flags Flags setting drawing features. Possible flags bit values are defined by
DrawMatchesFlags. See details above in drawMatches .
@note
For Python API, flags are modified as cv2.DRAW_MATCHES_FLAGS_DEFAULT,
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG,
cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
*/
CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage, const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT );
3 程序代碼
#include<opencv2\opencv.hpp> #include<iostream> using namespace std; using namespace cv; int main(int argc, char** argv) { //加載圖像 Mat src = imread("G:\\CVworkstudy\\program_wwx\\研習社140課時\\ZhaiZhigang140\\zhifang_ball.png"); if (src.empty()) { cout << "Could not load image..." << endl; return -1; } imshow("srcImg", src); Mat gray; cvtColor(src, gray, CV_RGB2GRAY); //初始化參數設置 SimpleBlobDetector::Params filterParams; filterParams.minThreshold = 10; filterParams.maxThreshold = 200; filterParams.filterByArea = true; filterParams.minArea = 100; filterParams.filterByCircularity = true; filterParams.minCircularity = 0.1f; filterParams.filterByConvexity = true; filterParams.minConvexity = 0.87f; filterParams.filterByInertia = true; filterParams.minInertiaRatio = 0.01f; //創建BLOB Detector Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(filterParams); //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); }
4 運行結果
5 擴展及注意事項
//TODO