【课题】基于matlab GUI DCT图像去噪【含Matlab源码 614期】

一、简介

1 DCT算法:
DCT变换的全称是离散余弦变换(Discrete Cosine Transform),离散余弦变换相当于一个长度大概是它两倍的离散傅里叶变换,这个离散傅里叶变换是对一个实偶函数进行的。通过数字信号处理的学习我们知道实函数的傅立叶变换获得的频谱大多是复数,而偶函数的傅立叶变换结果是实函数。以此为基础,使信号函数成为偶函数,去掉频谱函数的虚部,是余弦变换的特点之一。它可以将将一组光强数据转换成频率数据,以便得知强度变化的情形。若对高频的数据做些修饰,再转回原来形式的数据时,显然与原始数据有些差异,但是人类的眼睛却是不容易辨认出来。压缩时,将原始图像数据分成8*8数据单元矩阵,例如亮度值的第一个矩阵内。
在这里插入图片描述
在这里插入图片描述

2 DCT产生的工程背景:

视频信号的频谱线在0-6MHz范围内,而且1幅视频图像内包含的大多数为低频频谱线,只在占图像区域比例很低的图像边缘的视频信号中才含有高频的谱线。因此,在视频信号数字处理时,可根据频谱因素分配比特数:对包含信息量大的低频谱区域分配较多的比特数,对包含信息量低的高频 谱区域分配较少的比特数,而图像质量并没有可察觉的损伤,达到码率压缩的目的。然而,这一切要在低熵(Entropy)值的情况下,才能达到有效的编码。能否对一串数据进行有效的编码,取决于每个数据出现的概率。每个数据出现的概率差别大,就表明熵值低, 可以对该串数据进行高效编码。反之,出现的概率差别小,熵值高,则不能进行高效编码。视频信号的数字化是在规定的取样频率下由A/D转换器对视频电平转换而来的,每个像素的视频信号幅度随着每层的时间而周期性地变化。每个像素的平均信息量的总和为总平均信息量,即熵值。由于每个视频电平发生几乎具有相等的概率,所以视频信号的熵值很高。 熵值是一个定义码率压缩率的参数,视频图像的压缩率依赖于视频信号的熵值,在多数情况下视频信号为高熵值,要进行高效编码,就要将高熵值变为低熵值。怎样变成低熵值呢?这就需要分析视频频谱的特点。大多数情况下,视频频谱的幅度随着频率的升高而降低。其中 低频频谱在几乎相等的概率下获得0到最高的电平。与此相对照,高频频谱通常得到的是低电平及稀少的高电平。显然,低频频谱具有较高的熵值,高频频谱具有较低的熵值。据此,可对视频的低频分量和高频分量分别处理,获得高频的压缩值。

自从Ahmed和Rao于1974年给出了离散余弦变换(DCT)的定义以来,离散余弦变换(DCT)与改进型离散余弦变换(MDCT)就成为广泛应用于信号处理和图像处理特别是用于图像压缩和语音压缩编解码的重要工具和技术,一直是国际学术界和高科技产业界的研究热点。现在的很多图像和视频编码标准(如MPEG-1 , MEPG-2 ,MEPG-4中的第二部分)都要求实现整数的8×8 的DCT和IDCT,而MDCT 和IMDCT 则主要被应用于音频信号的编解码中(如MPEG-1 ,MEPG-2 和AC-]等标准的音频编码部分)。正是由于这类变换被广泛采用,对于这类变换的快速算法的研究才显得尤为重要。特别是针对特定的应用条件下的快速算法的研究对于提高整个系统的性能表现有很大帮助。
由上面的引用可见,码率压缩基于变换编码和熵值编码两种算法。前者用于降低熵值,后者将数据变为可降低比特数的有效编码方式。在MPEG标准中,变换编码采用的是DCT,变换过程本身虽然并不产生码率压缩作用,但是变换后的频率系数却非常有利于码率压缩。 实际上压缩数字视频信号的整个过程分为块取样、DCT、量化、编码4个主要过程进行-----首先在时间域将原始图像分成N(水平)×N(垂直)取样块,根据需要可选择4×4、4×8、8×8、8×16、16×16等块,这些取样的像素块代表了原图像帧各像素的灰度值,其范围在139-163之间,并依序送入DCT编码器,以便将取样块由时间域转换为频率域的DCT系数块。DCT系统的转换分别在每个取样块中进行,这些块中每个取样是数字化后的值,表示一场中对应像素的视频信号幅度值

