gpt4 book ai didi

c++ - OpenCV 最后的凸性缺陷不对

转载 作者:行者123 更新时间:2023-11-28 05:18:43 24 4
gpt4 key购买 nike

我正在尝试编写代码来跟踪手。我正在使用凸缺陷功能来查找手指,但由于某些原因,最后一个缺陷似乎总是有问题。

Here is a picture of what I'm talking about (对不起,我是论坛的新手,所以不能发图片)

青线是轮廓,黄线是外壳点,红线是缺陷点。如您所见,最后一个缺陷点从轮廓的错误一侧检测到缺陷。

这是我的代码:

#include "opencv2\opencv.hpp"

using namespace cv;
using namespace std;

int main() {
VideoCapture cap(0);
Mat src, gray, background, binary, diff;
cap >> background;
cvtColor(background, background, CV_BGR2GRAY);
vector<vector<Point>> contours;
vector < vector<int>> hullI = vector<vector<int>>(1);
vector < vector<Point>> hullP = vector<vector<Point>>(1);
vector<Vec4i> defects;
while (waitKey(30)!='q') {
cap >> src;
cvtColor(src, gray, CV_BGR2GRAY);
blur(gray, gray, Size(3, 3));
absdiff(gray, background, diff);
threshold(diff, binary, 15, 255, THRESH_BINARY);
erode(binary, binary, Mat(Size(5, 5), CV_8U));

imshow("binary", binary);

findContours(binary, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
if (!contours.empty()) {
sort(contours.begin(), contours.end(), [](vector<Point> a, vector<Point> b) { return a.size() > b.size(); });
drawContours(src, contours, 0, Scalar(255, 255, 0));

convexHull(contours[0], hullI[0]);
convexHull(contours[0], hullP[0]);
drawContours(src, hullP, 0, Scalar(0, 255, 255));

if (hullI[0].size() > 2) {
convexityDefects(contours[0], hullI[0], defects);

for (Vec4i defect : defects) {
line(src, contours[0][defect[0]], contours[0][defect[2]], Scalar(0, 0, 255));
line(src, contours[0][defect[1]], contours[0][defect[2]], Scalar(0, 0, 255));
}
}
}
imshow("src", src);
char key = waitKey(30);
if (key == 'q')break;
else if (key == 'p') waitKey();
else if (key == 'b') {
cap >> background;
cvtColor(background, background, CV_BGR2GRAY);
}
}
}

我已经通过实验证实,这种情况也总是发生在缺陷 vector 中的最后一个缺陷。这是 opencv 中的错误还是我做错了什么?

最佳答案

我使用下图(OpenCV 版本为 3.2)测试了您的代码(稍作修改)。

正如您在结果图像上看到的那样,它按预期工作。可能您使用的是旧版本的 OpenCV 并得到了错误的结果。 (我认为这是最近修复的错误)

enter image description here

enter image description here

#include "opencv2\opencv.hpp"

using namespace cv;
using namespace std;

int main() {
//VideoCapture cap(0);
Mat src, gray, background, binary, diff;
//cap >> background;
//cvtColor(background, background, CV_BGR2GRAY);
vector<vector<Point> > contours;
vector < vector<int> > hullI = vector<vector<int> >(1);
vector < vector<Point> > hullP = vector<vector<Point> >(1);
vector<Vec4i> defects;
src = imread("hand.png");
cvtColor(src, gray, CV_BGR2GRAY);
blur(gray, gray, Size(3, 3));
threshold(gray, binary, 150, 255, THRESH_BINARY_INV);
//erode(binary, binary, Mat(Size(5, 5), CV_8U));
imshow("binary", binary);
findContours(binary, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
if (!contours.empty()) {
sort(contours.begin(), contours.end(), [](vector<Point> a, vector<Point> b) { return a.size() > b.size(); });
drawContours(src, contours, 0, Scalar(255, 255, 0));

convexHull(contours[0], hullI[0]);
convexHull(contours[0], hullP[0]);
drawContours(src, hullP, 0, Scalar(0, 255, 255));

if (hullI[0].size() > 2) {
convexityDefects(contours[0], hullI[0], defects);

for (Vec4i defect : defects) {
line(src, contours[0][defect[0]], contours[0][defect[2]], Scalar(0, 0, 255));
line(src, contours[0][defect[1]], contours[0][defect[2]], Scalar(0, 0, 255));
}
}
}
imshow("result", src);
char key = waitKey(0);
return 0;
}

关于c++ - OpenCV 最后的凸性缺陷不对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988577/

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