为了满足图像处理的要求,博主写个一个简单的肤色检测算法代码,原理和方法见下面代码:
//Author: samylee
//Contact email: ahuljx@126.com
#include "stdlib.h"
#include "stdio.h"
#include "cv.h"
#include "highgui.h"using namespace cv;void SkinRGB(Mat rgb)
{imshow("org", rgb);Size size;size.width = rgb.cols;size.height = rgb.rows;Mat dst = Mat::ones(size, CV_8UC3);for (int row = 0; row < size.height; row++){for (int col = 0; col < size.width; col++){int B = rgb.at<Vec3b>(row, col)[0];int G = rgb.at<Vec3b>(row, col)[1];int R = rgb.at<Vec3b>(row, col)[2];principleif ((R > 95 &&G > 40 &&B > 20 &&R - B > 15 &&R - G > 15)||(R > 200 &&G > 210 &&B > 170 &&abs(R - B) <= 15 &&R > B &&G > B)){dst.at<Vec3b>(row, col)[0] = B;dst.at<Vec3b>(row, col)[1] = G;dst.at<Vec3b>(row, col)[2] = R;}principle}}imshow("result", dst);cv::waitKey(0);
}int main()
{Mat img = imread("test2.jpg");SkinRGB(img);return 0;
}
效果图如下:
原始图:
处理图:
任何问题请加唯一QQ2258205918(名称samylee)!
或唯一VX:samylee_csdn