图像压缩问题(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);

    }

}

依旧是我的猫猫!点赞收藏不迷路哦!
在这里插入图片描述

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
动态规划图像压缩问题中的应用可以通过以下Java代码实现: ```java public class ImageCompression { public static int compressImage(int[][] image, int compressionRate) { int rows = image.length; int cols = image.length; // 创建一个二维数组来保存每个像素点的最优压缩比率 int[][] dp = new int[rows][cols]; // 初始化第一行和第一列的最优压缩比率 dp = image; for (int i = 1; i < rows; i++) { dp[i] = dp[i-1] + image[i]; } for (int j = 1; j < cols; j++) { dp[j] = dp[j-1] + image[j]; } // 计算每个像素点的最优压缩比率 for (int i = 1; i < rows; i++) { for (int j = 1; j < cols; j++) { dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + image[i][j]; } } // 返回最后一个像素点的最优压缩比率 return dp[rows-1][cols-1]; } public static void main(String[] args) { int[][] image = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int compressionRate = 2; int optimalCompressionRate = compressImage(image, compressionRate); System.out.println("Optimal compression rate: " + optimalCompressionRate); } } ``` 这段代码实现了一个`compressImage`方,该方接受一个二维数组`image`表示图像的像素点灰度值,以及一个整数`compressionRate`表示压缩比率。方通过动态规划计算出图像的最优压缩比率,并返回最后一个像素点的最优压缩比率。 在上述代码中,我们使用一个二维数组`dp`来保存每个像素点的最优压缩比率。首先,我们初始化第一行和第一列的最优压缩比率,然后通过遍历每个像素点,计算出其最优压缩比率。最后,返回最后一个像素点的最优压缩比率。 请注意,上述代码仅为示例,实际的图像压缩问题可能涉及更复杂的算和数据结构。此外,还需要根据具体的需求进行适当的调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值