利用DCT进行jpg压缩;对图像进行二值化【matlab源码】

背景及原理讲解链接: https://blog.csdn.net/qq_44394952/article/details/122586662?spm=1001.2014.3001.5502.

DCT

RGB=imread('1.jpg');
R=RGB(:,:,1);
G=RGB(:,:,2);
B=RGB(:,:,3);
figure,imshow(RGB),title('原图');     %输出原图

%RGB->YUV
Y=0.299*double(R)+0.587*double(G)+0.114*double(B);
U=-0.169*double(R)-0.3316*double(G)+0.5*double(B);  
V=0.5*double(R)-0.4186*double(G)-0.0813*double(B);
figure,imshow(uint8(Y)),title('灰度图'); ;    %输出灰度图

T=dctmtx(8);
BY=blkproc(Y,[8 8],'P1*x*P2',T,T');  
BU=blkproc(U,[8 8],'P1*x*P2',T,T');  
BV=blkproc(V,[8 8],'P1*x*P2',T,T');  

 a=  [16 11 10 16 24 40 51 61;  
      12 12 14 19 26 58 60 55;  
      14 13 16 24 40 57 69 55;  
      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;];  %量化值  
 
  b=[ 17 18 24 47 99 99 99 99;  
      18 21 26 66 99 99 99 99;  
      24 26 56 99 99 99 99 99;  
      47 66 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;]; 
   
BY2=blkproc(BY,[8 8],'x./P1',a); 
BU2=blkproc(BU,[8 8],'x./P1',b);  
BV2=blkproc(BV,[8 8],'x./P1',b); 

BY3=int8(BY2); 
BU3=int8(BU2);  
BV3=int8(BV2);  

BY4=blkproc(double(BY3),[8 8],'x.*P1',a);
BU4=blkproc(double(BU3),[8 8],'x.*P1',b); 
BV4=blkproc(double(BV3),[8 8],'x.*P1',b);

mask=[  
         1  1  1  1  1  0  0  0;  
         1  1  1  1  0  0  0  0;  
         1  1  1  0  0  0  0  0;  
         1  1  0  0  0  0  0  0;  
         1  0  0  0  0  0  0  0;  
         0  0  0  0  0  0  0  0;  
         0  0  0  0  0  0  0  0;  
         0  0  0  0  0  0  0  0;];  
     
BY5=blkproc(BY4,[8 8],'P1.*x',mask); 
BU5=blkproc(BU4,[8 8],'P1.*x',mask);  
BV5=blkproc(BV4,[8 8],'P1.*x',mask); 

YI=blkproc(double(BY5),[8 8],'P1*x*P2',T',T);
UI=blkproc(double(BU5),[8 8],'P1*x*P2',T',T);  
VI=blkproc(double(BV5),[8 8],'P1*x*P2',T',T);  
figure,imshow(uint8(YI)),title('DCT变换量化后的黑白图'); ;   %输出DCT变换和量化后的YUV图像  
    
%YUV->RGB
RI=YI-0.001*UI+1.402*VI;  
GI=YI-0.344*UI-0.714*VI;  
BI=YI+1.772*UI+0.001*VI;  
    
RGBI=cat(3,RI,GI,BI)
RGBI=uint8(RGBI); 


I = RGBI;
[M,N] = size(I);
 I1 = I(:);
 P = zeros(1,256);
 %获取各符号的概率;
 for i = 0:255
     P(i+1) = length(find(I1 == i))/(M*N);
 end

 k = 0:255;
 dict = huffmandict(k,P); %生成字典
 enco = huffmanenco(I1,dict); %编码
 deco = huffmandeco(enco,dict); %解码
 Ide = col2im(deco,[M,N],[32,115],'distinct'); %把向量重新转换成图像块;
figure,imshow(uint8(Ide)),title('解码后黑白图像');   %输出解码后的图像  


二值化

I = imread('1.jpg');
figure,imshow(I),title('原图');
[x,y]=size(I);                % 求出图象大小
b=double(I);                  
                        
z0=max(max(max(I)));                   % 求出图象中最大的灰度
z1=min(min(min(I)));                   % 最小的灰度 

T=(z0+z1)/2;                      % T赋初值,为最大值和最小值的平均值
TT=0;                             %TT赋初值
S0=0.0; n0=0.0;                       %为计算灰度大于阈值的元素的灰度总值、个数赋值
S1=0.0; n1=0.0;                       %为计算灰度小于阈值的元素的灰度总值、个数赋值
allow=50;                       % 新旧阈值的允许接近程度
d=abs(T-TT);
count=0;                         % 记录几次循环
while(d>=allow)                 % 迭代最佳阈值分割算法,求最佳阈值
    count=count+1;
    for i=1:x,
        for j=1:y,
            if I(i,j)>=T,
                S0=S0+b(i,j);     
                n0=n0+1;
            end
            if I(i,j)<T,
                S1=S1+b(i,j);
                n1=n1+1;
            end
        end
    end 
    T0=S0/n0;
    T1=S1/n1;
    TT=(T0+T1)/2;            %在阈值T下,迭代阈值的计算过程
    d=abs(T-TT);
    T=TT;
end

i1=im2bw(I,T/255);          % 图像在最佳阈值下二值化
figure,imshow(i1),title('最佳阈值二值化');
                    

GUI可视化

function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI_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 GUI

% Last Modified by GUIDE v2.5 14-Jul-2017 12:35:54

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 GUI is made visible.
function GUI_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 GUI (see VARARGIN)

% Choose default command line output for GUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = GUI_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;


 
 

% % DCT 
% --- Executes on button press in DCT.
function DCT_Callback(hObject, eventdata, handles)

% hObject    handle to DCT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
RGB=imread('1.jpg');
R=RGB(:,:,1);
G=RGB(:,:,2);
B=RGB(:,:,3);
axes(handles.axes1);
imshow(RGB);

Y=0.299*double(R)+0.587*double(G)+0.114*double(B);
U=-0.169*double(R)-0.3316*double(G)+0.5*double(B);  
V=0.5*double(R)-0.4186*double(G)-0.0813*double(B);

axes(handles.axes2);
imshow(uint8(Y));

T=dctmtx(8);
BY=blkproc(Y,[8 8],'P1*x*P2',T,T');  
BU=blkproc(U,[8 8],'P1*x*P2',T,T');  
BV=blkproc(V,[8 8],'P1*x*P2',T,T');  

a=   [16 11 10 16 24 40 51 61;  
      12 12 14 19 26 58 60 55;  
      14 13 16 24 40 57 69 55;  
      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;]; %量化值  
 
  b=[ 17 18 24 47 99 99 99 99;  
      18 21 26 66 99 99 99 99;  
      24 26 56 99 99 99 99 99;  
      47 66 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;  
      99 99 99 99 99 99 99 99;]; 
   
BY2=blkproc(BY,[8 8],'x./P1',a); 
BU2=blkproc(BU,[8 8],'x./P1',b);  
BV2=blkproc(BV,[8 8],'x./P1',b); 

BY3=int8(BY2); 
BU3=int8(BU2);  
BV3=int8(BV2);  

BY4=blkproc(double(BY3),[8 8],'x.*P1',a);
BU4=blkproc(double(BU3),[8 8],'x.*P1',b); 
BV4=blkproc(double(BV3),[8 8],'x.*P1',b);

mask=[  
         1  1  1  1  1  0  0  0;  
         1  1  1  1  0  0  0  0;  
         1  1  1  0  0  0  0  0;  
         1  1  0  0  0  0  0  0;  
         1  0  0  0  0  0  0  0;  
         0  0  0  0  0  0  0  0;  
         0  0  0  0  0  0  0  0;  
         0  0  0  0  0  0  0  0;];  
     
BY5=blkproc(BY4,[8 8],'P1.*x',mask); 
BU5=blkproc(BU4,[8 8],'P1.*x',mask);  
BV5=blkproc(BV4,[8 8],'P1.*x',mask); 

YI=blkproc(double(BY5),[8 8],'P1*x*P2',T',T);
UI=blkproc(double(BU5),[8 8],'P1*x*P2',T',T);  
VI=blkproc(double(BV5),[8 8],'P1*x*P2',T',T); 

axes(handles.axes3);
imshow(uint8(YI));   %经过DCT变换和量化后灰度图
    

RI=YI-0.001*UI+1.402*VI;  
GI=YI-0.344*UI-0.714*VI;  
BI=YI+1.772*UI+0.001*VI;  
    
RGBI=cat(3,RI,GI,BI); 
RGBI=uint8(RGBI); 

axes(handles.axes4);
imshow(RGBI); 

I = RGBI;
[M,N] = size(I);
 I1 = I(:);
 P = zeros(1,256);
 %获取各符号的概率;
 for i = 0:255
     P(i+1) = length(find(I1 == i))/(M*N);
 end

 k = 0:255;
 dict = huffmandict(k,P); %生成字典
 enco = huffmanenco(I1,dict); %编码
 deco = huffmandeco(enco,dict); %解码
 Ide = col2im(deco,[M,N],[32,115],'distinct'); %把向量重新转换成图像块;
 
%  axes(handles.axes4);
%  imshow(uint8(Ide)); %解码后的黑白图
 
CR=length(enco)/length(deco); % 压缩比

 set(handles.edit3,'string',CR);




 
 % %  二值化
 % --- Executes on button press in 二值化.
function xingcheng_Callback(hObject, eventdata, handles)
% hObject    handle to xingcheng (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
I=imread('1.jpg');
image1=im2bw(I,0.43);
axes(handles.axes5);
imshow(I);

X=image1(:);  %X为新建的二值图像的一维数据组
x=1:1:length(X);   % 编码之前的图像数据
plot(handles.axes6,x,X(x));

% 游程编码程序段
 j=1;
 image4(1)=1;
for z=1:1:(length(X)-1) 
if (X(z)==X(z+1))
   image4(j)=image4(j)+1;
else data(j)=X(z); 
j=j+1;
image4(j)=1;
end
end
data(j)=X(length(X));    
image4length=length(image4); % 计算游程编码后的所占字节数
H=length(x);
%y=1:1:image4length ;  

CR1=H/image4length;   % 压缩比

% 游程编码解压
i=1;
 for m=1:image4length
    for n=1:1:image4(m);
        rec_image(i)=data(m);
        i=i+1;
     end
    end
 u=1:1:length(rec_image);       % 解压后的图像数据
plot(handles.axes7,u,rec_image(u));

rec2_image=reshape(rec_image,32,115); % 重建二维图像数组
axes(handles.axes8);
imshow(rec2_image);

 set(handles.edit4,'string',CR1);





function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.

function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.


function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值