gpt4 book ai didi

android - 在轮廓openCV中查找直线

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:41 27 4
gpt4 key购买 nike

我正在使用 android openCV,我想检测图像中的三角形、矩形和圆形。所以我这样做如下:Canny => findContours => approxPolyDP 并得到这张图片: Image Hosted by ImageShack.us


但是,approxPolyDP 的结果包含太多顶点,所以我无法确定它是哪种形状。为了消除顶点,我想检测每个轮廓中的线并找到它们的交点。我怎样才能为单个轮廓做到这一点?

最佳答案

对于圆检测,使用 HoughCircles .

那么在这里您只是在寻找简化的多边形(三角形和正方形)。您是否尝试过在 approxPolyDP 中调整 epsilon?

这是来自 openCV squares.cpp sample code 的示例 fragment - 查看近似精度(epsilon,approxPolyDP 的第三个参数)是如何相对于轮廓的大小设置的。

C++ 代码,但 openCV 接口(interface)应该是相同的,所以我相信它很容易适应您的环境。

 // test each contour
for( size_t i = 0; i < contours.size(); i++ )
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx,
arcLength(Mat(contours[i]), true)*0.02, true);

// square contours should have 4 vertices after approximation
// relatively large area (to filter out noisy contours)
// and be convex.
// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if( approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)) )
{
double maxCosine = 0;

for( int j = 2; j < 5; j++ )
{
// find the maximum cosine of the angle between joint edges
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

// if cosines of all angles are small
// (all angles are ~90 degree) then write quandrange
// vertices to resultant sequence

if( maxCosine < 0.3 )
squares.push_back(approx);
}
}

关于android - 在轮廓openCV中查找直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14322132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com