您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“怎么使用Opencv檢測多個圓形”,內容詳細,步驟清晰,細節處理妥當,希望這篇“怎么使用Opencv檢測多個圓形”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
主要是利用霍夫圓檢測、面積篩選等完成多個圓形檢測,具體代碼及結果如下。
第一部分是頭文件(common.h):
#pragma once #include<opencv2/opencv.hpp> #include<opencv2/highgui.hpp> #include<iostream> using namespace std; using namespace cv; extern Mat src; void imageBasicInformation(Mat& src);//圖像基本信息 const Mat houghCirclePre(Mat& srcPre);//霍夫圓檢測預處理 void houghCircle(Mat& srcPreHough);//霍夫圓檢測 const Mat RectCirclePre(Mat& srcPre);//面積篩選擬合圓的預處理 void AreaCircles(Mat& AreaInput);//面積篩選擬合圓檢測
第二部分是主函數:
#include"common.h" Mat src; int main() { src = imread("1.jpg",1); if (src.empty()) { cout << "圖像不存在!" << endl; } else { namedWindow("原圖", 1); imshow("原圖", src); imageBasicInformation(src); Mat srcPreHough = houghCirclePre(src); houghCircle(srcPreHough); Mat RectCir = RectCirclePre(src); AreaCircles(RectCir); waitKey(0); destroyAllWindows(); } return 0; }
第三部分為霍夫圓檢測函數(hough.cpp)
主要包括輸出圖像的基本信息函數:void imageBasicInformation(Mat& src)
霍夫圓檢測預處理函數:const Mat houghCirclePre(Mat& srcPre)
霍夫圓檢測函數:void houghCircle(Mat& srcPreHough)
#include"common.h" Mat graySrc, srcPre;//灰度圖,霍夫檢測預處理, Mat threshold_grayaSrc;//二值化圖 Mat erode_threshold_graySrc, dilate_threshold_graySrc;//二值化后腐蝕,二值化后膨脹 void imageBasicInformation(Mat& src) { int cols = src.cols; int rows = src.rows; int channels = src.channels(); cout << "圖像寬為:" << cols << endl; cout << "圖像高為:" << rows << endl; cout << "圖像通道數:" << channels << endl; } const Mat houghCirclePre(Mat& srcPre) { double houghCirclePreTime = static_cast<double>(getTickCount()); cvtColor(srcPre, graySrc, COLOR_BGR2GRAY); GaussianBlur(graySrc, graySrc, Size(3, 3), 2, 2);//濾波 threshold(graySrc, threshold_grayaSrc, 150, 255, 1);//二值化 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); dilate(threshold_grayaSrc, dilate_threshold_graySrc, element);//膨脹 erode(dilate_threshold_graySrc, erode_threshold_graySrc, element);//腐蝕 houghCirclePreTime = ((double)getTickCount() - houghCirclePreTime) / getTickFrequency(); cout << "霍夫圓預處理時間為:" << houghCirclePreTime << "秒" << endl; return erode_threshold_graySrc; } void houghCircle(Mat& srcPreHough) { cout << "進入霍夫圓檢測" << endl; vector<Vec3f> circles; HoughCircles(srcPreHough, circles, HOUGH_GRADIENT, 1, 60, 1, 35, 0, 0); cout << "圓的個數" << circles.size() << endl; for (size_t i = 0;i < circles.size();i++) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);//畫圓心 circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);//畫圓 } namedWindow("霍夫檢測結果", 0); imshow("霍夫檢測結果", src); imwrite("霍夫圓檢測結果.jpg", src);//保存檢測結果 }
第四部分為利用面積篩選擬合圓檢測(AreaCircle.cpp)
主要包括預處理函數:const Mat RectCirclePre(Mat& srcPre)
面積篩選擬合圓檢測函數:void AreaCircles(Mat& AreaInput)
#include"common.h" Mat graySrc, srcPre;//灰度圖,霍夫檢測預處理, Mat threshold_grayaSrc;//二值化圖 Mat erode_threshold_graySrc, dilate_threshold_graySrc;//二值化后腐蝕,二值化后膨脹 void imageBasicInformation(Mat& src) { int cols = src.cols; int rows = src.rows; int channels = src.channels(); cout << "圖像寬為:" << cols << endl; cout << "圖像高為:" << rows << endl; cout << "圖像通道數:" << channels << endl; } const Mat houghCirclePre(Mat& srcPre) { double houghCirclePreTime = static_cast<double>(getTickCount()); cvtColor(srcPre, graySrc, COLOR_BGR2GRAY); GaussianBlur(graySrc, graySrc, Size(3, 3), 2, 2);//濾波 threshold(graySrc, threshold_grayaSrc, 150, 255, 1);//二值化 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); dilate(threshold_grayaSrc, dilate_threshold_graySrc, element);//膨脹 erode(dilate_threshold_graySrc, erode_threshold_graySrc, element);//腐蝕 houghCirclePreTime = ((double)getTickCount() - houghCirclePreTime) / getTickFrequency(); cout << "霍夫圓預處理時間為:" << houghCirclePreTime << "秒" << endl; return erode_threshold_graySrc; } void houghCircle(Mat& srcPreHough) { cout << "進入霍夫圓檢測" << endl; vector<Vec3f> circles; HoughCircles(srcPreHough, circles, HOUGH_GRADIENT, 1, 60, 1, 35, 0, 0); cout << "圓的個數" << circles.size() << endl; for (size_t i = 0;i < circles.size();i++) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);//畫圓心 circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);//畫圓 } namedWindow("霍夫檢測結果", 0); imshow("霍夫檢測結果", src); imwrite("霍夫圓檢測結果.jpg", src);//保存檢測結果 }
結果如下(自己畫的兩個圓):
原圖:
以下為霍夫圓檢測結果:
以下為面積篩選擬合圓結果:
讀到這里,這篇“怎么使用Opencv檢測多個圓形”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。