高斯反向投影實(shí)現(xiàn)檢測圖像中的特定物

region_proposal_cat.png

高斯反向投影

在圖像處理中,我們通常需要設(shè)置感興趣的區(qū)域(ROI,region of interest),來簡化我們的工作。也就是從圖像中選擇的一個(gè)圖像區(qū)域,這個(gè)區(qū)域是我們圖像分析所關(guān)注的重點(diǎn)。

在上一篇文章圖像相似度比較和檢測圖像中的特定物中,我們使用直方圖反向投影的方式來獲取ROI,在這里我們采用另一種方式高斯反向投影。它通過基于高斯的概率密度函數(shù)(PDF)進(jìn)行估算,反向投影得到對(duì)象區(qū)域,該方法可以看成是最簡單的圖像分割方法。

隨機(jī)變量X服從一個(gè)數(shù)學(xué)期望為μ、標(biāo)準(zhǔn)方差為σ2的高斯分布,記為:XN(μ,σ2),
則其概率密度函數(shù)為

高斯分布的概率密度函數(shù)
高斯分布的概率密度函數(shù)

其中,正態(tài)分布的期望值μ決定了其位置,其標(biāo)準(zhǔn)差σ決定了分布的幅度。

算法實(shí)現(xiàn)

  1. 輸入模型M,對(duì)M的每個(gè)像素點(diǎn)(R,G,B)計(jì)算SUM=R+G+B
    r=R/SUM, g=G/SUM, b=B/SUM
  2. 根據(jù)得到權(quán)重比例值,計(jì)算得到對(duì)應(yīng)的均值 與標(biāo)準(zhǔn)方差
  3. 對(duì)輸入圖像的每個(gè)像素點(diǎn)計(jì)算根據(jù)高斯公式計(jì)算P(r)與P(g)的乘積
  4. 歸一化之后輸出結(jié)果,顯示基于高斯分布概率密度函數(shù)的反向投影圖像。

GaussianBackProjection的算法實(shí)現(xiàn):

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;
            }
        }

        // 計(jì)算均值與標(biāo)準(zhǔn)方差
        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的具體使用

GaussianBackProjection gaussianBackProjection = new GaussianBackProjection();
     
gaussianBackProjection.backProjection(colorProcessor,sampleProcessor,byteProcessor);

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

其中,colorProcessor表示原圖的對(duì)象,sampleProcessor是選取區(qū)域的對(duì)象,byteProcessor表示反向投影結(jié)果。最終byteProcessor把結(jié)果展示到Android的ImageView上。

高斯反向投影.png

總結(jié)

cv4jgloomyfish和我一起開發(fā)的圖像處理庫,純java實(shí)現(xiàn),目前的版本號(hào)是0.1.1

前段時(shí)間工作比較繁忙cv4j系列停更了一段時(shí)間,這次回來我們修復(fù)了一些bug。

上一篇cv4j系列的文章講述了直方圖投影,這次的高斯反向投影是另外一種選擇。其實(shí),模版匹配也能在圖像中尋找到特定的目標(biāo),接下來我們的cv4j也會(huì)開發(fā)模版匹配的功能。

如果您想看該系列先前的文章可以訪問下面的文集:
http://www.itdecent.cn/nb/10401400

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容