gpt4 book ai didi

c++ - 检测手中的手指

转载 作者:太空宇宙 更新时间:2023-11-03 23:16:14 25 4
gpt4 key购买 nike

我有一张图片

enter image description here

此图像是阈值函数的输出。我想像上面那样检测指定的物体并测量它们的高度。我的想法是提取轮廓并使用凸包,但我的结果不正确。

有人知道这个问题吗?问候。系统:Win7(64位),OpenCV 3.1,Visual Studio 2015

我的输出:

enter image description here

这是我的代码:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

/// Function header
void thresh_callback(int, void*);

/** @function main */
int main(int argc, char** argv)
{
/// Load source image and convert it to gray
src = imread("C:/Users/Amin/Desktop/binary.jpg", 1);

/// Convert image to gray and blur it
cvtColor(src, src_gray, CV_BGR2GRAY);
blur(src_gray, src_gray, Size(3, 3));

/// Create Window
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src);

createTrackbar(" Threshold:", "Source", &thresh, max_thresh, thresh_callback);
thresh_callback(0, 0);

waitKey(0);
return(0);
}

/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat src_copy = src.clone();
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

/// Detect edges using Threshold
threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);

/// Find contours
findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

/// Find the convex hull object for each contour
vector<vector<Point> >hull(contours.size());
for (int i = 0; i < contours.size(); i++)
{
convexHull(Mat(contours[i]), hull[i], false);
}

/// Draw contours + hull results
Mat drawing = Mat::zeros(threshold_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, 1, 8, vector<Vec4i>(), 0, Point());
drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
}

/// Show in a window
namedWindow("Hull demo", CV_WINDOW_AUTOSIZE);
imshow("Hull demo", drawing);
}

我得到了这个结果,但不知道如何测量 AB?A 在轮廓上,B 已知。

enter image description here谢谢

最佳答案

要测量高度(我想这就是您要问的?),使用凸包毫无意义 - 考虑这种困惑的输出。相反,我会遍历每个轮廓(1、2、3、4 和 5)及其所有点,并通过分析 X、Y 差异分别检测宽度和高度。所以,当你循环让我们说等高线 1 时,如果 Y 在增加而不是 X,你可以假设它是高度 .. 所以计算每个点。然后,当 X 开始增加时,假设高度已经结束。您需要设置一个公差,因为线条不是完全笔直的。希望这会有所帮助。

关于c++ - 检测手中的手指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316350/

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