【肤色检测(II)】Mat版
将之前写的 (I) 中的内容改成了 Mat 版本:
//opencv2.0风格 #include "cv.h" #include "highgui.h" #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> #include <iostream> #include <string> #include <cstdio> using namespace std; using namespace cv; void whThresholdOtsu(Mat &src, Mat &dst) { int height=src.rows; int width=src.cols; //histogram float histogram[256]={0}; for(int i=0;i<height;i++) { unsigned char* p=(unsigned char*)src.data+src.step*i; for(int j=0;j<width;j++) { histogram[*p++]++; } } //normalize histogram int size=height*width; for(int i=0;i<256;i++) { histogram[i]=histogram[i]/size; } //average pixel value float avgValue=0; for(int i=0;i<256;i++) { avgValue+=i*histogram[i]; } int threshold; float maxVariance=0; float w=0,u=0; for(int i=0;i<256;i++) { w+=histogram[i]; u+=i*histogram[i]; float t=avgValue*w-u; float variance=t*t/(w*(1-w)); if(variance>maxVariance) { maxVariance=variance; threshold=i; } } cv::threshold( src, dst, threshold, 255, THRESH_BINARY); } void cvSkinOtsu(Mat &src, Mat &dst) { Mat ycrcb; Mat cr; cvtColor(src,ycrcb,CV_BGR2YCrCb); //show ycrcb, test //imshow("ycrcb",ycrcb); vector<Mat> mv; split(ycrcb,mv); whThresholdOtsu(mv[1],cr); imshow("out2",cr); //cvWaitKey(0); dst = cr.clone(); } int main() { Mat img = imread("mmr.jpg"); //out用以保存输出图像 Mat out; imshow("in",img); cvSkinOtsu(img,out); //imshow("out",out); cvWaitKey(0); return 0; }
最后更新:2017-04-03 05:40:10
上一篇:
如何才能成为一名优秀的软件测试人员
下一篇:
机房收费系统之思想性总结
新工具——TensorLayer:管理深度学习发展的复杂性
关于基于xfire webservice框架开发webservice的总结
Error 1 Expected class, delegate, enum, interface, or struct
Gradle精选: 在IntellIJ IDEA中创建快捷方式,用于刷新Gradle工程
configure: error: XML configuration could not be found
C++之拷贝构造函数
天猫618收官之时,马云张勇在哪?
C# 计算中文汉字笔画数
我国物联网基础标准研究国际领先
VO对象通过groovy模板映射XML文件