How to save code for each block ?

1 次查看(过去 30 天)
nur aqila
nur aqila 2018-5-20
Hi, I have constructed a huffman coding. supposedly the coding should read the pixel number block by block and then save the huffman code in each block. I used 512x512, and the coding should read 8x8 pixel each 64x64. therefore the total block I should have is 4096 blocks. My problem right now is I don't know how to save the HuffCode for each block, and my current coding it just sums up all the huffman code from the previous block. Can someone please help me to fix this?
clc
clear all
%%%%%%%%%%%%%%%%%%%%%truncated huffman %%%%%%%%%%%%%%%%%%%%%
head = [0 2 2 2 2 2 2 2
1 0 2 2 2 2 2 2
1 1 0 2 2 2 2 2
1 1 1 0 2 2 2 2
1 1 1 1 0 2 2 2
1 1 1 1 1 0 2 2
1 1 1 1 1 1 0 2
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 ];
head_length = [1 2 3 4 5 6 7 8 8];
tail=[ 1 2 3 4 5 6 7 8 9];
i=1;
%%%%%%%%%%%%%%%%%%%%%start read image %%%%%%%%%%%%%%%%%%%%%%%
Im = double(imread('lena.bmp'));
%DPCM for the first column
for x=2:512; y=1;
PixErr(x,y)=Im(x,y)-Im(x-1,y);
end
%DPCM for first row
for y=2:512;x=1;
PixErr(x,y)=Im(x,y)-Im(x,y-1);
end
k1=0.2;k2=0.2;k3=0.6; %constant value
for x=2:512;y=2:512;
PixErr(x,y)=round(Im(x,y)-(k1*Im(x-1,y)+k2*Im(x-1,y-1)+k3*Im(x,y-1))); %k1=c,k2=b,k3=a
end
%positive integer converter%
PixErr(1,1)=0;
for x=1:512;
for y=1:512;
if PixErr(x,y)==0;
PixPos(x,y)=1;
else if PixErr(x,y)>0;
PixPos(x,y)=(PixErr(x,y)*2)+1;
else
PixPos(x,y)=abs(PixErr(x,y))*2;
end
end
end
end
%convert 2D to 1D
for block_row = 1:2
for block_col= 1:2
for pix_row = 1:8
for pix_col= 1:8
input1((pix_row-1)*8 + pix_col)= PixPos((block_row-1)*8 + pix_row,(block_col -1)*8 + pix_col);
%input1(p)= PixPos((block_row-1)*8 + pix_row,(block_col -1)*8 + pix_col);
k = length(input1);
x1 = input1;
input=reshape(x1.',1,[]);
for g=1:length(input)
%select level process%
%511-1022 %level 9
if input(g)>510 %head process%
row=9;
col=1;
small_sym=511;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
%255-510 %level 8
elseif input(g)>254
row=8;
col=1;
small_sym=255;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
%127-254 %level 7
elseif input(g)>126
row=7;
col=1;
small_sym=127;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
%63-126 %level 6
elseif input(g)>62
row=6;
col=1;
small_sym=63;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
%31-62 %level 5
elseif input(g)>30
row=5;
col=1;
small_sym=31;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
%15-30 %level 4
elseif input(g)>14
row=4;
col=1;
small_sym=15;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
%7-14 %level 3
elseif input(g)>6
row=3;
col=1;
small_sym=7;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
%3-6 %level 2
elseif input(g)>2
row=2;
col=1;
small_sym=3;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
% 1-2 %
else
row=1;
col=1;
small_sym=1;
%head%
for col=1:head_length(row);
HC(i)=head(row,col);
col=col+1;
i=i+1;
end
%tail%
%convert integer to binary 256 bit %tail process%
tail1=tail(row);
HCextra=de2bi(input(g)-small_sym, tail1,'left-msb');
for col=1:tail1;
HC(i)=HCextra(col);
col=col+1;
i=i+1;
end
end
end
end
end
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by