gpt4 book ai didi

c++ - 如何在opencv中获取轮廓中的角

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:01 28 4
gpt4 key购买 nike

我在 C++ 和 opencv 工作

我正在检测图像中的大轮廓,因为其中有一个黑色区域。 My image

在这种情况下,区域只是水平的,但它可以在任何地方。

Mat resultGray;
cvtColor(result,resultGray, COLOR_BGR2GRAY);
medianBlur(resultGray,resultGray,3);
Mat resultTh;
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Canny( resultGray, canny_output, 100, 100*2, 3 );

findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
Vector<Point> best= contours[0];
int max_area = -1;
for( int i = 0; i < contours.size(); i++ ) {
Scalar color = Scalar( 0, 0, 0 );

if(contourArea(contours[i])> max_area)
{
max_area=contourArea(contours[i]);
best=contours[i];
}
}
Mat approxCurve;
approxPolyDP(Mat(best),approxCurve,0.01*arcLength(Mat(best),true),true);

有了这个,我就有了大轮廓和它的近似值(在 approxCurve 中)。现在,我想获得这个近似值的角并获得这个轮廓内的图像,但我不知道该怎么做。

我正在使用这个 How to remove black part from the image?但是最后一部分我不太理解。

任何人都知道我怎样才能获得角落?这是另一种更简单的方法吗?

谢谢你的时间,

最佳答案

一种更简单的方法是检查图像像素并找到非黑色像素的最小/最大坐标。

像这样:

int maxx,maxy,minx,miny;
maxx=maxy=-std::numeric_limits<int>::max();
minx=miny=std::numeric_limits<int>::min();
for(int y=0; y<img.rows; ++y)
{
for(int x=0; x<img.cols; ++x)
{
const cv::Vec3b &px = img.at<cv::Vec3b>(y,x);
if(px(0)==0 && px(1)==0 && px(2)==0)
continue;
if(x<minx) minx=x;
if(x>maxx) maxx=x;
if(y<miny) miny=y;
if(y>maxy) maxy=y;
}
}
cv::Mat subimg;
img(cv::Rect(cv::Point(minx,miny),cv::Point(maxx,maxy))).copyTo(subimg);

在我看来,这种方法更可靠,因为您不必检测任何轮廓,这可能会导致错误检测,具体取决于输入图像。

关于c++ - 如何在opencv中获取轮廓中的角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23460290/

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