记录一下问题 unit8格式最多只能255要操作图片矩阵一般先转换成double格式
.m文件
function[a,b,B] = en(I)
global a;
global b;
global B;
[M N] = size(I);
I = double(I);
high = 0;
low = 0;
sum1 = 0;
avgI = sum(sum(I))/numel(I);
for i=1 : M
for j = 1:N
if(I(i,j) >= avgI)
B(i,j) = 1;
high = high + I(i,j);
sum1 = sum1 + 1;
else
B(i,j) = 0;
low = low + I(i,j);
end
end
end
a =round(high / sum1);
b =round(low / (numel(I) - sum1));
B
end
%压缩并解压缩图片代码:
%% BTCen
I = imread('liftingbody.png');
I2 = I;
C = Divide_into_N_block(I2,4);
res = mat2cell(I , 4*ones(512/4 , 1), 4*ones(512/4 , 1));
for k = 1:(128*128)
[a,b,B] = en(res{k});
for i = 1 : 4
for j = 1 : 4
if(B(i,j) == 1)
C{k}(i,j) = a;
else
C{k}(i,j) = b;
end
end
end
end
I3 = cell2mat(C);
imshow(uint8(I3));
figure,imshow(I2);