用matlab 对图像进行算术编码和解码的程序!!!求大神指点

用matlab 对图像进行算术编码和解码的程序!!!求大神指点,第1张

close allclear allclc %关闭所有图形窗口,清除工作空间所有变量,清空命令行

I=imread('lena.bmp')%待编码的矩阵

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+range*low

numberhigh=tmp+range*high

end

end

fprintf('算兄纯蠢术编码范围下限为%16.15f\n\n',numberlow)

fprintf('算术编码范围上限为%16.15f\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

h = StartC + g* K

l = Endc + g* K1

其具体程序如下:

const Ca = 0.2Ce = 0.3

Ci = 0.2Co = 0.2

Cu = 0.1

var

Form1: TForm1

s: string

StartC, EndC: Extended

implementation

{$R *.dfm}

procedure ConvertTo(s: stringvar StartC, EndC: Extended){将字符串变为数值}

var n, i: integer

c: char

g: Extended

begin

StartC := 0

EndC := 1

n := Strlen(Pchar(s))

for i := 1 to n do

begin

c := s[i]

g := EndC - StartC

case C of

'a'带碰辩:

begin

EndC :=StartC + g * Ca

StartC := StartC + g * 0

end

'e':

begin

EndC := StartC + g * (Ca + Ce)

StartC := StartC + g * Ca

end

'i':

begin

EndC := StartC + g * (Ca + Ce + Ci)

StartC := StartC + g * (Ca + Ce)

end

'o':

begin

EndC := StartC + g * (Ca + Ce + Ci + Co)

StartC := StartC + g * (Ca + Ce + Ci)

end

'u':

begin

EndC := StartC + g * (Ca + Ce + Ci + Co + Cu)

StartC := StartC + g * (Ca + Ce + Ci + Co)

end

else

begin

Showmessage(' 输入的字符串有误 ')

exit

end

end

end

end

procedure NemuricalToStr(var s: Stringvar StartC, EndC: Extended)

{将数值转换为字符串}

const eps = -1e-5

begin

if StartC-0.2 <-eps then

if (EndC- 0.2<= -eps) and (EndC >StartC) then

begin

StartC := StartC / 0.2

EndC := EndC / 0.2

s := s + 'a'

if (StartC <>0) or (EndC <>1) then

NemuricaltoStr(s,StartC,EndC)

end

if (StartC- 0.2 >= eps) and (StartC-0.5 <-eps) then

if (EndC-0.5<= -eps) and (EndC>StartC) then

begin

StartC := StartC - 0.2

EndC := EndC - 0.2

StartC := StartC / 0.3

EndC := EndC /蠢缺 0.3

s := s + 'e'

if (StartC <>0) or (EndC <>1) then

NemuricaltoStr(s,StartC,EndC)

end

if (StartC- 0.5>= eps) and (StartC- 0.7<-eps) then

if (EndC-0.7<= -eps) and (EndC>StartC) then

begin

StartC := StartC - 0.5

EndC := EndC - 0.5

StartC := StartC /吵物 0.2

EndC := EndC / 0.2

s := s + 'i'

if (StartC <>0) or (EndC <>1) then

NemuricaltoStr(s,StartC,EndC)

end

if (StartC-0.7 >= eps) and (StartC-0.9 <-eps) then

if (EndC-0.9<=-eps) and (EndC>StartC) then

begin

StartC := StartC - 0.7

EndC := EndC - 0.7

StartC := StartC / 0.2

EndC := EndC / 0.2

s := s + 'o'

if (StartC <>0) or (EndC <>1) then

NemuricaltoStr(s,StartC,EndC)

end

if (StartC -0.9>=eps) and (StartC-1 <-eps) then

if (EndC-1<= -eps) and (EndC>StartC) then

begin

StartC := StartC - 0.9

EndC := EndC - 0.9

StartC := StartC / 0.1

EndC := EndC / 0.1

s := s + 'u'

if (StartC <>0) or (EndC <>1) then

NemuricaltoStr(s,StartC,EndC)

end

end

procedure TForm1.Button1Click(Sender: TObject)

begin

s := Edit1.Text

ConvertTo(s,StartC,EndC)

Edit2.Text := FloattoStr(StartC)

Edit3.Text := FloattoStr(EndC)

end

procedure TForm1.Button2Click(Sender: TObject)

begin

s := ''

StartC := StrToFloat(Edit2.Text)

EndC := StrtoFloat(Edit3.Text)

NemuricalToStr(s,StartC,Endc)

Edit1.Text := s

end

end.

给你一段程序,自己研究下吧!

clc

clear

close all

%定义HufData/戚伍Len为全局变量的结构体

global HufData

global Len

disp('计算机正在准备输出哈夫曼编码结果,请耐心等待……')

%原始码字的灰度

a=imread('kids.tif')

%分区画出原始图像和灰度直方图

figure

subplot(1,2,1)

imshow(a)

%取消坐标轴和边框

axis off

box off

title('MATLAB自带图像','fontsize',13)

subplot(1,2,2)

axis off

box off

imhist(a)

title('图像灰度直方图','fontsize',13)

%图像的灰度统计

GrayStatistics=imhist(a)

GrayStatistics=GrayStatistics'

GrayRatioo=GrayStatistics/sum(GrayStatistics)

GrayRatioNO=find(GrayRatioo~=0)

Len=length(GrayRatioNO)

%初始化灰度集,防止系统随即赋予其垃圾值

GrayRatio=ones(1,Len)

for i=1:Len

GrayRatio(i)=GrayRatioo(i)

end

GrayRatio=abs(sort(-GrayRatio))

%将图像灰度概率赋予结构体

for i=1:Len

HufData(i).value=GrayRatio(i)

end

% 哈夫曼编码/霍夫曼编码

HuffmanCode(Len)

%输出码字高耐或

zippedHuffman=1

for i=1:Len

tmpData=HufData(i).code

str=''

for j=1:length(tmpData)

str=strcat(str,num2str(tmpData(j)))

zippedHuffman=zippedHuffman+1

end

disp(strcat('a',num2str(i),'= ',str))

end

i

%计算计算机一共输出多少个哈夫曼编码/霍夫曼编码

zippedHuffman

%计算在删去0灰度级压缩之前的原始图像字节容量

unzipped_delete=i*8

%计算压缩比率

ratio_delete=zippedHuffman/unzipped_delete

%计算图像的压缩比率

ad=num2str(ratio_delete*100)

str2=strcat(ad,'%')

disp(strcat('哈夫曼编码压缩比率','= ',str2))

%子程序:哈夫曼编码/霍夫曼编码函数HuffmanCode.m

function HuffmanCode(OriginSize)

global HufData

global Len

for i=1:Len

%%霍夫曼编码树左边纪录为1

HufData(i).left=1

%%霍夫曼编码树右边纪录为0

HufData(i).right=0

%%输出码初始化为0

HufData(i).code=[]

%%排序列表初始化

SortList(i).symbol=i

SortList(i).value=HufData(i).value

end

%初始化原始消息数目

newsymbol=OriginSize

for n=OriginSize:-1:2

%将N个消息进行排序

SortList=sortdata(SortList,n)

%将最后两个出现亩裤概率最小的消息合成一个消息

newsymbol=newsymbol+1

HufData(newsymbol).value=SortList(n-1).value+SortList(n).value

HufData(newsymbol).left=SortList(n-1).symbol

HufData(newsymbol).right=SortList(n).symbol

%将消息添加到列队的最后,为N-1个消息重新排序作好准备

SortList(n-1).symbol=newsymbol

SortList(n-1).value=HufData(newsymbol).value

end

%遍历霍夫曼树,获得霍夫曼编码/哈夫曼编码

visit(newsymbol,Len,[])

end

%子程序:冒泡排序法函数sortdata.m

function reData=sortdata(SortList,n)

%根据消息概率进行排序

for k=n:-1:2

for j=1:k-1

min=SortList(j).value

sbl=SortList(j).symbol

if(min<SortList(j+1).value)

SortList(j).value=SortList(j+1).value

SortList(j+1).value=min

SortList(j).symbol=SortList(j+1).symbol

SortList(j+1).symbol=sbl

end

end

end

reData=SortList

end

%子程序:遍历哈夫曼编码/霍夫曼编码树搜索函数visit.m

function visit(node,n,ocode)

global HufData

if node<=n

%如果没有哈夫曼编码/霍夫曼编码树的子接点直接输出原始码,这里为空码([])

HufData(node).code=ocode

else

if(HufData(node).left>0)

%遍历左分支接点输出1,这里采用子函数嵌套调用

ocode1=[ocode 1]

visit(HufData(node).left,n,ocode1)

end

if(HufData(node).right>0)

%遍历右分支接点输出0,这里采用子函数嵌套调用

ocode2=[ocode 0]

visit(HufData(node).right,n,ocode2)

end

end

end


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12408693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存