gpt4 book ai didi

c++ - OpenCV 三角形检测

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:33 25 4
gpt4 key购买 nike

以下是一种在图像中找到可能正方形的工作方法。有什么建议我可以更改它以代替三角形吗?

static void getSquares(const Mat& image, vector<vector<Point>>& triangles)
{
squares.clear();

Mat pyr, timg, gray0(image.size(), CV_8U), gray;

pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
pyrUp(pyr, timg, image.size());
vector<vector<Point> > contours;

int planes = 1;
int canny = 0;

if (accuracy) {
planes = 4;
canny = 1;
}

for (int c = 0; c < planes; c++)
{
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);

for (int l = 0; l < N; l++) {
if (l == 0 && canny == 1) {
Canny(gray0, gray, 0, thresh, 5);
dilate(gray, gray, Mat(), Point(-1,-1));
} else gray = gray0 >= (l+1)*255/N;

findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

vector<Point> approx;

for (size_t i = 0; i < contours.size(); i++) {
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

if(approx.size() == 4 && fabs(contourArea(Mat(approx))) > 1000 && isContourConvex(Mat(approx))) {
double maxCosine = 0;

for (int j = 2; j < 5; j++) {
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

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

任何帮助将不胜感激,否则,如果您可以建议另一种方法。

问候,C.

最佳答案

至少你需要改变

approx.size() == 4

approx.size() == 3

关于c++ - OpenCV 三角形检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22307078/

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