文章目录

  • 问题描述
  • 算法原理
  • 算法实现
  • 参考资料

问题描述

在计算机中常用像素点灰度值序列算法实现

#include<bits/stdc++.h>
#define VLength 100 
#define LMAX 256
#define HEADER 11
using namespace std;

int length(int x) {  //计算数据存储位数
	int num = 0;
	while (x / 2.0>=1) {
		x = x / 2.0;
		num++;
	}
	if (x > 0)
		num++;
	return num;
}
void compress(int n, int S[], int l[], int b[], int P[]) {//更新S[i]为前i个段最优合并的存储位数
	S[0] = 0;
	for (int i = 1; i <= n; i++) {//遍历每一个像素点
		//每个新的像素点单独成段
		b[i] = length(P[i]);//更新第i位的像素位数
		int bmax = b[i];
		S[i] = S[i - 1] + bmax;
		l[i] = 1;
		for (int j = 2; j <= i && j <= LMAX; j++) {//倒序遍历分段长度,j=2表示后俩点为一组
			bmax = bmax > b[i - j + 1] "共分:" << Length.size() << "段" << endl;
	while (Length.size()) {
		cout << "段起始位置:" << beginP.top() << " ";
		cout << "段长度:" << Length.top() << " ";
		int bmax = INT_MIN;
		for (int i = 0; i < Length.top(); i++) //确定每个分段中最大的存储位数
			bmax = bmax > b[beginP.top() + Length.top() - 1] "存储位数:" << bmax << endl;
		Length.pop();
		beginP.pop();
	}
}
/*
测试数据:
6
10 12 15 255 1 2
16
10 9 12 40 50 35 15 12 8 10 9 15 11 130 160 240
*/

参考资料

  1. 3.7动态规划–图像压缩
  2. 2022春浙江工业大学算法设计与分析习题分享—图像压缩