Floyd-Steinberg抖動算法處理價簽圖片-高度還原的圖像二值化處理

原文鏈接:https://blog.csdn.net/weixin_34406061/article/details/92425842

本文只做細(xì)微改動

package com.example;

import java.awt.image.BufferedImage;

/**
 * Floyd-Steinberg抖動算法 工具類
 *
 * @author duxuefu
 * @date 2019-09-16
 */
public class BMPConverterUtil {

    /**
     * 黑白屏
     */
    public final static int PALETTE_BW = 0;

    /**
     * 黑白紅屏
     */
    public final static int PALETTE_BWR = 1;

    /**
     * 黑白黃屏
     */
    public final static int PALETTE_BWY = 2;

    /**
     * 取二值化屏幕類型
     *
     * @param deviceType 屏幕類型
     * @return RGBTriple[]
     * @see RGBTriple
     */
    public static RGBTriple[] getPalette(int deviceType) {
        final RGBTriple[] palette;

        if (deviceType == 1) {
            //黑白紅價簽
            palette = new RGBTriple[]{new RGBTriple(0, 0, 0), new RGBTriple(255, 255, 255), new RGBTriple(255, 0, 0)};
        } else if (deviceType == 2) {
            //黑白黃價簽
            palette = new RGBTriple[]{new RGBTriple(0, 0, 0), new RGBTriple(255, 255, 255), new RGBTriple(255, 255, 0)};
        } else {
            //黑白價簽
            palette = new RGBTriple[]{new RGBTriple(0, 0, 0), new RGBTriple(255, 255, 255)};
        }
        return palette;
    }

    /**
     * 核心算法伙菜,二值化處理
     *
     * @param image   圖片
     * @param palette 屏幕類型
     * @return 二值化數(shù)組宫莱, 0表示黑让虐,1表示白图云,2表示紅或黃(假定紅和黃色不同時存在)
     */
    public static byte[][] floydSteinbergDither(RGBTriple[][] image, RGBTriple[] palette) {
        byte[][] result = new byte[image.length][image[0].length];


        for (int y = 0; y < image.length; y++) {
            for (int x = 0; x < image[y].length; x++) {
                RGBTriple currentPixel = image[y][x];
                byte index = findNearestColor(currentPixel, palette);
                result[y][x] = index;

                for (int i = 0; i < 3; i++) {
                    int error = (currentPixel.channels[i] & 0xff) - (palette[index].channels[i] & 0xff);
                    if (x + 1 < image[0].length) {
                        image[y][x + 1].channels[i] = plus_truncate_uchar(image[y][x + 1].channels[i], (error * 7) >> 4);
                    }
                    if (y + 1 < image.length) {
                        if (x - 1 > 0) {
                            image[y + 1][x - 1].channels[i] = plus_truncate_uchar(image[y + 1][x - 1].channels[i], (error * 3) >> 4);
                        }
                        image[y + 1][x].channels[i] = plus_truncate_uchar(image[y + 1][x].channels[i], (error * 5) >> 4);
                        if (x + 1 < image[0].length) {
                            image[y + 1][x + 1].channels[i] = plus_truncate_uchar(image[y + 1][x + 1].channels[i], (error) >> 4);
                        }
                    }

                }

            }
        }
        return result;
    }

    /**
     * 轉(zhuǎn)換圖片
     *
     * @param bufferedImage    原圖
     * @param newBufferedImage 二值化后圖片
     * @param deviceType       屏幕類型
     */
    public static void floydSteinberg(BufferedImage bufferedImage, BufferedImage newBufferedImage, int deviceType) {
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        RGBTriple[][] image = new RGBTriple[width][height];
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int pixel = bufferedImage.getRGB(i, j);
                int rx = (pixel & 0xff0000) >> 16;
                int gx = (pixel & 0xff00) >> 8;
                int bx = (pixel & 0xff);
                RGBTriple rgbTriple = new RGBTriple(rx, gx, bx);
                image[i][j] = rgbTriple;
            }
        }

        byte[][] to = floydSteinbergDither(image, getPalette(deviceType));
        for (int i = 0; i < to.length; i++) {
            for (int j = 0; j < to[i].length; j++) {
                System.out.println(to[i][j]);
                if (to[i][j] == 0) {
                    // 處理黑色
                    newBufferedImage.setRGB(i, j, 0);
                } else if (to[i][j] == 1) {
                    // 處理白色
                    newBufferedImage.setRGB(i, j, (255 << 16) + (255 << 8) + 255);
                } else if (to[i][j] == 2 && deviceType == 1) {
                    // 處理紅色
                    newBufferedImage.setRGB(i, j, (255 << 16));
                } else if (to[i][j] == 2 && deviceType == 2) {
                    // 處理黃色
                    newBufferedImage.setRGB(i, j, (255 << 16) + (255 << 8));
                }
            }
        }
    }

    /**
     * @param a
     * @param b
     * @return
     */
    private static byte plus_truncate_uchar(byte a, int b) {
        if ((a & 0xff) + b < 0) {
            return 0;
        } else if ((a & 0xff) + b > 255) {
            return (byte) 255;
        } else {
            return (byte) (a + b);
        }
    }

    /**
     * @param color
     * @param palette
     * @return
     */
    private static byte findNearestColor(RGBTriple color, RGBTriple[] palette) {
        int minDistanceSquared = 255 * 255 + 255 * 255 + 255 * 255 + 1;
        byte bestIndex = 0;
        for (byte i = 0; i < palette.length; i++) {
            int Rdiff = (color.channels[0] & 0xff) - (palette[i].channels[0] & 0xff);
            int Gdiff = (color.channels[1] & 0xff) - (palette[i].channels[1] & 0xff);
            int Bdiff = (color.channels[2] & 0xff) - (palette[i].channels[2] & 0xff);
            int distanceSquared = Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff;
            if (distanceSquared < minDistanceSquared) {
                minDistanceSquared = distanceSquared;
                bestIndex = i;
            }
        }
        return bestIndex;
    }

    /**
     * 二值化顏色類
     *
     * @author duxuefu
     * @date 2019-09-16
     */
    public static class RGBTriple {
        public final byte[] channels;

        public RGBTriple(int R, int G, int B) {
            channels = new byte[]{(byte) R, (byte) G, (byte) B};
        }
    }
}

