⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image_compress.m

📁 对一幅二维图像采用预测编码(DPCM)、小波变换等不同方法进行压缩的代码。
💻 M
字号:
function varargout = image_compress(varargin)
% IMAGE_COMPRESS M-file for image_compress.fig
%      IMAGE_COMPRESS, by itself, creates a new IMAGE_COMPRESS or raises the existing
%      singleton*.
%
%      H = IMAGE_COMPRESS returns the handle to a new IMAGE_COMPRESS or the handle to
%      the existing singleton*.
%
%      IMAGE_COMPRESS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in IMAGE_COMPRESS.M with the given input arguments.
%
%      IMAGE_COMPRESS('Property','Value',...) creates a new IMAGE_COMPRESS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before image_compress_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to image_compress_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help image_compress

% Last Modified by GUIDE v2.5 31-Aug-2006 15:20:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @image_compress_OpeningFcn, ...
                   'gui_OutputFcn',  @image_compress_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before image_compress is made visible.
function image_compress_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to image_compress (see VARARGIN)
handles.cameraman=imshow('cameraman.tif');
handles.current_data=handles.cameraman;
% Choose default command line output for image_compress
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes image_compress wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = image_compress_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
% --------------------------------------------------------------------function Untitled_1_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_2_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)clear;
clc;
load gu2;
subplot(1,2,1);imshow(I);title('原始图象');
J=dct2(I);
J(abs(j)<100)=0;
K=idct2(J);
subplot(1,2,2);imshow(K,[0 255]);title('dct2重构图象');% --------------------------------------------------------------------function Untitled_3_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_3 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)clc
load gu2;
subplot(1,2,1);imshow(I);title('原始图象');
[m,n]=size(I);
J1=dctmtx(n);
I=double(I);
J=J1*I*J1';
J(abs(j)<100)=0;
K=J1'*J*J1;
subplot(1,2,2);imshow(K,[0 255]);title('dctmtx重构图象');% --------------------------------------------------------------------function Untitled_4_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_4 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_5_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_5 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)%A DPCM COMPRESSION CODE
%Using format:X(n)=X(n-1)
clc
clear
load gu2;
X=double(I);
[m,n]=size(X);
Y=X;
c=zeros(m,n);
for i=1:m
    for j=2:n
        Y(i,j)=X(i,j-1);
        c(i,j)=X(i,j)-Y(i,j);
    end
end
rc=abs(c);
rc=reshape(rc,m*n,1);
rc=sort(rc);
bit=8;
for i=1:(2^(bit-1))
    q=round(m*n*i/(2^(bit-1)));
    d(i)=rc(q);
end
e=zeros(m,n);
for i=2:(2^(bit-1))
    [k1,k2]=find(abs(c)<=d(i)&abs(c)>d(i-1));
    q1=length(k1);
    for j=1:q1
        if(c(k1(j),k2(j))>=0) e(k1(j),k2(j))=(d(i)+d(i-1))/2;
        else e(k1(j),k2(j))=-(d(i)+d(i-1))/2;
        end
    end
end
[k1,k2]=find(abs(c)<=d(1));
q1=length(k1);
for j=1:q1
    if(c(k1(j),k2(j))>=0) e(k1(j),k2(j))=d(1)/2;
    else e(k1(j),k2(j))=-d(1)/2;
    end
end
e(1,1)=X(1,1);
e(1,2)=X(1,2);
e=round(e*(2^(bit-1)))/(2^(bit-1));
%decode
cX=zeros(m,n);
cX(1,1)=e(1,1);
cX(1,2)=e(1,2);
for i=1:m
    if(i~=1)
      cX(i,1)=e(i,1)+X(i-1,1);     
    end
    for j=2:n
        cX(i,j)=e(i,j)+cX(i,j-1);
    end