3 离散余弦变换的实现:

实现DCT的方法很多,最直接的是根据DCT的定义来计算。以二维8xSDCT为例,需要作4096次乘法和3584次加法。这种算法的实现需要巨大的计算量,不具有实用价值。在应用中,需要寻找快速而又精确的算法。较为常用的方法是利用DCT的可拆分特性,同样以二维8xSDCT为例,先进行8行一维DCT需要64xS次乘法和56xS次加法,再进行8列一维DCT要64xS次乘法和56xS次加法,共需要64x8xZ=1024次乘法和56x8xZ=896次加法,计算量大为减少。

除此之外,DCT还有很多公开的快速算法。快速算法主要是通过减少运算次数而减少运算时间,这对于设计快速的硬件系统非常有效。二维DCT的快速算法则一般采用行列分离DCT算法,即转换为两次一维变换,其间通过转置矩阵连接。最为经典和常用的快速算法是由Arai等人于1988年提出的AAN算法以及Loeffier等人于1989年提出的LLM算法。但是,由于行列分离DCT算法能够重复使用一维变换结构,因此在实际实现上,尤其在硬件上比二维直接计算算法更有优势。

二、源代码

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

% Last Modified by GUIDE v2.5 07-Dec-2009 19:54:09

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @imDCT_OpeningFcn, ...
                   'gui_OutputFcn',  @imDCT_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 imDCT is made visible.
function imDCT_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 imDCT (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = imDCT_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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
ImgPath=get(handles.edit1,'String');
CompressRate=uint8(fix(str2num(get(handles.edit2,'String'))));
if CompressRate>64
    CompressRate=64;
end
if CompressRate<1
    CompressRate=1;
end
set(handles.edit2,'String',num2str(CompressRate));
img=imread(ImgPath);
axes(handles.axes1);
imshow(img);
set(handles.text7,'Visible','off');
set(handles.text7,'String','处理状态:正在处理中...');
set(handles.text7,'Visible','on');
pause(0.1);
Block=ceil(size(img)/8);
ImgDCT=zeros(Block*8);
ImgRestoreTemp=zeros(Block*8);
ImgRestore=zeros(size(img));
ImgBlock=zeros(8);
DCTBlock=zeros(8);
vector=zeros(1,64);
for ii=1:Block(1)
    for jj=1:Block(2)
        ImgBlock(:,:)=zeros(8);
        if ii==Block(1)
            if jj==Block(2)
                ImgBlock(1:size(img,1)-(ii-1)*8,1:size(img,2)-(jj-1)*8)=img((ii-1)*8+1:size(img,1),(jj-1)*8+1:size(img,2));
            else
                ImgBlock(1:size(img,1)-(ii-1)*8,:)=img((ii-1)*8+1:size(img,1),(jj-1)*8+1:jj*8);
            end
        else
            if jj==Block(2)
                ImgBlock(:,1:size(img,2)-(jj-1)*8)=img((ii-1)*8+1:ii*8,(jj-1)*8+1:size(img,2));
            else
                ImgBlock(:,:)=img((ii-1)*8+1:ii*8,(jj-1)*8+1:jj*8);
            end
        end
        DCTBlock(:,:)=dct2(ImgBlock(:,:));
        if CompressRate~=64
            vector(1:64)=m2v(DCTBlock(:,:));
            vector(CompressRate+1:64)=0;
            DCTBlock(:,:)=v2m(vector(1:64));
        end
        ImgDCT((ii-1)*8+1:ii*8,(jj-1)*8+1:jj*8)=DCTBlock(:,:);
    end
end

三、运行结果

在这里插入图片描述

四、备注

完整代码或者代写添加QQ 1564658423

猜你喜欢

转载自blog.csdn.net/TIQCmatlab/article/details/115149263