MATLAB预测编码老出错
I=imread('car.png','png');
x=double(I);
y=LPCencode(x);
xx=LPCdecode(y);
%显示预测误差值
figure(1);
subplot(121);
imshow(I);
subplot(122);
Imshow(mat2gray(y));
%计算均方差误差,因为是无损编码,那么erms应该为0
e=double(x)-double(xx);
[m, n]=size(e);
erms=sqrt(sum(e(:).^2)/(m*n));
figure(2);
Subplot(121);
[h, f]=hist(x(:));
bar(f, h, 'k');
%显示预测误差的直方图
subplot(122);
[h, f]=hist(y(:));
bar(f, h,'k');
%编码器
%LPCencode函数用一维无损预测编码压缩图像x,a为预测系数,如果a默认,则默认a=1,就是前值预测。
function y=LPCencode(x, a)
error(nargchk(1, 2, nargin));
if nargin<2
a=1;
end
x=double(x);
[m, n]=size(x);
p=zeros(m, n); %存放预测值
xs=x;
zc=zeros(m, 1);
for i=1:length(a)
xs=[zc xs(:, 1:end-1)];
p=p+a(i)*xs;
end
y=x-round(p);
%解码器
%LPCdecode函数的解码程序,与编码程序用的是同一个预测器
function x=LPCdecode(y, a)
error(nargchk(1, 2, nargin));
if nargin<2
a=1;
end
a=a(end: -1: 1);
[m, n]=size(y);
order=length(a);
a=repmat(a, m, 1);
x=zeros(m, n+order);
for i=1:n
ii=i+order;
x(:, ii)=y(:, i)+round(sum(a(:, order: -1: 1).*x(:, (ii-1): -1:(ii-order)), 2));
end
x=x(:, order+1: end);
end
end
二进制数组操作的数组维度必须匹配。
出错 car5>LPCencode (line 40)
y=x-round(p);
出错 car5 (line 3)
y=LPCencode(x);
仔细检查输出的x与LPCencode( )m函数中的维度匹配不