我試著·寫了jpeg壓縮的代碼,結果一直顯示 未定義函式或變數 'bits'。用了cd命令·顯示在·當前·檔案夾·目錄,想·請教一下·是·哪里出錯了。
代碼如下:I=imread('風景.bmp.bmp');
I=rgb2gray(I);
syms quality ; syms nargchik ;
syms bit;
y=im2jpeg(I,quality,99);
x=jpeg2im(y);
figure (1),imshow(I);
figure (2),imshow(x);
m檔案:
im2jpeg.m:
function y= im2jpeg(I,quality,~)
narginchk(1,3);
if ndims(I)~=2 ||~isreal(I) ||~isnumeric(I) ||~isinteger(I)
error('The input image must be unsiged integer.');
end
if nargin <3
bits=8;
end
if bits<0||bits>16
error('The input image must have 1 to 16 bits/pixel.');
end
if nargin <2
quality=1;
end
if quality<=0
error('Input parameter QUALITY must be greater than zero.');
end
m =[16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56
14 17 22 29 51 87 80 62
18 22 37 56 68 109 103 77
24 35 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99];
order=[ 1 9 2 3 10 17 25 18 11 4 5 12 19 26 33 41
34 27 20 13 6 7 14 21 28 35 42 49 57 50 43 36
29 22 15 8 16 23 30 37 44 51 58 59 52 45 38 31
24 32 39 46 53 60 61 54 47 40 48 55 62 63 56 64 ] ;
[xm,xn]=size(I);
I=double(I)-2^(round(bits)-1);
t=dctmtx(8);
y=blockproc(I,[8 8],'P1 * x * P2',t,t');
y=blockkproc(y,[8 8],'round(x. /P1)',m);
y=im2col(y,[8 8],'distinct');
xb=size(y,2);
y=y(order,:);
eob=max(y(:))+1;
r=zeros(numel(y)+size(y,2),1);
count=0;
for j=1:xb
i=find(y(:,j),1,'last');
if isempty(i)
i=0;
end
p=count+1;
q=p+i;
r(p:q)=[y(1:i,j);eob];
count=count+i+1;
end
r((count+1):end)=[];
y =struct;
y.size =uint16([xm xn]);
y.bits =uint16(bits);
y.quality =uint16(quality * 100);
y.huffman =mat2huff(r);
m檔案:
jpeg2im.m
function x= jpeg2im(y)
narginchk(1,1);
m=[16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56
14 17 22 29 51 87 80 62
18 22 37 56 68 109 103 77
24 35 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99];
order=[1 9 2 3 10 17 25 18 11 4 5 12 19 26 33
41 34 27 20 13 6 7 14 21 28 35 42 49 57 50
43 36 29 22 15 8 16 23 30 37 44 51 58 59 52
45 38 31 24 32 39 46 53 60 61 54 47 40 48 55 ];
rev = order;
for k=1:length(order)
rev(k)= find(order== k);
end
m=double(y.quality) /100 * m;
xb= double(y.numblock);
sz= double(y.size);
xn=sz(2);
xm=sz(1);
x= huff2mat(y.huffman);
eob= max(x(:));
z=zeros(64,xb); k=1;
for j=1:xb
for i=1:64
if x(k)==eob
k=k+1; break;
else
z(i,j)=x(k);
k=k+1;
end
end
end
z= z(rev,:);
x= col2im(z,[8 8],[xm,xn],'distinct');
x= blockproc(x,[8 8],'x.*P1',m);
t= dctmtx(8);
x= blockproc(x,[8 8],'P1 * x * P2',t',t);
x= x+ double2(2^(y.bits-1));
if y.bits<=8
x=uint8(x);
else
x=uint16(x);
end
未定義函式或變數 'bits'。
出錯 im2jpeg (line 9)
if bits<0||bits>16
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/27673.html
標籤:新手樂園
上一篇:指標陣列做main函式引數的問題
