我有一张图片
该图像是阈值函数的输出。
我想如上所述检测指定的对象并测量其高度。
我的想法是提取轮廓并使用凸包,但是我的结果不正确。
有没有人知道这个问题?
问候。
Sys:Win7(64位),OpenCV 3.1,Visual Studio 2015
我的输出:
这是我的代码:
#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是已知的。
谢谢
最佳答案
要测量高度(我想这就是您要问的?),使用凸包没有什么意义-考虑这个杂乱的输出。相反,我将遍历每个轮廓(1、2、3、4和5)及其所有点,并通过分析X,Y差异分别检测宽度和高度。因此,当您遍历让我们说等高线1时,如果Y在增加,而X在不增加,则可以假定它是高度..因此计算每个点。然后,当X开始增加时,假定高度已经结束。您将要设置公差,因为直线不是完全笔直。希望这可以帮助。
关于c++ - 检测手中的手指,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39316350/