end
subplot(121);imshow(X,[0 255]);%colormap(map);
title('原始图像');
subplot(122);imshow(cX,[0 255]);%colormap(map);
title('前值预测压缩图像');
per1=norm(X)
per2=norm(cX)
err=norm(cX-X)% --------------------------------------------------------------------function Untitled_7_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_7 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)clc
clear
load gu2;
X=double(I);
[m,n]=size(X);
a1=0.5;
a2=0.5;
Y=zeros(m,n);
c=zeros(m,n);
for i=1:m
    for j=3:n
        Y(i,j)=a1*X(i,j-1)+a2*X(i,j-2);
        c(i,j)=X(i,j)-Y(i,j);
    end
end
for i=2:m
    Y(i,1)=a1*X(i-1,1)+a2*X(i-1,2);
    c(i,1)=X(i,1)-Y(i,1);
    Y(i,2)=a1*X(i-1,2)+a2*X(i-1,1);
    c(i,2)=X(i,2)-Y(i,2);
end
c(1,1)=0;c(1,2)=0;
rc=abs(c);
rc=reshape(rc,m*n,1);
rc=sort(rc);
bit=8;
for i=1:(2^(bit-1))
    q=round(m*n*i/(2^(bit-1)));
    d(i)=rc(q);
end
e=zeros(m,n);
for i=2:(2^(bit-1))
    [k1,k2]=find(abs(c)<=d(i)&abs(c)>d(i-1));
    q1=length(k1);
    for j=1:q1
        if(c(k1(j),k2(j))>=0) e(k1(j),k2(j))=(d(i)+d(i-1))/2;
        else e(k1(j),k2(j))=-(d(i)+d(i-1))/2;
        end
    end
end
[k1,k2]=find(abs(c)<=d(1));
q1=length(k1);
for j=1:q1
    if(c(k1(j),k2(j))>=0) e(k1(j),k2(j))=d(1)/2;
    else e(k1(j),k2(j))=-d(1)/2;
    end
end
e(1,1)=X(1,1);
e(1,2)=X(1,2);
e=round(e*(2^(bit-1)))/(2^(bit-1));
%decode
cX=zeros(m,n);
cX(1,1)=e(1,1);
cX(1,2)=e(1,2);
for i=1:m
    if(i~=1)
      cX(i,1)=e(i,1)+a1*X(i-1,1)+a2*X(i-1,2);
      cX(i,2)=e(i,2)+a1*X(i-1,2)+a2*X(i-1,1);
    end
    for j=3:n
        cX(i,j)=e(i,j)+a1*cX(i,j-1)+a2*cX(i,j-2);
    end
end
subplot(121);imshow(X,[0 255]);%colormap(map);
title('原始图像');
subplot(122);imshow(cX,[0 255]);%colormap(map);
title('一维预测压缩图像');
per1=norm(X)
per2=norm(cX)
err=norm(cX-X)% --------------------------------------------------------------------function Untitled_8_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_8 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_9_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_9 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)clc
clear
load gu2;
X=double(I);
[c,l]=wavedec2(X,2,'haar');
cA1=appcoef2(c,l,'haar',1);
cH1=detcoef2('h',c,l,1);
cD1=detcoef2('d',c,l,1);
cV1=detcoef2('v',c,l,1);
A1=wrcoef2('a',c,l,'haar',1);
H1=wrcoef2('h',c,l,'haar',1);
D1=wrcoef2('d',c,l,'haar',1);
V1=wrcoef2('v',c,l,'haar',1);
c1=[A1 H1;V1 D1];
subplot(121);imshow(X,[0 255]);%colormap(map);
title('原始图像');
ca1=wcodemat(cA1,440,'mat',0);
ca1=0.5*ca1;
subplot(122);imshow(ca1,[0 255]);%colormap(map);
title('haar小波压缩图像');% --------------------------------------------------------------------function Untitled_10_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_10 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)clc
clear
load gu2;
X=double(I);
[c,l]=wavedec2(X,2,'db4');
cA1=appcoef2(c,l,'db4',1);
cH1=detcoef2('h',c,l,1);
cD1=detcoef2('d',c,l,1);
cV1=detcoef2('v',c,l,1);
A1=wrcoef2('a',c,l,'db4',1);
H1=wrcoef2('h',c,l,'db4',1);
D1=wrcoef2('d',c,l,'db4',1);
V1=wrcoef2('v',c,l,'db4',1);
c1=[A1 H1;V1 D1];
subplot(121);imshow(X,[0 255]);%colormap(map);
title('原始图像');
ca1=wcodemat(cA1,440,'mat',0);
ca1=0.5*ca1;
subplot(122);imshow(ca1,[0 255]);%colormap(map);
title('daubechies小波压缩图像');
% --------------------------------------------------------------------function Untitled_11_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_11 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)clc
clear
load gu2;
X=double(I);
[m,n]=size(X);
r1=0;r2=0;r3=0;
for i=1:m
    r1=r1+X(i,1)^2+X(i,2)^2;
    r2=r2+X(i,1)*X(i,2);
    for j=3:n
        r1=r1+X(i,j)^2;
        r2=r2+X(i,j)*X(i,j-1);
        r3=r3+X(i,j)*X(i,j-2);
    end
