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檢測多個圓形(霍夫圓檢測,輪廓面積篩選)的文章就介紹到這瞭,更多相關Opencv檢測圓形內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: