图片压缩的算法

图片压缩的算法

public class image {
	public void Compress(int n, int[] p, int[] s, int[] l, int[] b) {
		int Lmax = 256;
		int header = 11;
		s[0] = 0;
		for (int i = 1; i <= n; i++) {
			b[i] = length(p[i]);
			int bmax = b[i];
			s[i] = s[i - 1] + bmax;
			l[i] = 1;
			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;
		}
	}

	public int length(int i) {
		int k = 1;
		i = i / 2;
		while (i > 0) {
			k++;
			i = i / 2;
		}
		return k;
	}

	public int Traceback(int n, int i, int[] s, int[] l) {
		if (n == 0)
			return i;
		i = Traceback(n - l[n], i, s, l);
		s[i++] = n - l[n];
		return i;
	}

	public void Output(int[] s, int[] l, int[] b, int n) {
		System.out.println("图像压缩后的最小空间为: " + s[n]);
		int m = 0;
		m = Traceback(n, m, s, l);
		s[m] = n;
		System.out.println("将原灰度序列分成  " + m + " 段序列段");
		for (int j = 1; j <= m; j++) {
			l[j] = l[s[j]];
			b[j] = Maxb(s, l, b, j);
		}
		for (int j = 1; j <= m; j++) {
			System.out.println("段长度:" + l[j] + ",所需存储位数:" + b[j]);
		}
	}

	public int Maxb(int[] s, int[] l, int[] b, int j) {
		int bmax = 0;
		if (j == 1) {
			bmax = b[1];
			for (int i = 2; i <= s[j]; i++) {
				if (bmax < b[i])
					bmax = b[i];
			}
		} else {
			bmax = b[s[j - 1] + 1];
			for (int i = s[j - 1] + 2; i <= s[j]; i++) {
				if (bmax < b[i]) {
					bmax = b[i];
				}
			}
		}
		return bmax;
	}

	public static void image_compressions(Scanner sc) throws NumberFormatException {

		String c = sc.next();
		String[] o = c.split(",");
		int[] p = new int[o.length + 1];
		for (int i = 1; i < p.length; i++) {
			p[i] = Integer.parseInt(o[i - 1]);
		}

		int N = p.length;
		System.out.println();
		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();

		image ic = new image();
		ic.Compress(N - 1, p, s, l, b);
		ic.Output(s, l, b, N - 1);
	}

	public static void main(String[] args) {
		boolean flag = true;
		boolean bflag = true;
		Scanner sc = new Scanner(System.in);
		a: while (flag) {
			System.out.println("请输入图像的灰度序列(整数,以逗号为分隔符):");
			try {
				image_compressions(sc);
			} catch (Exception e) {
				System.out.println("输入不符合规范,请重新输入!");
				System.out.println(
						"---------------------------------------------------------------------------------------");
				System.out.println();
				continue;
			}
			System.out.println();
			System.out
					.println("---------------------------------------------------------------------------------------");
			System.out.println();
			b: while (bflag) {
				System.out.println("选择接下来的操作: 1--继续   2--退出");
				int t = sc.nextInt();
				switch (t) {
				case 1:
					System.out.println(
							"---------------------------------------------------------------------------------------");
					System.out.println();
					break b;
				case 2:
					System.out.println("已退出!");
					break a;
				default:
					System.out.println("没有可执行的操作,请重新输入!");
					System.out.println(
							"---------------------------------------------------------------------------------------");
					System.out.println();
					break;
				}
			}
		}
	}
}

执行结果

请输入图像的灰度序列(整数,以逗号为分隔符)10,12,15,225

图像的灰度值序列为:
10 12 15 225 
图像压缩后的最小空间为: 42
将原灰度序列分成  2 段序列段
段长度:3,所需存储位数:4
段长度:1,所需存储位数:8

---------------------------------------------------------------------------------------

选择接下来的操作: 1--继续   2--退出
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值