Java實(shí)現(xiàn)圖像中值濾波

pixel是通過標(biāo)準(zhǔn)jdk或android bitmap獲取的圖像32位像素?cái)?shù)組
這個(gè)方法的濾波器尺寸是3*3,需要更大尺寸濾波器可以依此類推,ColorModel可根據(jù)各平臺(tái)變化調(diào)整

private int[] medianFiltering(int[] pixel, int w, int h) {
        int[] newPixel = new int[w * h];
        int[] tempR = new int[9];
        int[] tempG = new int[9];
        int[] tempB = new int[9];
        ColorModel cm = ColorModel.getRGBdefault();
        // median values of the matrix
        int r;
        int g;
        int b;
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (x == 0 || x == w - 1 || y == 0 || y == h - 1) {
                    newPixel[y * w + x] = pixel[y * w + x];
                    continue;
                }
                tempR[0] = cm.getRed(pixel[x - 1 + (y - 1) * w]);
                tempR[1] = cm.getRed(pixel[x + (y - 1) * w]);
                tempR[2] = cm.getRed(pixel[x + 1 + (y - 1) * w]);
                tempR[3] = cm.getRed(pixel[x - 1 + y * w]);
                tempR[4] = cm.getRed(pixel[x + y * w]);
                tempR[5] = cm.getRed(pixel[x + 1 + y * w]);
                tempR[6] = cm.getRed(pixel[x - 1 + (y + 1) * w]);
                tempR[7] = cm.getRed(pixel[x + (y + 1) * w]);
                tempR[8] = cm.getRed(pixel[x + 1 + (y + 1) * w]);
                r = getMedianValue(tempR);

                tempG[0] = cm.getGreen(pixel[x - 1 + (y - 1) * w]);
                tempG[1] = cm.getGreen(pixel[x + (y - 1) * w]);
                tempG[2] = cm.getGreen(pixel[x + 1 + (y - 1) * w]);
                tempG[3] = cm.getGreen(pixel[x - 1 + y * w]);
                tempG[4] = cm.getGreen(pixel[x + y * w]);
                tempG[5] = cm.getGreen(pixel[x + 1 + y * w]);
                tempG[6] = cm.getGreen(pixel[x - 1 + (y + 1) * w]);
                tempG[7] = cm.getGreen(pixel[x + (y + 1) * w]);
                tempG[8] = cm.getGreen(pixel[x + 1 + (y + 1) * w]);

                g = getMedianValue(tempG);
                tempB[0] = cm.getBlue(pixel[x - 1 + (y - 1) * w]);
                tempB[1] = cm.getBlue(pixel[x + (y - 1) * w]);
                tempB[2] = cm.getBlue(pixel[x + 1 + (y - 1) * w]);
                tempB[3] = cm.getBlue(pixel[x - 1 + y * w]);
                tempB[4] = cm.getBlue(pixel[x + y * w]);
                tempB[5] = cm.getBlue(pixel[x + 1 + y * w]);
                tempB[6] = cm.getBlue(pixel[x - 1 + (y + 1) * w]);
                tempB[7] = cm.getBlue(pixel[x + (y + 1) * w]);
                tempB[8] = cm.getBlue(pixel[x + 1 + (y + 1) * w]);

                // median value
                b = getMedianValue(tempB);
                newPixel[y * w + x] = 255 << 24 | r << 16 | g << 8 | b;
            }
        }
        return newPixel;
    }
?著作權(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)容