www.gusucode.com > 声音的处理有:LPC,FFT,共振峰,频谱源码程序 > siganlandsystemusingMatlab/SSUM/aliasing/image/imgaliasexpofn.m

    %
function imgaliasexpofn(action,datastruct)
if nargin < 1
	action='init';
end

% When not keep size, scaling wrong for kernel

name = mfilename;
figname = [name(1:end-2) '_fig'];
f=findobj('Tag',figname);
handles = get(f,'UserData');

switch action
	case 'help'
		display_help(figname);
	case 'init'
		reset(handles.spectrum);
		setdefaults;
	case 'load'
		handles.imagedata = load_imagedata;
		set(handles.downsample,'Value',1);
		if ~isfield(handles.imagedata, 'filenamepath')
			return;
		elseif isfield(handles,'figwin');
			try,
				close(handles.figwin);
			catch
			end
		end
		handles.original = handles.imagedata;
		make2DSpecPlot(handles.imagedata, handles);
		handles.figwin = showimage(handles.imagedata, ...
			get(handles.scale,'Value'), get(handles.inverse,'Value'));
		place_header(handles.figwin, handles);
		movegui(handles.figwin,'onscreen');
	case 'readinput'
		handles.imagedata = datastruct;
		clear datastruct
		reset(handles.spectrum);
		setdefaults;
		set(handles.downsample,'Value',1);
		handles.original = handles.imagedata;
		make2DSpecPlot(handles.imagedata,handles);
		handles.figwin = showimage(handles.imagedata, ...
			get(handles.scale,'Value'), get(handles.inverse,'Value'));
		place_header(handles.figwin, handles);
		movegui(handles.figwin,'onscreen');
	case {'scale','inverse'}
		if isfield(handles, 'imagedata')
			figure(handles.figwin);
			X = get(gca, 'XLim');
			Y = get(gca, 'YLim');
			showimage(handles.imagedata, get(handles.scale,'Value'), get(handles.inverse,'Value'), handles.figwin);
			set(gca,'XLim',X,'YLim',Y);
		end
	case {'colormap','inversecolor'}
		if isfield(handles, 'imagedata')
			figure(handles.imgaliasexpo_fig);
			axes(handles.spectrum);
			contents = get(handles.colormap,'String');
			cmap = colormap(lower(contents{get(handles.colormap,'Value')}));
			if (get(handles.inversecolor,'Value'))
				colormap(flipud(cmap));
			else
				colormap(cmap);
			end
		end
	case 'downsample'
		if isfield(handles, 'imagedata')
			contents = get(handles.downsample,'String');
			downfactor = str2num(contents{get(handles.downsample,'Value')});
			if get(handles.horizontal,'Value')
				type = 'horizontal';
			elseif get(handles.vertical,'Value')
				type = 'vertical';
			else
				type = 'kernel';
			end
			keepsize = get(handles.keepsize,'Value');
			figure(handles.figwin);
			X = get(gca, 'XLim');
			Y = get(gca, 'YLim');
			handles.imagedata_alias = downsample(handles.imagedata, ...
				downfactor, get(handles.antialias,'Value'), type, keepsize);
			make2DSpecPlot(handles.imagedata_alias, handles);
			showimage(handles.imagedata_alias, get(handles.scale,'Value'), ...
				get(handles.inverse,'Value'),handles.figwin);
			movegui(handles.figwin,'onscreen');
		end
	case 'zoom_reset'
		if isfield(handles, 'imagedata_alias')
			showimage(handles.imagedata_alias, get(handles.scale,'Value'), get(handles.inverse,'Value'), handles.figwin);
		end
	case 'plot'
		[rows,columns] = size(handles.imagedata.data);
		contents = get(handles.filtermenu,'String');
		kernel_size = str2double(get(handles.kernel_size,'String'));
		switch (lower(contents{get(handles.filtermenu,'Value')}))
			case 'median'
				% Cannot plot frequency response
			otherwise
		end
	case {'imgspecexpo','imgfilterexpo'}
		if isfield(handles,'imagedata')
			imagedata = handles.imagedata;
			switch action
				case 'imgspecexpo'
					imgspectrumexpogui(imagedata);
				case 'imgfilterexpo'
					imgfilterexpogui(imagedata);
			end
		end
	case 'save'
		save_imagedata(handles.imagedata_alias);
	case 'print'
		print_figure(f);
	case 'close'
		close_figure(f,figname(1:end-4));
		if isfield(handles, 'figwin'),
			close_figure(handles.figwin);
		end
		return;