測試類

package binaryzation;

import com.jd.jr.innovation.common.util.BMPConverterUtil;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 類描述
 *
 * @author duxuefu
 * @date 2019-09-16
 */
public class FloydSteinbergTest {

    @Test
    public void binaryzation() {
        // 原圖
        String from = "d:/image/5b7620e1N058c3b11.jpg";
        // 二值化后圖片
        String toFloyd = "d:/image/Floyd.png";
        String toFloydForRed = "d:/image/FloydForRed.png";
        String toFloydForYellow = "d:/image/FloydForYellow.png";
        try {
            // 黑白
            BufferedImage bufferedImage = ImageIO.read(new File(from));
            BufferedImage binaryBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_BGR);
            BMPConverterUtil.floydSteinberg(bufferedImage, binaryBufferedImage, BMPConverterUtil.PALETTE_BW);
            ImageIO.write(binaryBufferedImage, "png", new File(toFloyd));
            // 黑白紅
            BufferedImage binaryBufferedImageForRed = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_BGR);
            BMPConverterUtil.floydSteinberg(bufferedImage, binaryBufferedImageForRed, BMPConverterUtil.PALETTE_BWR);
            ImageIO.write(binaryBufferedImageForRed, "png", new File(toFloydForRed));
            // 黑白黃
            BufferedImage binaryBufferedImageForYellow = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_BGR);
            BMPConverterUtil.floydSteinberg(bufferedImage, binaryBufferedImageForYellow, BMPConverterUtil.PALETTE_BWY);
            ImageIO.write(binaryBufferedImageForYellow, "png", new File(toFloydForYellow));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

測試效果

原圖


5b7620e1N058c3b11.jpg

黑白圖


Floyd.png

黑白紅


FloydForRed.png

黑白黃


FloydForYellow.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绎橘,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子赎婚,更是在濱河造成了極大的恐慌趴乡,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件焙糟,死亡現(xiàn)場離奇詭異口渔,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)穿撮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門缺脉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人悦穿,你說我怎么就攤上這事攻礼。” “怎么了栗柒?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵礁扮,是天一觀的道長。 經(jīng)常有香客問我瞬沦,道長太伊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任逛钻,我火速辦了婚禮僚焦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘曙痘。我一直安慰自己芳悲,他們只是感情好立肘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著名扛,像睡著了一般谅年。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上肮韧,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天融蹂,我揣著相機(jī)與錄音,去河邊找鬼惹苗。 笑死殿较,一個胖子當(dāng)著我的面吹牛耸峭,可吹牛的內(nèi)容都是我干的桩蓉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼劳闹,長吁一口氣:“原來是場噩夢啊……” “哼院究!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起本涕,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤业汰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后菩颖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體样漆,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年晦闰,在試婚紗的時候發(fā)現(xiàn)自己被綠了放祟。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡呻右,死狀恐怖跪妥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情声滥,我是刑警寧澤眉撵,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站落塑,受9級特大地震影響纽疟,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜憾赁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一仰挣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缠沈,春花似錦膘壶、人聲如沸错蝴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽顷锰。三九已至,卻和暖如春亡问,著一層夾襖步出監(jiān)牢的瞬間官紫,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工州藕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留束世,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓床玻,卻偏偏與公主長得像毁涉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子锈死,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容