本文介绍了opencv c ++查找轮廓的内切圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想找到轮廓的最大内切圆.
我用cv::findContours
检测到轮廓,并以vector<Point>
的形式存在.
I have detected the contour with cv::findContours
and it is there as a vector<Point>
.
我知道如何检测最小封闭圆(cv::minEnclosingCircle
),但不知道如何获取最大封闭圆.该怎么做?
I know how to detect the minimum enclosing circle (cv::minEnclosingCircle
), but not how to get the maximum inclosing circle. How to do this?
问题2:如何使刻划和外接圆以质心为中心?
Question2: How do i get the inscribing and circumscribing circles centered on the center of mass?
为澄清起见,我尝试描述这些圆圈的含义:
For clarification, i try to describe, what i mean with these circels:
- 最小包围圈:从外部触摸物体,中心位置无所谓,最小面积.
- 外接圆:从外部触摸对象,中心位置位于对象的质心上,最小面积.
- 最大闭合圆:从内部触摸物体,中心位置无所谓,最大面积.
- 内切圆:从内部触摸对象,对象的质心的中心位置,最大面积.
- min enclosing circle: touching object from outside, center position doesn't matter, minimum area.
- circumscribing circle: touching object from outside, center position on the center of mass of the object, minimum area.
- max inclosing circle: touching object from inside, center position doesn't matter, maximum area.
- inscribing circle: touching object from inside, center position on the center of mass of the object, maximum area.
推荐答案
您可以:
1)从轮廓创建蒙版
2)计算蒙版上的distanceTransform
3)最大值是半径,位置是中心
3) The highest value is the radius, its position is the center
代码:
#include <opencv2\opencv.hpp>
int main()
{
// Load image
cv::Mat1b img = cv::imread("path_to_img", cv::IMREAD_GRAYSCALE);
// Correct image
cv::Mat1b bin = img < 127;
// Find contour
std::vector<std::vector<cv::Point>> contours;
cv::findContours(bin, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
// Draw on mask
cv::Mat1b mask(bin.rows, bin.cols, uchar(0));
cv::drawContours(mask, contours, 0, cv::Scalar(255), cv::FILLED);
// Distance Trasnsform
cv::Mat1f dt;
cv::distanceTransform(mask, dt, cv::DIST_L2, 5, cv::DIST_LABEL_PIXEL);
// Find max value
double max_val;
cv::Point max_loc;
cv::minMaxLoc(dt, nullptr, &max_val, nullptr, &max_loc);
// Output image
cv::Mat3b out;
cv::cvtColor(img, out, cv::COLOR_GRAY2BGR);
cv::circle(out, max_loc, max_val, cv::Scalar(0, 255, 0));
return 0;
}
这篇关于opencv c ++查找轮廓的内切圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!