在图像处理和计算机视觉中,基于彩色空间有许多的应用,本实验主要涉及基于HLS模型的皮肤检测和红眼检测,测试图片如下:
在OpenCV中,将RGB图像转换到HLS空间的效果如下:
在大量的研究实验工作后,(S>=50) && (L_S_ratio>0.5) && (L_S_ratio<3.0) && ((H<=14) || (H>=165))的模型较为可靠,下面分别是测试代码和效果:
void skinDetect(Mat &hls_image, Mat &bina_image){
CV_Assert(hls_image.channels() == 3 && bina_image.type() == CV_8UC1);
for (int row = 0; row < hls_image.rows; ++row)
for (int col = 0; col < hls_image.cols; ++col)
{
uchar H = hls_image.at<Vec3b>(row, col)[0];
uchar L = hls_image.at<Vec3b>(row, col)[1];
uchar S = hls_image.at<Vec3b>(row, col)[2];
double LS_ratio = ((double)L) / ((double)S);
bina_image.at<uchar>(row, col) = ((S >= 50) && (LS_ratio>0.5) &&
(LS_ratio < 3.0) && ((H <= 14) || (H>165)))? 255:0;
}
}
在红眼检测中,(L>=64) && (S>=100) && (L_S_ration>0.5) && (L_S_ratio<1.5) && ((H<=7) || (H>=162))的模型较为可靠。
void redEyeDetect(Mat &hls_image, Mat &bina_image){
CV_Assert(hls_image.channels() == 3 && bina_image.type() == CV_8UC1);
for (int row = 0; row < hls_image.rows; ++row)
for (int col = 0; col < hls_image.cols; ++col)
{
uchar H = hls_image.at<Vec3b>(row, col)[0];
uchar L = hls_image.at<Vec3b>(row, col)[1];
uchar S = hls_image.at<Vec3b>(row, col)[2];
double LS_ratio = ((double)L) / ((double)S);
bina_image.at<uchar>(row, col) = ((L >= 64) && (S>=100) &&
(LS_ratio >0.5) && (LS_ratio<1.5) && ((H <= 7) || (H>162)))? 255:0;
}
}