gpt4 book ai didi

c++ - 在 OpenCV 中获取轮廓的坐标

转载 作者:搜寻专家 更新时间:2023-10-31 00:32:54 29 4
gpt4 key购买 nike

假设我有以下输出图像:

enter image description here

基本上,我有视频流,我只想在输出图像中获取矩形的坐标。这是我的 C++ 代码:

while(1)
{
capture >> frame;

if(frame.empty())
break;

cv::cvtColor( frame, gray, CV_BGR2GRAY ); // Grayscale image

Canny( gray, canny_output, thresh, thresh * 2, 3 );

// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

// Draw contours
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );

for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
}

cv::imshow( "w", drawing );

waitKey(20); // waits to display frame

}

谢谢。

最佳答案

看opencv文档中find contours函数的定义,看参数(link):

void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())

参数:here

看看轮廓,就像 Rafa 所说的那样,每个轮廓都存储在一个点 vector 中,每个点 vector 都存储在一个 vector 中,因此,通过在外部 vector 中行走,然后在内部 vector 中行走,你会发现你想要的分数。

但是,如果您只想检测较大的轮廓,您可能需要使用 CV_RETR_EXTERNAL 作为模式参数,因为它只会检测大部分外部轮廓(大矩形)。

如果您仍然希望保持较小的轮廓,那么您可以使用 CV_RETR_TREE 并使用层次结构进行计算:Using hierarchy contours

关于c++ - 在 OpenCV 中获取轮廓的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30212713/

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