检测手指

时间:2016-09-04 11:19:31

标签: c++ visual-studio opencv contour

我有一张图片

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 个答案:

答案 0 :(得分:0)

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