end
r1=r1/(m*n);r2=r2/(m*n);r3=r3/(m*n);
a1=r2*(r1-r3)/(r1^2-r2^2);
a2=(r3*r1-r2^2)/(r1^2-r2^2);
Y=zeros(m,n);
c=zeros(m,n);
for i=1:m
    for j=3:n
        Y(i,j)=a1*X(i,j-1)+a2*X(i,j-2);
        c(i,j)=X(i,j)-Y(i,j);
    end
end
for i=2:m
    Y(i,1)=a1*X(i-1,1)+a2*X(i-1,2);
    c(i,1)=X(i,1)-Y(i,1);
    Y(i,2)=a1*X(i-1,2)+a2*X(i-1,1);
    c(i,2)=X(i,2)-Y(i,2);
end
c(1,1)=0;c(1,2)=0;
rc=abs(c);
rc=reshape(rc,m*n,1);
rc=sort(rc);
bit=8;
for i=1:(2^(bit-1))
    q=round(m*n*i/(2^(bit-1)));
    d(i)=rc(q);
end
e=zeros(m,n);
for i=2:(2^(bit-1))
    [k1,k2]=find(abs(c)<=d(i)&abs(c)>d(i-1));
    q1=length(k1);
    for j=1:q1
        if(c(k1(j),k2(j))>=0) e(k1(j),k2(j))=(d(i)+d(i-1))/2;
        else e(k1(j),k2(j))=-(d(i)+d(i-1))/2;
        end
    end
end
[k1,k2]=find(abs(c)<=d(1));
q1=length(k1);
for j=1:q1
    if(c(k1(j),k2(j))>=0) e(k1(j),k2(j))=d(1)/2;
    else e(k1(j),k2(j))=-d(1)/2;
    end
end
e(1,1)=X(1,1);
e(1,2)=X(1,2);
e=round(e*(2^(bit-1)))/(2^(bit-1));
%decode
cX=zeros(m,n);
cX(1,1)=e(1,1);
cX(1,2)=e(1,2);
for i=1:m
    if(i~=1)
      cX(i,1)=e(i,1)+a1*X(i-1,1)+a2*X(i-1,2);
      cX(i,2)=e(i,2)+a1*X(i-1,2)+a2*X(i-1,1);
    end
    for j=3:n
        cX(i,j)=e(i,j)+a1*cX(i,j-1)+a2*cX(i,j-2);
    end
end
subplot(121);imshow(X,[0 255]);%colormap(map);
title('原始图像');
subplot(122);imshow(cX,[0 255]);%colormap(map);
title('自适应预测压缩图像');
per1=norm(X)
per2=norm(cX)
err=norm(cX-X)
% --------------------------------------------------------------------function Untitled_13_Callback(hObject, eventdata, handles)% hObject    handle to Untitled_13 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -