高斯反向投影实现检测图像中的特定物

高斯反向投影实现检测图像中的特定物



高斯反向投影

在图像处理中,我们通常需要设置感兴趣的区域(ROI,region of interest),来简化我们的工作。也就是从图像中选择的一个图像区域,这个区域是我们图像分析所关注的重点。

在上一篇文章图像相似度比较和检测图像中的特定物中,我们使用直方图反向投影的方式来获取ROI,在这里我们采用另一种方式高斯反向投影。它通过基于高斯的概率密度函数(PDF)进行估算,反向投影得到对象区域,该方法可以看成是最简单的图像分割方法。

随机变量X服从一个数学期望为μ、标准方差为σ2的高斯分布,记为:XN(μ,σ2), 则其概率密度函数为

高斯反向投影实现检测图像中的特定物

其中,正态分布的期望值μ决定了其位置,其标准差σ决定了分布的幅度。

算法实现

  1. 输入模型M,对M的每个像素点(R,G,B)计算SUM=R+G+B r=R/SUM, g=G/SUM, b=B/SUM

  2. 根据得到权重比例值,计算得到对应的均值 与标准方差

  3. 对输入图像的每个像素点计算根据高斯公式计算P(r)与P(g)的乘积

  4. 归一化之后输出结果,显示基于高斯分布概率密度函数的反向投影图像。


GaussianBackProjection的算法实现:


  1. import com.cv4j.core.datamodel.ByteProcessor;

  2. import com.cv4j.core.datamodel.ImageProcessor;

  3. import com.cv4j.exception.CV4JException;

  4. import com.cv4j.image.util.Tools;

  5. public class GaussianBackProjection {

  6.    public void backProjection(ImageProcessor src, ImageProcessor model, ByteProcessor dst) {

  7.        if(src.getChannels() == 1 || model.getChannels() == 1) {

  8.            throw new CV4JException("did not support image type : single-channel...");

  9.        }

  10.        float[] R = model.toFloat(0);

  11.        float[] G = model.toFloat(1);

  12.        int r = 0, g = 0, b = 0;

  13.        float sum = 0;

  14.        int mw = model.getWidth();

  15.        int mh = model.getHeight();

  16.        int index = 0;

  17.        for (int row = 0; row < mh; row++) {

  18.            for (int col = 0; col < mw; col++) {

  19.                index = row*mw + col;

  20.                b = model.toByte(2)[index]&0xff;

  21.                g = model.toByte(1)[index]&0xff;

  22.                r = model.toByte(0)[index]&0xff;

  23.                sum = b + g + r;

  24.                R[index] = r / sum;

  25.                G[index] = g / sum;

  26.            }

  27.        }

  28.        // 计算均值与标准方差

  29.        float[] rmdev = Tools.calcMeansAndDev(R);

  30.        float[] gmdev = Tools.calcMeansAndDev(G);

  31.        int width = src.getWidth();

  32.        int height = src.getHeight();

  33.        // 反向投影

  34.        float pr = 0, pg = 0;

  35.        float[] result = new float[width*height];

  36.        for (int row = 0; row < height; row++) {

  37.            for (int col = 0; col < width; col++) {

  38.                index = row*width + col;

  39.                b = src.toByte(2)[index]&0xff;

  40.                g = src.toByte(1)[index]&0xff;

  41.                r = src.toByte(0)[index]&0xff;

  42.                sum = b + g + r;

  43.                float red = r / sum;

  44.                float green = g / sum;

  45.                pr = (float)((1.0 / (rmdev[1]*Math.sqrt(2 * Math.PI)))*Math.exp(-(Math.pow((red - rmdev[0]), 2)) / (2 * Math.pow(rmdev[1], 2))));

  46.                pg = (float)((1.0 / (gmdev[1]*Math.sqrt(2 * Math.PI)))*Math.exp(-(Math.pow((green - gmdev[0]),2)) / (2 * Math.pow(gmdev[1], 2))));

  47.                sum = pr*pg;

  48.                if(Float.isNaN(sum)){

  49.                    result[index] = 0;

  50.                    continue;

  51.                }

  52.                result[index] = sum;

  53.            }

  54.        }

  55.        // 归一化显示高斯反向投影

  56.        float min = 1000;

  57.        float max = 0;

  58.        for(int i=0; i<result.length; i++) {

  59.            min = Math.min(min, result[i]);

  60.            max = Math.max(max, result[i]);

  61.        }

  62.        float delta = max - min;

  63.        for(int i=0; i<result.length; i++) {

  64.            dst.getGray()[i] =  (byte)(((result[i] - min)/delta)*255);

  65.        }

  66.    }

  67. }

GaussianBackProjection的具体使用


  1. GaussianBackProjection gaussianBackProjection = new GaussianBackProjection();

  2. gaussianBackProjection.backProjection(colorProcessor,sampleProcessor,byteProcessor);

  3. result.setImageBitmap(byteProcessor.getImage().toBitmap());

其中,colorProcessor表示原图的对象,sampleProcessor是选取区域的对象,byteProcessor表示反向投影结果。最终byteProcessor把结果展示到Android的ImageView上。


高斯反向投影实现检测图像中的特定物


总结

cv4j(https://github.com/imageprocessor/cv4j) 是

gloomyfish(http://blog.****.net/jia20003)和我一起开发的图像处理库,纯java实现,目前的版本号是0.1.1

前段时间工作比较繁忙cv4j系列停更了一段时间,这次回来我们修复了一些bug。

上一篇cv4j系列的文章讲述了直方图投影,这次的高斯反向投影是另外一种选择。其实,模版匹配也能在图像中寻找到特定的目标,接下来我们的cv4j也会开发模版匹配的功能。

如果您想看该系列先前的文章可以访问下面的文集:http://www.jianshu.com/nb/10401400



关注【Java与Android技术栈】

更多精彩内容请关注扫码

高斯反向投影实现检测图像中的特定物