首頁 > 軟體

Opencv檢測多個圓形(霍夫圓檢測,輪廓面積篩選)

2022-08-07 22:00:37

主要是利用霍夫圓檢測、面積篩選等完成多個圓形檢測,具體程式碼及結果如下。

第一部分是標頭檔案(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檢測圓形內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com