手掌手指分割算法(源码)
Posted 柒月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手掌手指分割算法(源码)相关的知识,希望对你有一定的参考价值。
开发环境
开发环境
- 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
|
double duration = static_cast<double>(cv::getTickCount());//time
cout << "filename=" << filename <<endl;
Mat src = imread(filename, CV_LOAD_IMAGE_COLOR);
if (src.empty())
{
cout << "imread error!!!";
getchar();
return -1;
}
memset(out_filename, 0, sizeof(out_filename) / sizeof(char));
sprintf(out_filename, "%s-%s.jpg",out_name,"0-0src");
imwrite(out_filename, src);
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);
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();
//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));
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
// out
Mat imgSrc = Mat(imgCut.size(), CV_8UC1);
imgCut.copyTo(imgSrc);
Mat imgContour = Mat(imgSrc.size(), CV_8UC1);
cout << "Nigth,Threshold..." << endl;
Mat imgTmp;// = Mat(imgCut.size(), CV_8UC1);
cvtColor(imgSrc, imgTmp, CV_RGB2GRAY);
cvThresholdOtsu(&((IplImage)imgTmp), &((IplImage)imgTmp));
imgTmp.copyTo(imgContour);
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);
/////////////////////// 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;
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;//每个轮廓包含的点数
}
// 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;
// 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;
}
duration = static_cast<double>(cv::getTickCount()) - duration;
duration /= cv::getTickFrequency(); // the elapsed time in ms
cout << "time cost=" << duration << "s"<<endl;
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);
|
算法效果
白天复杂场景
晚上场景
批量测试场景
转至:http://skyseraph.com/2014/07/24/CV/%E6%89%8B%E6%8E%8C%E6%89%8B%E6%8C%87%E5%88%86%E5%89%B2%E7%AE%97%E6%B3%95/
以上是关于手掌手指分割算法(源码)的主要内容,如果未能解决你的问题,请参考以下文章
图像分割基于matlab粒子群算法优化模拟退火算法图像分割含Matlab源码 2020期
图像分割基于matlab粒子群算法优化模拟退火算法图像分割含Matlab源码 2020期
图像分割基于matlab和声搜索算法图像多级阈值分割含Matlab源码 2044期
图像分割基于matlab和声搜索算法图像多级阈值分割含Matlab源码 2044期