本文主要是介绍【肤色检测(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;//histogramfloat 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 histogramint size=height*width;for(int i=0;i<256;i++) {histogram[i]=histogram[i]/size;}//average pixel valuefloat 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;
}
这篇关于【肤色检测(II)】Mat版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!