end
set(f,'UserData',handles);

%-------------------------------------------------------------
function make2DSpecPlot(imagedata,handles)
figure(handles.imgaliasexpo_fig);
axes(handles.spectrum);
spectrum = log(abs(fftshift(fft2(imagedata.data))));
% Get rid of inf
points = isinf(spectrum);
spectrum(points) = 0;
imagesc(spectrum);
contents = get(handles.colormap,'String');
cmap = colormap(lower(contents{get(handles.colormap,'Value')}));
if (get(handles.inversecolor,'Value'))
	colormap(flipud(cmap));
else
	colormap(cmap);
end
axis image;

%-------------------------------------------------------------

function imagedata = downsample(imagedata,factor,antialias,varargin)

if nargin<4,
	type = 'kern';
	keepsize = 0;
elseif nargin<5
	type = varargin{1};
	keepsize = 0;
elseif nargin<6
	type = varargin{1};
	keepsize = varargin{2};
end

% Lowpass image
if antialias,
	imagedata.data = imresize(imagedata.data,1/factor,'bilinear',10);
	imagedata.data = imresize(imagedata.data,factor);
end

[rows,columns] = size(imagedata.data);
k = floor(factor/2);
% Create place to store values
A = zeros(rows+2*k,columns+2*k);
% Put the original image in the center
A(k+1:rows+k,k+1:columns+k) = imagedata.data;
% Four corners
A(1:k,1:k) = imagedata.data(1,1);
A(1:k,columns+k+1:columns+2*k) = imagedata.data(1,columns);
A(rows+k+1:rows+2*k, columns+k+1:columns+2*k) = ...
	imagedata.data(rows,columns);
A(rows+k+1:rows+2*k,1:k) = imagedata.data(rows,1);

% Handle the four sides
for i = 1:k
	A(i,k+1:columns+k) = imagedata.data(1,1:columns);
	A(k+1:rows+k,columns+k+i) = ...
		imagedata.data(1:rows,columns);
	A(rows+k+i,k+1:columns+k) = ...
		imagedata.data(rows,1:columns);
	A(k+1:rows+k,i) = imagedata.data(1:rows,1);
end

if keepsize
	B = zeros(rows,columns);
	switch type
		case 'horizontal'
			for i=1:rows,
				for j=1:columns,
					B(i,j) = A(i+k,floor((j-1)/factor)*factor+1+2*k);
				end
			end
		case 'vertical'
			for i=1:rows,
				for j=1:columns,
					B(i,j) = A(floor((i-1)/factor)*factor+1+2*k,j+k);
				end
			end
		case 'kernel'
			for i=1:rows,
				for j=1:columns,
					B(i,j) = A(floor((i-1)/factor)*factor+1+2*k,floor((j-1)/factor)*factor+1+2*k);
				end
			end
	end
else
	switch type
		case 'horizontal'
			B = zeros(rows,floor(columns/factor));
			for i=1:rows,
				for j=1:floor(columns/factor),
					B(i,j) = A(i+k,(j-1)*factor+1+2*k);
				end
			end
		case 'vertical'
			B = zeros(floor(rows/factor),columns);
			for i=1:floor(rows/factor),
				for j=1:columns,
					B(i,j) = A((i-1)*factor+1+2*k,j+k);
				end
			end
		case 'kernel'
			B = zeros(floor(rows/factor),floor(columns/factor));
			for i=1:floor(rows/factor),
				for j=1:floor(columns/factor),
					B(i,j) = A((i-1)*factor+1+2*k, (j-1)*factor+1+2*k);
				end
			end
	end
end

imagedata.data = B;