close all; clear all; clc; %关闭所有图形窗口,清除工作空间所有变量,清空命令行
I=imread('lenabmp');%待编码的矩阵
imshow(I);
thresh = graythresh(I); %自动确定二值化阈值
I2 = im2bw(I,thresh); %对图像二值化
imshow(I2);
[m,n]=size(I2); %计算矩阵大小
I2=double(I2);
p_table=tabulate(I2(:)); %统计矩阵中元素出现的概率,第一列为矩阵元素,第二列为个数,第三列为概率百分数
color=p_table(:,1)';
p=p_table(:,3)'/100; %转换成小数表示的概率
psum=cumsum(p_table(:,3)'); %计算数组各行的累加值
allLow=[0,psum(1:end-1)/100];%由于矩阵中元素只有两种,将[0,1)区间划分为两个区域allLow和 allHigh
allHigh=psum/100;
numberlow=0; %定义算术编码的上下限numberlow和numberhigh
numberhigh=1;
for k=1:m %以下计算算术编码的上下限,即编码结果
for kk=1:n
data=I2(k,kk);
low=allLow(data==color);
high=allHigh(data==color);
range=numberhigh-numberlow;
tmp=numberlow;
numberlow=tmp+rangelow;
numberhigh=tmp+rangehigh;
end
end
fprintf('算术编码范围下限为%1615f\n\n',numberlow);
fprintf('算术编码范围上限为%1615f\n\n',numberhigh);
Mat=zeros(m,n); %解码
for k=1:m
for kk=1:n
temp=numberlow<low;
temp=[temp 1];
indiff=diff(temp);
indiff=logical(indiff);
Mat(k,kk)=color(indiff);
low=low(indiff);
high=allHigh(indiff);
range=high - low;
numberlow=numberlow-low;
numberlow=numberlow/range;
end
end
1你说的解码程序一般是作为库存在的,也就是xxdll
2c语言调用库可以参考msdn或者百度,其实都很容易实现。
3一般成熟的库都会有官方说明,看看官方说明就会知道需要的函数在什么dll里面。
4你现在的问题主要是入门不太会,搞懂了第一第二两个方面,后面几乎不涉及算法,还是比较容易制作的。
祝你好运,不懂留言给我。
#include <stdioh>
#define MINCHAR 32
#define CHARSUM 94
int encode(char key, char source, char dest);
int decode(char key, char source, char dest);
char table[CHARSUM][CHARSUM];
int main()
{
int i, j;
char key[256];
char source[256];
char destination[256];
int operation;
FILE fp;
/ Initial the Vigenere table /
for(i = 0; i < CHARSUM; i++)
for(j = 0; j < CHARSUM; j++)
table[i][j] = MINCHAR + (i + j) % CHARSUM;
printf("please choose one operation code:\n");
printf("1 Encode; 2 Decode; Others Exit\n");
scanf("%d", &operation);
fflush(stdin);
switch (operation)
{
case 1:
printf("please input the key code:\n");
gets(key);
fflush(stdin);
printf("please input the source code you want to encode:\n");
gets(source);
fflush(stdin);
encode(key, source, destination);
printf("after encode is: \n");
printf("%s\n", destination);
fp=fopen("keytxt", "w");
fprintf(fp, "%s", key);
fclose(fp);
fp=fopen("sourcetxt", "w");
fprintf(fp, "%s", source);
fclose(fp);
fp=fopen("destinationtxt", "w");
fprintf( fp, "%s",destination);
fclose(fp);
break;
case 2:
printf("please input the key code:\n");
gets(key);
fflush(stdin);
printf("please input the source code you want to decode:\n");
gets(source);
fflush(stdin);
decode(key, source, destination);
printf("after decode is: \n");
printf("%s\n", destination);
fp=fopen("keytxt", "w");
fprintf(fp, "%s", key);
fclose(fp);
fp=fopen("sourcetxt", "w");
fprintf(fp, "%s", source);
fclose(fp);
fp=fopen("destinationtxt", "w");
fprintf(fp, "%s", destination);
fclose(fp);
break;
default:
return 0;
}
return 0;
}
int encode(char key, char source, char dest)
{
char tempSource = source;
char tempKey = key;
char tempDest = dest;
do
{
tempDest = table[(tempKey) - MINCHAR][(tempSource) - MINCHAR];
tempDest++;
if (!((++tempKey)))
tempKey = key;
} while(tempSource++);
dest[strlen(source)] = '\0';
return 1;
}
int decode(char key, char source, char dest)
{
char tempSource = source;
char tempKey = key;
char tempDest = dest;
char offset;
do
{
offset = (tempSource) - (tempKey);
offset = offset >= 0 offset : offset + CHARSUM;
tempDest = MINCHAR + offset;
tempDest++;
if (!((++tempKey)))
tempKey = key;
} while(++tempSource);
dest[strlen(source)] = '\0';
return 1;
}
以上就是关于用matlab 对图像进行算术编码和解码的程序!!!求大神指点全部的内容,包括:用matlab 对图像进行算术编码和解码的程序!!!求大神指点、C语言编程mp3的播放,求解怎么实现通过调用mp3的解码源程序、写一个维吉尼亚加密和解码的C语言程序,具体要求如下。不要用GOTO, 不要有MAGIC NUMBER。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)