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

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

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

	% What about color images?
	% Resize spectrum window
	% What happens when a figure is already open with the hard coded fig number?

    switch action
		case 'help'
			display_help(figname);
        case 'init'
			setdefaults;
            movegui(f,'center');
		case 'load'
			handles.imagedata = load_imagedata;
            if ~isfield(handles.imagedata, 'filenamepath')
                return;
            end
			handles.original = handles.imagedata;
			handles.imgwin = showimage(handles.imagedata, ...
				get(handles.scale,'Value'), get(handles.inverse,'Value'),3);
			handles.specwin = make2DSpecPlot(handles);
			handles.synthdata.map = handles.imagedata.map;
			f1 = fft2(handles.imagedata.data);
            handles.phase = angle(f1);
            handles.mag = abs(f1);
			handles.synthdata.data = resynth(handles.mag, handles.phase);
			handles.resynwin = showimage(handles.synthdata, ...
				get(handles.scale,'Value'), get(handles.inverse,'Value'),4);
            set(handles.imgwin,'Name','Original','MenuBar','none');
            set(handles.resynwin,'Name','Reconstruction','MenuBar','none');
            set(handles.specwin,'Name','Spectrum','MenuBar','none');
			%set(handles.resynwin,'Colormap', get(handles.imgwin,'Colormap'));
			%place_header(f, handles);
		case {'resynthmag','resynthphase'}
			phasecontents = get(handles.phasemenu,'String');
			magcontents = get(handles.phasemenu,'String');
			switch action
				case 'resynthmag' % Keep phase, create magnitude
					magcontents = get(handles.magmenu,'String');
					handles.mag = getmagdata(handles.imagedata, ...
						magcontents{get(handles.magmenu,'Value')});
				case 'resynthphase' % Keep magnitude, create phase
					phasecontents = get(handles.phasemenu,'String');
					handles.phase = getphasedata(handles.imagedata, ...
						phasecontents{get(handles.phasemenu,'Value')});
			end
			handles.synthdata.data = resynth(handles.mag, handles.phase);
			handles.resynwin = showimage(handles.synthdata, ...
				get(handles.scale,'Value'), get(handles.inverse,'Value'),4);
			%set(handles.resynwin,'Colormap', get(handles.imgwin,'Colormap'));
		case {'scale','inverse'}
			if isfield(handles, 'imagedata')
				showimage(handles.imagedata, get(handles.scale,'Value'), get(handles.inverse,'Value'), handles.imgwin);
				showimage(handles.synthdata, get(handles.scale,'Value'), get(handles.inverse,'Value'), handles.resynwin);
			end
		case {'colormap','inversecolor'}
			if isfield(handles, 'imagedata')
                figure(handles.specwin);
                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 'save'
			save_imagedata(handles.imagedata);
		case 'print'
			print_figure(f);
		case 'close'
			close_figure(f,figname(1:end-4));
			if isfield(handles, 'imgwin'),
				close_figure(handles.imgwin);
			end
			if isfield(handles, 'specwin'),
				close_figure(handles.specwin);
			end
			if isfield(handles, 'resynwin'),
				close_figure(handles.resynwin);
			end
            return;
	end
	set(f,'UserData',handles);

%-------------------------------------------------------------
function figwin = make2DSpecPlot(handles)
	figwin = figure('Units','normalized','Position',[0.2 0.1 0.3 0.8]);
    spectrum = log(abs(fftshift(fft2(handles.imagedata.data))));
    % Get rid of inf
    points = isinf(spectrum);
    spectrum(points) = 0;
	h = subplot(2,1,1);
    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;
	title('Magnitude Spectrum');
	
	subplot(2,1,2);
	phase = angle(fftshift(fft2(handles.imagedata.data)));
	imagesc(phase);
	axis image;
	title('Phase Spectrum');


function synthdata = resynth(magdata, phasedata);
	synthdata = abs(ifft2(complex(magdata.*cos(phasedata), magdata.*sin(phasedata))));


function magdata = getmagdata(imagedata, option)
	f1 = fft2(imagedata.data);
	magdata = abs(f1);

	[m,n] = size(magdata);
	switch lower(option)
		case 'random'
			magdata = 255*rand(m,n);
		case 'other'
			otherdata = load_imagedata;
			if ~isfield(otherdata, 'filenamepath')
				return;
			end
			f2 = fft2(otherdata.data);
			mag2 = abs(f2);
			[m2,n2] = size(mag2);
			if (m2>=m & n2>=n)
				magdata = mag2(1:m,1:n);
			elseif (m2<m & n2<n)
				magdata = 255*rand(m,n);
				magdata(1:m2,1:n2) = mag2;
			elseif (m2<m & n2>=n)
				magdata = 255*rand(m,n);
				magdata(1:m2,1:n) = mag2(1:m2,1:n);
			elseif (m2>m & n2<n)
				magdata = 255*rand(m,n);
				magdata(1:m,1:n2) = mag2(1:m,1:n2);
			end
	end


function phasedata = getphasedata(imagedata, option)
	f1 = fft2(imagedata.data);
	phasedata = angle(f1);

	[m,n] = size(phasedata);
	switch lower(option)
		case 'random'
			phasedata = pi*(0.5 - rand(m,n));
		case 'other'
			otherdata = load_imagedata;
			if ~isfield(otherdata, 'filenamepath')
				return;
			end
			f2 = fft2(otherdata.data);
			a2 = angle(f2);
			[m2,n2] = size(a2);
			if (m2>=m & n2>=n)
				phasedata = a2(1:m,1:n);
			elseif (m2<m & n2<n)
				phasedata = pi*(0.5-rand(m,n));
				phasedata(1:m2,1:n2) = a2;
			elseif (m2<m & n2>=n)
				phasedata = pi*(0.5-rand(m,n));
				phasedata(1:m2,1:n) = a2(1:m2,1:n);
			elseif (m2>m & n2<n)
				phasedata = pi*(0.5-rand(m,n));
				phasedata(1:m,1:n2) = a2(1:m,1:n2);
			end
	end