图像处理之简单脸谱检测算法(Simple Face Detection Algorithm)
介绍基于皮肤检测之后的,寻找最大连通区域,完成脸谱检测的算法。大致的算法步骤如下:
原图如下:
每步处理以后的效果:
程序运行,加载选择图像以后的截屏如下:
截屏中显示图片,是适当放缩以后,代码如下:
Image scaledImage = rawImg.getScaledInstance(200, 200, Image.SCALE_FAST); // Java Image API, rawImage is source image
g2.drawImage(scaledImage, 0, 0, 200, 200, null);
第一步:图像预处理,预处理的目的是为了减少图像中干扰像素,使得皮肤检测步骤可以得
到更好的效果,最常见的手段是调节对比度与亮度,也可以高斯模糊。关于怎么调节亮度与
对比度可以参见这里:http://blog.csdn.net/jia20003/article/details/7385160
这里调节对比度的算法很简单,源代码如下:
package com.gloomyfish.face.detection;
import java.awt.image.BufferedImage;
public class ContrastFilter extends AbstractBufferedImageOp {
private double nContrast = 30;
public ContrastFilter() {
System.out.println("Contrast Filter");
}
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dest) {
int width = src.getWidth();
int height = src.getHeight();
double contrast = (100.0 + nContrast) / 100.0;
contrast *= contrast;
if ( dest == null )
dest = createCompatibleDestImage( src, null );
int[] inPixels = new int[width*height];
int[] outPixels = new int[width*height];
getRGB( src, 0, 0, width, height, inPixels );
int index = 0;
int ta = 0, tr = 0, tg = 0, tb = 0;
for(int row=0; row> 24) & 0xff;
tr = (inPixels[index] >> 16) & 0xff;
tg = (inPixels[index] >> 8) & 0xff;
tb = inPixels[index] & 0xff;
// adjust contrast - red, green, blue
tr = adjustContrast(tr, contrast);
tg = adjustContrast(tg, contrast);
tb = adjustContrast(tb, contrast);
// output RGB pixel
outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
}
}
setRGB( dest, 0, 0, width, height, outPixels );
return dest;
}
public int adjustContrast(int color, double contrast) {
double result = 0;
result = color / 255.0;
result -= 0.5;
result *= contrast;
result += 0.5;
result *=255.0;
return clamp((int)result);
}
public static int clamp(int c) {
if (c < 0)
return 0;
if (c > 255)
return 255;
return c;
}
}
注意:第一步不是必须的,如果图像质量已经很好,可以直接跳过。
第二步:皮肤检测,采用的是基于RGB色彩空间的统计结果来判断一个像素是否为skin像
素,如果是皮肤像素,则设置像素为黑色,否则为白色。给出基于RGB色彩空间的五种皮
肤检测统计方法,最喜欢的一种源代码如下:
package com.gloomyfish.face.detection;
import java.awt.image.BufferedImage;
/**
* this skin detection is absolutely good skin classification,
* i love this one very much
*
* this one should be always primary skin detection
* from all five filters
*
* @author zhigang
*
*/
public class SkinFilter4 extends AbstractBufferedImageOp {
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dest) {
int width = src.getWidth();
int height = src.getHeight();
if ( dest == null )
dest = createCompatibleDestImage( src, null );
int[] inPixels = new int[width*height];
int[] outPixels = new int[width*height];
getRGB( src, 0, 0, width, height, inPixels );
int index = 0;
for(int row=0; row> 24) & 0xff;
tr = (inPixels[index] >> 16) & 0xff;
tg = (inPixels[index] >> 8) & 0xff;
tb = inPixels[index] & 0xff;
// detect skin method...
double sum = tr + tg + tb;
if (((double)tb/(double)tg<1.249) &&
((double)sum/(double)(3*tr)>0.696) &&
(0.3333-(double)tb/(double)sum>0.014) &&
((double)tg/(double)(3*sum)<0.108))
{
tr = tg = tb = 0;
} else {
tr = tg = tb = 255;
}
outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
}
}
setRGB(dest, 0, 0, width, height, outPixels);
return dest;
}
}
使用连通组件标记算法,寻找最大连通区域,关于什么是连通组件标记算法,可以参见这里
http://blog.csdn.net/jia20003/article/details/7483249,里面提到的连通组件算法效率不高,所
以这里我完成了一个更具效率的版本,主要思想是对像素数据进行八邻域寻找连通,然后合
并标记。源代码如下:
package com.gloomyfish.face.detection;
import java.util.Arrays;
import java.util.HashMap;
/**
* fast connected component label algorithm
*
* @date 2012-05-23
* @author zhigang
*
*/
public class FastConnectedComponentLabelAlg {
private int bgColor;
private int[] labels;
private int[] outData;
private int dw;
private int dh;
public FastConnectedComponentLabelAlg() {
bgColor = 255; // black color
}
public int[] doLabel(int[] inPixels, int width, int height) {
dw = width;
dh = height;
int nextlabel = 1;
int result = 0;
labels = new int[dw * dh/2];
outData = new int[dw * dh];
for(int i=0; i knownLabels[m] && knownLabels[m] != 0) {
minLabel = knownLabels[m];
}
}
// find the final label number...
result = (minLabel == 0) ? result : minLabel;
// re-assign the label number now...
if(knownLabels[0] != 0) {
setData(outData, row-1, col, result);
}
if(knownLabels[1] != 0) {
setData(outData, row, col-1, result);
}
if(knownLabels[2] != 0) {
setData(outData, row-1, col-1, result);
}
if(knownLabels[3] != 0) {
setData(outData, row-1, col+1, result);
}
}
}
}
}
outData[index] = result; // assign to label
}
}
// post merge each labels now
for(int row = 0; row < height; row ++) {
for(int col = 0; col < width; col++) {
index = row * width + col;
mergeLabels(index);
}
}
// labels statistic
HashMap labelMap = new HashMap();
for(int d=0; d
找到最大连通区域以后,对最大连通区域数据进行扫描,找出最小点,即矩形区域左上角坐
标,找出最大点,即矩形区域右下角坐标。知道这四个点坐标以后,在原图上打上红色矩形
框,标记出脸谱位置。寻找四个点坐标的实现代码如下:
private void getFaceRectangel() {
int width = resultImage.getWidth();
int height = resultImage.getHeight();
int[] inPixels = new int[width*height];
getRGB(resultImage, 0, 0, width, height, inPixels);
int index = 0;
int ta = 0, tr = 0, tg = 0, tb = 0;
for(int row=0; row> 24) & 0xff;
tr = (inPixels[index] >> 16) & 0xff;
tg = (inPixels[index] >> 8) & 0xff;
tb = inPixels[index] & 0xff;
if(tr == tg && tg == tb && tb == 0) { // face skin
if(minY > row) {
minY = row;
}
if(minX > col) {
minX = col;
}
if(maxY < row) {
maxY = row;
}
if(maxX < col) {
maxX = col;
}
}
}
}
}
缺点:
此算法不支持多脸谱检测,不支持裸体中的脸谱检测,但是根据人脸的
生物学特征可以进一步细化分析,支持裸体人脸检测。
写本文章的目的:本例为图像处理综合运行的一个简单实例。同时人脸检
测也是个感兴趣的话题。