手掌手指分割算法(源码)

Catalogue
  1. 1. 开发环境
  2. 2. 功能原理
  3. 3. 算法效果

开发环境

开发环境

  • 64 bits Windows OS (Win8.1)
  • VS2013
  • OpenCV 2.4.9

功能原理

算法要求

完成将Camera拍摄的手掌图片中分割出每个手指用于指纹识别

算法流程

核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifdef TIME_RUN_COST
double duration = static_cast<double>(cv::getTickCount());//time
#endif
cout << "filename=" << filename <<endl;
Mat src = imread(filename, CV_LOAD_IMAGE_COLOR);
if (src.empty())
{
cout << "imread error!!!";
getchar();
return -1;
}
#ifdef BOB_DBG_COM
memset(out_filename, 0, sizeof(out_filename) / sizeof(char));
sprintf(out_filename, "%s-%s.jpg",out_name,"0-0src");
imwrite(out_filename, src);
#endif // BOB_DBG_COM
#if 0
int scaleSize = 4;
resize(src, src, Size(src.cols / scaleSize, src.rows / scaleSize), 0, 0, CV_INTER_AREA);
memset(out_filename, 0, sizeof(out_filename) / sizeof(char));
sprintf(out_filename, "%s-%s.jpg", out_name, "0-0src");
imwrite(out_filename, src);
#endif
#if 1
cout << "cut..." << endl;
int width = src.cols;
int height = src.rows;
float scale = 0.8;
cout << "width=" << width << ",height=" << height << endl;
Rect rect(0, 0, width, height*scale);
Mat imgCut;
imgCut = src(rect).clone();
#endif
//Mat imgCut = src;
cout << "filter..." << endl;
// filter2D(imgCut, imgCut, -1, kernel);
GaussianBlur(imgCut, imgCut, Size(5, 5), 0, 0);
// blur(imgCut, imgCut, Size(5, 5));
#if 0
cout << "EqualizeHist..." << endl;
Mat matOutEqualizeHist = Mat(imgCut.size(), CV_8UC3);
//IplImage* pImgOutEqualizeHist = cvCreateImage(cvSize(cameraFrame.cols, cameraFrame.rows), IPL_DEPTH_8U, 3);
IplImage pImgInEqualizeHist = (IplImage)(imgCut); // Mat-> IplImage
IplImage* pImgOutEqualizeHist = EqualizeHistColorImage(&pImgInEqualizeHist);
matOutEqualizeHist = pImgOutEqualizeHist; //IplImage -> Mat
#endif
// out
Mat imgSrc = Mat(imgCut.size(), CV_8UC1);
imgCut.copyTo(imgSrc);
Mat imgContour = Mat(imgSrc.size(), CV_8UC1);
#ifdef FINGER_EXTRACT_AT_NIGHT
cout << "Nigth,Threshold..." << endl;
Mat imgTmp;// = Mat(imgCut.size(), CV_8UC1);
cvtColor(imgSrc, imgTmp, CV_RGB2GRAY);
cvThresholdOtsu(&((IplImage)imgTmp), &((IplImage)imgTmp));
imgTmp.copyTo(imgContour);
#else
cout << "Day,Skin..." << endl;
Mat imgSkin2 = Mat(imgSrc.size(), CV_8UC1);
IplImage* pImgSkin2 = cvCreateImage(cvSize(imgSrc.cols, imgSrc.rows), IPL_DEPTH_8U, 1);
IplImage pImg2 = (IplImage)(imgSrc); // Mat-> IplImage
cvSkinOtsu(&pImg2, pImgSkin2);
imgSkin2 = pImgSkin2; //IplImage -> Mat
//Mat imgSkin = Mat(imgSrc.size(), CV_8UC1);
imgSkin2.copyTo(imgContour);
#endif
/////////////////////// Contours
cout << "Find Contours..." << endl;
vector<vector<cv::Point> > contours;
vector<Vec4i> hierarchy;
findContours(imgContour, contours, hierarchy,
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
sort(contours.begin(), contours.end(), compareContourAreas);
int contours_num = contours.size();
cout << "contours_num=" << contours_num << endl;
#if 0
vector<vector<Point>>::const_iterator itContours = contours.begin();
//for (int i = 0; i < contours.size(); i++)
for (; itContours != contours.end(); ++itContours)
{
cout << "Size: " << itContours->size() << endl;//每个轮廓包含的点数
}
#endif
#if 1 //usd
// Eliminate too short or too long contours
int cmin = 100; // minimum contour length
//int cmax= 1000; // maximum contour length
vector<vector<Point>>::const_iterator itc = contours.begin();
while (itc != contours.end())
{
//if (itc->size() < cmin || itc->size() > cmax)
if (itc->size() < cmin) {
itc = contours.erase(itc);
}
else
++itc;
}
contours_num = contours.size();
cout << endl << "contours_num after Eliminate=" << contours_num << endl;
#endif
// extract the contour img
cout << "Extract Contours..." << endl;
if (contours_num >= 4)
{
Mat img1, img2, img3, img4;
std::vector<cv::Point> biggest1Contour = contours[contours_num - 1];
std::vector<cv::Point> biggest2Contour = contours[contours_num - 2];
std::vector<cv::Point> biggest3Contour = contours[contours_num - 3];
std::vector<cv::Point> biggest4Contour = contours[contours_num - 4];
std::vector<cv::Point> smallestContour = contours[0];
extractFingerImg2(contours, imgSrc, img1, contours_num, 1);
extractFingerImg2(contours, imgSrc, img2, contours_num, 2);
extractFingerImg2(contours, imgSrc, img3, contours_num, 3);
extractFingerImg2(contours, imgSrc, img4, contours_num, 4);
}
else if (contours_num == 3)
{
Mat img1, img2, img3;
std::vector<cv::Point> biggest1Contour = contours[contours_num - 1];
std::vector<cv::Point> biggest2Contour = contours[contours_num - 2];
std::vector<cv::Point> biggest3Contour = contours[contours_num - 3];
std::vector<cv::Point> smallestContour = contours[0];
extractFingerImg2(contours, imgSrc, img1, contours_num, 1);
extractFingerImg2(contours, imgSrc, img2, contours_num, 2);
extractFingerImg2(contours, imgSrc, img3, contours_num, 3);
}
else if (contours_num == 2)
{
Mat img1, img2;
std::vector<cv::Point> biggest1Contour = contours[contours_num - 1];
std::vector<cv::Point> biggest2Contour = contours[contours_num - 2];
std::vector<cv::Point> smallestContour = contours[0];
extractFingerImg2(contours, imgSrc, img1, contours_num, 1);
extractFingerImg2(contours, imgSrc, img2, contours_num, 2);
}
else if (contours_num == 1)
{
Mat img1;
std::vector<cv::Point> biggest1Contour = contours[contours_num - 1];
std::vector<cv::Point> smallestContour = contours[0];
extractFingerImg2(contours, imgSrc, img1, contours_num, 1);
}
else
{
cout << "error" << endl;
}
#ifdef TIME_RUN_COST
duration = static_cast<double>(cv::getTickCount()) - duration;
duration /= cv::getTickFrequency(); // the elapsed time in ms
cout << "time cost=" << duration << "s"<<endl;
#endif
#ifdef BOB_DBG_COM
memset(out_filename, 0, sizeof(out_filename) / sizeof(char));
sprintf(out_filename, "%s-%s.jpg", out_name, "4-imgContoursInSrc");
imwrite(out_filename, imgSrc);
//imwrite("4-imgContoursInSrc.jpg", imgSrc);
#endif // BOB_DBG_COM

算法效果

白天复杂场景

晚上场景

批量测试场景



By SkySeraph-2014

合作联系:skyseraph00@163.com

————————

版权声明

SkySeraph by SkySeraph is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Bob创作并维护的SkySeraph博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证.
本文首发于SkySeraph博客( http://skyseraph.com ),版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

微信扫码打赏SkySeraph

如果您愿意捐助其它金额请戳我~~,扫码支付宝/微信

本文永久链接:http://skyseraph.com/2014/07/24/CV/手掌手指分割算法/

Comments