我用 OpenCV 用 C++ 创建了一个程序,它现在能够识别皮肤并成功地创建凸包和凸面缺陷。现在我想计算手指的数量,还有什么办法可以改进这段代码?
我在这里做的是找到船体点的大小,但这并没有给我准确的结果:(
我无法找到一种方法来计算手指的数量,请您帮我计算手指的数量。
这是我的代码
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <sstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv/cxcore.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
void DetectContour(Mat img) {
Mat drawing = Mat::zeros( img.size(), CV_8UC3 );
vector<vector<Point> > contours;
vector<vector<Point> > bigContours;
vector<Vec4i> hierarchy;
findContours(img,contours, hierarchy, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE, Point());
if(contours.size() > 0) {
vector<vector<int> >hull( contours.size() );
vector<vector<Vec4i> > convDef(contours.size() );
vector<vector<Point> > hull_points(contours.size());
vector<vector<Point> > defect_points(contours.size());
for( int i = 0; i < contours.size(); i++ ) {
if(contourArea(contours[i]) > 5000) {
convexHull( contours[i], hull[i], false );
convexityDefects( contours[i],hull[i], convDef[i]);
for(int k=0; k < hull[i].size() ; k++) {
int ind=hull[i][k];
hull_points[i].push_back(contours[i][ind]);
}
for(int k=0;k<convDef[i].size();k++) {
if( convDef[i][k][3] > 20*256 ) { // filter defects by depth
int ind_0=convDef[i][k][0];
int ind_1=convDef[i][k][1];
int ind_2=convDef[i][k][2];
defect_points[i].push_back(contours[i][ind_2]);
circle(drawing,contours[i][ind_0],5,Scalar(0,255,0),-1);
circle(drawing,contours[i][ind_1],5,Scalar(0,255,0),-1);
circle(drawing,contours[i][ind_2],5,Scalar(0,0,255),-1);
line(drawing,contours[i][ind_2],contours[i][ind_0],Scalar(0,0,255),1);
line(drawing,contours[i][ind_2],contours[i][ind_1],Scalar(0,0,255),1);
namedWindow("test",CV_WINDOW_AUTOSIZE);
Mat pic = Mat::zeros(250,250,CV_8UC3);
if(hull_points.size() <= 2)
putText(pic, "0",Point(50,50), CV_FONT_HERSHEY_SIMPLEX, 2, Scalar(255),3,8,false);
else if(hull_points.size() <= 3)
putText(pic,"1",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
else if(hull_points.size() <= 5)
putText(pic,"2",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
else if(hull_points.size() <= 7)
putText(pic,"3",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
else if(hull_points.size() <= 9)
putText(pic,"4",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
else if(hull_points.size() <= 11)
putText(pic,"5",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
else
putText(pic,"OUT OF RANGE",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
imshow("test",pic);
}
}
drawContours( drawing, contours, i, Scalar(0,255,0), 1, 8, vector<Vec4i>(), 0, Point() );
drawContours( drawing, hull_points, i, Scalar(255,0,0), 1, 8, vector<Vec4i>(), 0, Point() );
}
}
}
imshow( "Hull demo", drawing );
}
int main(void) {
int lh = 0;
int hh = 179;
int ls = 0;
int hs = 255;
int lv = 0;
int hv = 255;
namedWindow("HSV",CV_WINDOW_AUTOSIZE);
createTrackbar("LH","HSV",&lh,179);
createTrackbar("HH","HSV",&hh,179);
createTrackbar("LS","HSV",&ls,255);
createTrackbar("HS","HSV",&hs,255);
createTrackbar("LV","HSV",&lv,255);
createTrackbar("HV","HSV",&hv,255);
Mat img;
VideoCapture cap(0);
int th = 100;
while(cap.read(img)) {
Mat frame;
cvtColor(img,frame,CV_BGR2HSV);
inRange(frame,Scalar(lh,ls,lv),Scalar(hh,hs,hv),frame);
threshold(frame,frame,th,2*th+55,THRESH_OTSU);
DetectContour(frame);
if(waitKey(5) == 27)
break;
}
destroyAllWindows();
return 0;
}