高斯反向投影實現檢測圖像中的特定物

region_proposal_cat.png
region_proposal_cat.png

高斯反向投影

在圖像處理中,咱們一般須要設置感興趣的區域(ROI,region of interest),來簡化咱們的工做。也就是從圖像中選擇的一個圖像區域,這個區域是咱們圖像分析所關注的重點。java

在上一篇文章圖像類似度比較和檢測圖像中的特定物中,咱們使用直方圖反向投影的方式來獲取ROI,在這裏咱們採用另外一種方式高斯反向投影。它經過基於高斯的機率密度函數(PDF)進行估算,反向投影獲得對象區域,該方法能夠當作是最簡單的圖像分割方法。git

隨機變量X服從一個數學指望爲μ、標準方差爲σ2的高斯分佈,記爲:XN(μ,σ2),
則其機率密度函數爲github

高斯分佈的機率密度函數
高斯分佈的機率密度函數

其中,正態分佈的指望值μ決定了其位置,其標準差σ決定了分佈的幅度。算法

算法實現

  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的算法實現:函數

import com.cv4j.core.datamodel.ByteProcessor;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.exception.CV4JException;
import com.cv4j.image.util.Tools;

public class GaussianBackProjection {

    public void backProjection(ImageProcessor src, ImageProcessor model, ByteProcessor dst) {
        if(src.getChannels() == 1 || model.getChannels() == 1) {
            throw new CV4JException("did not support image type : single-channel...");
        }
        float[] R = model.toFloat(0);
        float[] G = model.toFloat(1);
        int r = 0, g = 0, b = 0;
        float sum = 0;
        int mw = model.getWidth();
        int mh = model.getHeight();
        int index = 0;
        for (int row = 0; row < mh; row++) {
            for (int col = 0; col < mw; col++) {
                index = row*mw + col;
                b = model.toByte(2)[index]&0xff;
                g = model.toByte(1)[index]&0xff;
                r = model.toByte(0)[index]&0xff;
                sum = b + g + r;
                R[index] = r / sum;
                G[index] = g / sum;
            }
        }

        // 計算均值與標準方差
        float[] rmdev = Tools.calcMeansAndDev(R);
        float[] gmdev = Tools.calcMeansAndDev(G);

        int width = src.getWidth();
        int height = src.getHeight();

        // 反向投影
        float pr = 0, pg = 0;
        float[] result = new float[width*height];
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                index = row*width + col;
                b = src.toByte(2)[index]&0xff;
                g = src.toByte(1)[index]&0xff;
                r = src.toByte(0)[index]&0xff;
                sum = b + g + r;
                float red = r / sum;
                float green = g / sum;
                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))));
                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))));
                sum = pr*pg;

                if(Float.isNaN(sum)){
                    result[index] = 0;
                    continue;
                }

                result[index] = sum;

            }
        }

        // 歸一化顯示高斯反向投影
        float min = 1000;
        float max = 0;
        for(int i=0; i<result.length; i++) {
            min = Math.min(min, result[i]);
            max = Math.max(max, result[i]);
        }

        float delta = max - min;
        for(int i=0; i<result.length; i++) {
            dst.getGray()[i] =  (byte)(((result[i] - min)/delta)*255);
        }
    }
}複製代碼

GaussianBackProjection的具體使用post

GaussianBackProjection gaussianBackProjection = new GaussianBackProjection();

gaussianBackProjection.backProjection(colorProcessor,sampleProcessor,byteProcessor);

result.setImageBitmap(byteProcessor.getImage().toBitmap());複製代碼

其中,colorProcessor表示原圖的對象,sampleProcessor是選取區域的對象,byteProcessor表示反向投影結果。最終byteProcessor把結果展現到Android的ImageView上。spa

高斯反向投影.png
高斯反向投影.png

總結

cv4jgloomyfish和我一塊兒開發的圖像處理庫,純java實現,目前的版本號是0.1.1.net

前段時間工做比較繁忙cv4j系列停更了一段時間,此次回來咱們修復了一些bug。3d

上一篇cv4j系列的文章講述了直方圖投影,此次的高斯反向投影是另一種選擇。其實,模版匹配也能在圖像中尋找到特定的目標,接下來咱們的cv4j也會開發模版匹配的功能。rest

若是您想看該系列先前的文章能夠訪問下面的文集:
www.jianshu.com/nb/10401400

相關文章
相關標籤/搜索