C++ OpenCv二值化找圓心座標

金灿灿發表於2024-11-05

思路:影像轉為灰度圖,然後二值化得到只有0或255的點座標,此處圓是黑點,所以新增所有畫素值為0的座標。在這些座標中找到圓上最左邊、最頂端、最右邊、最底端的四個點,這時可求出圓心座標。

.cpp檔案


#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <map>
#include <any>


//
1.獲取圖片 cv::Mat src = cv::imread("D:\\1.bmp"); // 2.將彩色影像轉換為灰度影像 cv::Mat gray_image; cv::cvtColor(src, gray_image, cv::COLOR_BGR2GRAY); // 3.二值化處理 cv::Mat dst; cv::threshold(gray_image, dst, threshold, 255, cv::THRESH_BINARY); // 4.找到所有目標座標 cv::Point leftPoint; cv::Point rightPoint; cv::Point topPoint; cv::Point bottomPoint; std::vector<cv::Point> points; for (int i = 0; i < dst.rows; ++i) { for (int j = 0; j < dst.cols; ++j) { if (dst.at<uchar>(i, j) != 255) { // 0:白色、255:黑色 cv::Point tempPoint = cv::Point(j, i); // 去除邊緣干擾點位,按實際需求新增 if (j < 50 || i < 50) { continue; } // 賦值初始座標 if (leftPoint.x == 0 && leftPoint.y == 0) { leftPoint = tempPoint; } if (rightPoint.x == 0 && rightPoint.y == 0) { rightPoint = tempPoint; } if (topPoint.x == 0 && topPoint.y == 0) { topPoint = tempPoint; } if (bottomPoint.x == 0 && bottomPoint.y == 0) { bottomPoint = tempPoint; } points.push_back(tempPoint); // 注意OpenCV的座標系是(列,行) //else { if (leftPoint.x == 0 || tempPoint.x < leftPoint.x) { leftPoint = tempPoint; } if (topPoint.y == 0 || tempPoint.y < topPoint.y) { topPoint = tempPoint; } if (rightPoint.x == 0 || tempPoint.x > rightPoint.x) { rightPoint = tempPoint; } if (bottomPoint.y == 0 || tempPoint.y > bottomPoint.y) { bottomPoint = tempPoint; } //} } } } // 5.求圓半徑, int r1 = rightPoint.x - leftPoint.x; int r2 = bottomPoint.y - topPoint.y; int r = (r1 + r2) / 2;

// 6.求圓心座標
cv::Point circleCenter(leftPoint.x + r, topPoint.y + r);

// 7.對圓畫框
// 7.1.定義矩形的左上角和右下角座標
cv::Point2f rect_start(leftPoint.x, topPoint.y);
cv::Point2f rect_end(rightPoint.x, bottomPoint.y);

// 7.2.畫矩形框
cv::rectangle(src, rect_start, rect_end, cv::Scalar(0, 255, 0));

相關文章