圖像壓縮問題(Java)

圖像壓縮問題(Java)

好久沒更了,屯了好久,這次依舊是算法作業。

分析我就不說了,直接上網圖。這裏這篇博客

在這裏插入圖片描述
在這裏插入圖片描述

我看網上沒有這個問題的Java動態規劃代碼,我就來更新吧。

/**
 * @ClassName ImageCompress
 * @Description 圖像壓縮問題
 * @author 滑技工廠 https://blog.csdn.net/qq_41718454
 * @date 2020/5/23
 * @version 1.0
 */
public class ImageCompress {

    public static final int N = 7;
    public static int m;
    /*
     * @Title compressImg
     * @Description 分段
     * @author 滑技工廠
     * @Date 2020/5/23
     * @param [n, image, s, l, b]
     * @return void
     * @throws
     */
    public static void compressImg(int n, int[] image, int[] s, int[] l, int[] b) {

        int Lmax = 256;
        int header = 11;
        s[0] = 0;
        //i爲子問題後邊界
        for (int i = 1; i <= n; i++) {
            //像素點p[i]所需的存儲位數
            b[i] = length(image[i]);
            int Bmax = b[i];
            s[i] = s[i - 1] + Bmax;
            l[i] = 1;

            //最後段長j
            for (int j = 2; j <= i && j < Lmax; j++) {

                if (Bmax < b[i - j + 1]) {
                    Bmax = b[i - j + 1];
                }
                if (s[i] > s[i - j] + j * Bmax) {
                    //找到更好的分段
                    s[i] = s[i - j] + j * Bmax;
                    l[i] = j;
                }

            }

            s[i] += header;
        }

    }

    /*
     * @Title length
     * @Description 求i的存儲位數
     * @author 滑技工廠
     * @Date 2020/5/23
     * @param [i]
     * @return int  存儲位數
     * @throws
     */
    public static int length(int i) {
        int k = 1;
        i = i / 2;
        while (i > 0) {
            k++;
            i = i / 2;
        }
        return k;
    }

    /*
     * @Title trackBack
     * @Description 追溯
     * @author 滑技工廠
     * @Date 2020/5/23
     * @param [n, i, s, l]
     * @return void
     * @throws
     */
    public static void trackBack(int n, int[] s, int[] l) {
        if (n == 0)
            return;
        trackBack(n - l[n], s, l);
        s[m++] = n - l[n];
    }

    /*
     * @Title output
     * @Description 輸出
     * @author 滑技工廠
     * @Date 2020/5/23
     * @param [s, l, b, n]
     * @return void
     * @throws
     */
    public static void output(int[] s, int[] l, int[] b, int n) {

        System.out.println("圖像壓縮後的最小空間爲:" + s[n]);

        m = 0;
        trackBack(n,  s, l);
        s[m] = n;
        System.out.println("將原灰度序列分成" + m + "段序列段");
        for (int j = 1; j <= m; j++) {
            l[j] = l[s[j]];
            b[j] = b[s[j]];
        }
        for (int j = 1; j <= m; j++) {
            System.out.println("段長度:" + l[j] + ",所需存儲位數:" + b[j]);
        }

    }

    public static void main(String[] args) {
        //圖像灰度數組,我們從1開始
        int[] p = {0, 10, 12, 25, 255, 1, 2};
        int[] s = new int[N];
        int[] l = new int[N];
        int[] b = new int[N];
        System.out.println("圖像的序列爲:");
        for (int i = 1; i < N; i++) {
            System.out.print(p[i] + " ");
        }

        System.out.println();
        compressImg(N - 1, p, s, l, b);
        output(s, l, b, N - 1);

    }

}

依舊是我的貓貓!點贊收藏不迷路哦!
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章