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

📄 perform_blsgsm_denoising.m

📁 A toolbox for the non-local means algorithm
💻 M
📖 第 1 页 / 共 3 页
字号:
% Javier Portilla, Universidad de Granada, Jan 2004[Ny,Nx,Nc] = size(im);im_ext = zeros(Ny+2*By,Nx+2*Bx,Nc);im_ext(By+1:Ny+By,Bx+1:Nx+Bx,:) = im;if strcmp(type,'mirror'),    im_ext(1:By,:,:) = im_ext(2*By:-1:By+1,:,:);    im_ext(:,1:Bx,:) = im_ext(:,2*Bx:-1:Bx+1,:);    im_ext(Ny+1+By:Ny+2*By,:,:) = im_ext(Ny+By:-1:Ny+1,:,:);    im_ext(:,Nx+1+Bx:Nx+2*Bx,:) = im_ext(:,Nx+Bx:-1:Nx+1,:);    im_ext(1:By,1:Bx,:) = im_ext(2*By:-1:By+1,2*Bx:-1:Bx+1,:);    im_ext(Ny+1+By:Ny+2*By,Nx+1+Bx:Nx+2*Bx,:) = im_ext(Ny+By:-1:Ny+1,Nx+Bx:-1:Nx+1,:);    im_ext(1:By,Nx+1+Bx:Nx+2*Bx,:) = im_ext(2*By:-1:By+1,Nx+Bx:-1:Nx+1,:);    im_ext(Ny+1+By:Ny+2*By,1:Bx,:) = im_ext(Ny+By:-1:Ny+1,2*Bx:-1:Bx+1,:);elseif strcmp(type,'mirror_nr'),                im_ext(1:By,:,:) = im_ext(2*By+1:-1:By+2,:,:);    im_ext(:,1:Bx,:) = im_ext(:,2*Bx+1:-1:Bx+2,:);    im_ext(Ny+1+By:Ny+2*By,:,:) = im_ext(Ny+By-1:-1:Ny,:,:);    im_ext(:,Nx+1+Bx:Nx+2*Bx,:) = im_ext(:,Nx+Bx-1:-1:Nx,:);    im_ext(1:By,1:Bx,:) = im_ext(2*By+1:-1:By+2,2*Bx+1:-1:Bx+2,:);    im_ext(Ny+1+By:Ny+2*By,Nx+1+Bx:Nx+2*Bx,:) = im_ext(Ny+By-1:-1:Ny,Nx+Bx-1:-1:Nx,:);    im_ext(1:By,Nx+1+Bx:Nx+2*Bx,:) = im_ext(2*By+1:-1:By+2,Nx+Bx-1:-1:Nx,:);    im_ext(Ny+1+By:Ny+2*By,1:Bx,:) = im_ext(Ny+By-1:-1:Ny,2*Bx+1:-1:Bx+2,:);        elseif strcmp(type,'circular'),                    im_ext(1:By,:,:) =  im_ext(Ny+1:Ny+By,:,:);    im_ext(:,1:Bx,:) = im_ext(:,Nx+1:Nx+Bx,:);    im_ext(Ny+1+By:Ny+2*By,:,:) = im_ext(By+1:2*By,:,:);    im_ext(:,Nx+1+Bx:Nx+2*Bx,:) = im_ext(:,Bx+1:2*Bx,:);    im_ext(1:By,1:Bx,:) = im_ext(Ny+1:Ny+By,Nx+1:Nx+Bx,:);    im_ext(Ny+1+By:Ny+2*By,Nx+1+Bx:Nx+2*Bx,:) = im_ext(By+1:2*By,Bx+1:2*Bx,:);    im_ext(1:By,Nx+1+Bx:Nx+2*Bx,:) = im_ext(Ny+1:Ny+By,Bx+1:2*Bx,:);    im_ext(Ny+1+By:Ny+2*By,1:Bx,:) = im_ext(By+1:2*By,Nx+1:Nx+Bx,:);   end            % M = MEAN2(MTX)%% Sample mean of a matrix.function res = mean2(mtx)res = mean(mean(mtx));function [pyr,pind] = buildWUpyr(im, Nsc, daub_order);% [PYR, INDICES] = buildWUpyr(IM, HEIGHT, DAUB_ORDER)% % Construct a separable undecimated orthonormal QMF/wavelet pyramid% on matrix (or vector) IM.% % HEIGHT specifies the number of pyramid levels to build. Default% is maxPyrHt(IM,FILT).  You can also specify 'auto' to use this value.% % DAUB_ORDER: specifies the order of the daubechies wavelet filter used% % PYR is a vector containing the N pyramid subbands, ordered from fine% to coarse.  INDICES is an Nx2 matrix containing the sizes of% each subband. % JPM, Univ. de Granada, 03/2003, based on Rice Wavelet Toolbox % function "mrdwt" and on Matlab Pyrtools from Eero Simoncelli.if Nsc < 1,    display('Error: Number of scales must be >=1.');else,   Nor = 3; % fixed number of orientations;h = daubcqf(daub_order);[lpr,yh] = mrdwt(im, h, Nsc+1); % performs the decomposition[Ny,Nx] = size(im);% Reorganize the output, forcing the same format as with buildFullSFpyr2pyr = [];pind = zeros((Nsc+1)*Nor+2,2);    % Room for a "virtual" high pass residual, for compatibilitynband = 1;for nsc = 1:Nsc+1,    for nor = 1:Nor,        nband = nband + 1;        band = yh(:,(nband-2)*Nx+1:(nband-1)*Nx);        sh = (daub_order/2 - 1)*2^nsc;  % approximate phase compensation        if nor == 1,        % horizontal            band = shift(band, [sh 2^(nsc-1)]);        elseif nor == 2,    % vertical            band = shift(band, [2^(nsc-1) sh]);        else            band = shift(band, [sh sh]);    % diagonal        end            if nsc>2,            band = real(shrink(band,2^(nsc-2)));  % The low freq. bands are shrunk in the freq. domain        end        pyr = [pyr; vector(band)];        pind(nband,:) = size(band);    end    end            band = lpr;band = shrink(band,2^Nsc);pyr = [pyr; vector(band)];pind(nband+1,:) = size(band);endfunction [h_0,h_1] = daubcqf(N,TYPE)%    [h_0,h_1] = daubcqf(N,TYPE); %%    Function computes the Daubechies' scaling and wavelet filters%    (normalized to sqrt(2)).%%    Input: %       N    : Length of filter (must be even)%       TYPE : Optional parameter that distinguishes the minimum phase,%              maximum phase and mid-phase solutions ('min', 'max', or%              'mid'). If no argument is specified, the minimum phase%              solution is used.%%    Output: %       h_0 : Minimal phase Daubechies' scaling filter %       h_1 : Minimal phase Daubechies' wavelet filter %%    Example:%       N = 4;%       TYPE = 'min';%       [h_0,h_1] = daubcqf(N,TYPE)%       h_0 = 0.4830 0.8365 0.2241 -0.1294%       h_1 = 0.1294 0.2241 -0.8365 0.4830%%    Reference: "Orthonormal Bases of Compactly Supported Wavelets",%                CPAM, Oct.89 %%File Name: daubcqf.m%Last Modification Date: 01/02/96	15:12:57%Current Version: daubcqf.m	2.4%File Creation Date: 10/10/88%Author: Ramesh Gopinath  <ramesh@dsp.rice.edu>%%Copyright (c) 2000 RICE UNIVERSITY. All rights reserved.%Created by Ramesh Gopinath, Department of ECE, Rice University. %%This software is distributed and licensed to you on a non-exclusive %basis, free-of-charge. Redistribution and use in source and binary forms, %with or without modification, are permitted provided that the following %conditions are met:%%1. Redistribution of source code must retain the above copyright notice, %   this list of conditions and the following disclaimer.%2. Redistribution in binary form must reproduce the above copyright notice, %   this list of conditions and the following disclaimer in the %   documentation and/or other materials provided with the distribution.%3. All advertising materials mentioning features or use of this software %   must display the following acknowledgment: This product includes %   software developed by Rice University, Houston, Texas and its contributors.%4. Neither the name of the University nor the names of its contributors %   may be used to endorse or promote products derived from this software %   without specific prior written permission.%%THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON, TEXAS, %AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, %BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS %FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RICE UNIVERSITY %OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, %EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, %PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; %OR BUSINESS INTERRUPTIONS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, %WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR %OTHERWISE), PRODUCT LIABILITY, OR OTHERWISE ARISING IN ANY WAY OUT OF THE %USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%%For information on commercial licenses, contact Rice University's Office of %Technology Transfer at techtran@rice.edu or (713) 348-6173if(nargin < 2),  TYPE = 'min';end;if(rem(N,2) ~= 0),  error('No Daubechies filter exists for ODD length');end;K = N/2;a = 1;p = 1;q = 1;h_0 = [1 1];for j  = 1:K-1,  a = -a * 0.25 * (j + K - 1)/j;  h_0 = [0 h_0] + [h_0 0];  p = [0 -p] + [p 0];  p = [0 -p] + [p 0];  q = [0 q 0] + a*p;end;q = sort(roots(q));qt = q(1:K-1);if TYPE=='mid',  if rem(K,2)==1,      qt = q([1:4:N-2 2:4:N-2]);  else    qt = q([1 4:4:K-1 5:4:K-1 N-3:-4:K N-4:-4:K]);  end;end;h_0 = conv(h_0,real(poly(qt)));h_0 = sqrt(2)*h_0/sum(h_0); 	%Normalize to sqrt(2);if(TYPE=='max'),  h_0 = fliplr(h_0);end;if(abs(sum(h_0 .^ 2))-1 > 1e-4)   error('Numerically unstable for this value of "N".');end;h_1 = rot90(h_0,2);h_1(1:2:N)=-h_1(1:2:N);% [RES] = shift(MTX, OFFSET)% % Circular shift 2D matrix samples by OFFSET (a [Y,X] 2-vector),% such that  RES(POS) = MTX(POS-OFFSET).function res = shift(mtx, offset)dims = size(mtx);offset = mod(-offset,dims);res = [ mtx(offset(1)+1:dims(1), offset(2)+1:dims(2)),  ...          mtx(offset(1)+1:dims(1), 1:offset(2));        ...        mtx(1:offset(1), offset(2)+1:dims(2)),          ...	  mtx(1:offset(1), 1:offset(2)) ];  % [VEC] = vector(MTX)% % Pack elements of MTX into a column vector.  Same as VEC = MTX(:)% Previously named "vectorize" (changed to avoid overlap with Matlab's% "vectorize" function).function vec = vector(mtx)vec = mtx(:);function ts = shrink(t,f)%	im_shr = shrink(im0, f)%% It shrinks (spatially) an image into a factor f% in each dimension. It does it by cropping the% Fourier transform of the image.% JPM, 5/1/95.% Revised so it can work also with exponents of 3 factors: JPM 5/2003[my,mx] = size(t);T = fftshift(fft2(t))/f^2;Ts = zeros(my/f,mx/f);cy = ceil(my/2);cx = ceil(mx/2);evenmy = (my/2==floor(my/2));evenmx = (mx/2==floor(mx/2));y1 = cy + 2*evenmy - floor(my/(2*f));y2 = cy + floor(my/(2*f));x1 = cx + 2*evenmx - floor(mx/(2*f));x2 = cx + floor(mx/(2*f));Ts(1+evenmy:my/f,1+evenmx:mx/f)=T(y1:y2,x1:x2);if evenmy,    Ts(1+evenmy:my/f,1)=(T(y1:y2,x1-1)+T(y1:y2,x2+1))/2;endif evenmx,    Ts(1,1+evenmx:mx/f)=(T(y1-1,x1:x2)+T(y2+1,x1:x2))/2;endif evenmy & evenmx,    Ts(1,1)=(T(y1-1,x1-1)+T(y1-1,x2+1)+T(y2+1,x1-1)+T(y2+1,x2+1))/4;end    Ts = fftshift(Ts);Ts = shift(Ts, [1 1] - [evenmy evenmx]);ts = ifft2(Ts);% RES = pyrBand(PYR, INDICES, BAND_NUM)%% Access a subband from a pyramid (gaussian, laplacian, QMF/wavelet, % or steerable).  Subbands are numbered consecutively, from finest% (highest spatial frequency) to coarsest (lowest spatial frequency).% Eero Simoncelli, 6/96.function res =  pyrBand(pyr, pind, band)res = reshape( pyr(pyrBandIndices(pind,band)), pind(band,1), pind(band,2) );% RES = pyrBandIndices(INDICES, BAND_NUM)%% Return indices for accessing a subband from a pyramid % (gaussian, laplacian, QMF/wavelet, steerable).% Eero Simoncelli, 6/96.function indices =  pyrBandIndices(pind,band)if ((band > size(pind,1)) | (band < 1))  error(sprintf('BAND_NUM must be between 1 and number of pyramid bands (%d).', ...      size(pind,1)));endif (size(pind,2) ~= 2)  error('INDICES must be an Nx2 matrix indicating the size of the pyramid subbands');endind = 1;for l=1:band-1  ind = ind + prod(pind(l,:));endindices = ind:ind+prod(pind(band,:))-1;function res = reconWUpyr(pyr, pind, daub_order);% RES = reconWUpyr(PYR, INDICES, DAUB_ORDER) % Reconstruct image from its separable undecimated orthonormal QMF/wavelet pyramid% representation, as created by buildWUpyr.%% PYR is a vector containing the N pyramid subbands, ordered from fine% to coarse.  INDICES is an Nx2 matrix containing the sizes of% each subband.  % % DAUB_ORDER: specifies the order of the daubechies wavelet filter used % JPM, Univ. de Granada, 03/2003, based on Rice Wavelet Toolbox % functions "mrdwt" and  "mirdwt", and on Matlab Pyrtools from Eero Simoncelli.Nor = 3;Nsc = (size(pind,1)-2)/Nor-1;h = daubcqf(daub_order);yh = [];nband = 1;last = prod(pind(1,:)); % empty "high pass residual band" for compatibility with full steerpyr 2for nsc = 1:Nsc+1,  % The number of scales corresponds to the number of pyramid levels (also for compatibility)    for nor = 1:Nor,        nband = nband +1;        first = last + 1;        last = first + prod(pind(nband,:)) - 1;        band = pyrBand(pyr,pind,nband);        sh = (daub_order/2 - 1)*2^nsc;  % approximate phase compensation        if nsc > 2,            band = expand(band, 2^(nsc-2));        end           if nor == 1,        % horizontal            band = shift(band, [-sh -2^(nsc-1)]);        elseif nor == 2,    % vertical            band = shift(band, [-2^(nsc-1) -sh]);        else            band = shift(band, [-sh -sh]);    % diagonal        end            yh = [yh band];        end    end    nband = nband + 1;band = pyrBand(pyr,pind,nband);lpr = expand(band,2^Nsc);res= mirdwt(lpr,yh,h,Nsc+1);function te = expand(t,f)%	im_exp = expand(im0, f)%% It expands (spatially) an image into a factor f% in each dimension. It does it filling in with zeros% the expanded Fourier domain.% JPM, 5/1/95.% Revised so it can work also with exponents of 3 factors: JPM 5/2003[my mx] = size(t);my = f*my;mx = f*mx;Te = zeros(my,mx);T = f^2*fftshift(fft2(t));cy = ceil(my/2);cx = ceil(mx/2);evenmy = (my/2==floor(my/2));evenmx = (mx/2==floor(mx/2));y1 = cy + 2*evenmy - floor(my/(2*f));y2 = cy + floor(my/(2*f));x1 = cx + 2*evenmx - floor(mx/(2*f));x2 = cx + floor(mx/(2*f));Te(y1:y2,x1:x2)=T(1+evenmy:my/f,1+evenmx:mx/f);if evenmy,    Te(y1-1,x1:x2)=T(1,2:mx/f)/2;    Te(y2+1,x1:x2)=((T(1,mx/f:-1:2)/2)').';endif evenmx,    Te(y1:y2,x1-1)=T(2:my/f,1)/2;    Te(y1:y2,x2+1)=((T(my/f:-1:2,1)/2)').';endif evenmx & evenmy,    esq=T(1,1)/4;    Te(y1-1,x1-1)=esq;    Te(y1-1,x2+1)=esq;    Te(y2+1,x1-1)=esq;    Te(y2+1,x2+1)=esq;end    Te=fftshift(Te);Te = shift(Te, [1 1] - [evenmy evenmx]);te=ifft2(Te);% RES = innerProd(MTX)%% Compute (MTX' * MTX) efficiently (i.e., without copying the matrix)function res = innerProd(mtx)%% NOTE: THIS CODE SHOULD NOT BE USED! (MEX FILE IS CALLED INSTEAD)% fprintf(1,'WARNING: You should compile the MEX version of "innerProd.c",\n         found in the MEX subdirectory of matlabPyrTools, and put it in your matlab path.  It is MUCH faster.\n');res = mtx' * mtx;

⌨️ 快捷键说明

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