Matlab如何从轴保存修改后的图像

时间:2014-10-13 20:34:36

标签: matlab

这是我到目前为止所拥有的 正如你可以看到我现在正在使用保存然后我保存原始图像如何在应用其中一个过滤器(如黑白)后保存修改后的图像。

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

% Last Modified by GUIDE v2.5 13-Oct-2014 13:17:45

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

% Choose default command line output for testme

handles.fileLoaded = 0;
handles.fileLoaded2 = 0;


%-----------------------------------------------------------------------
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


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


% --- Executes on button press in loadbutton.
function loadbutton_Callback(hObject, eventdata, handles)
% hObject    handle to loadbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global im im2
[path,user_cance]=imgetfile();
if user_cance
    msgbox(sprintf('Error'),'Error','Error');
    return
end
im=imread(path);

im=im2double(im); %converts to double
im2=im; %for backup process :)
axes(handles.axes1);
imshow(im);
axes(handles.axes2);

hist(im);















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

% --- Executes on button press in resetbutton.
function resetbutton_Callback(hObject, eventdata, handles)
% hObject    handle to resetbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global im2
axes(handles.axes1);
imshow(im2);
axes(handles.axes2);

hist(im2);

% --- Executes on button press in negativebutton.
function negativebutton_Callback(hObject, eventdata, handles)
% hObject    handle to negativebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global im
imblack=im;
imblack=1-im;
axes(handles.axes1);
imshow(imblack);
axes(handles.axes2);

hist(imblack);


% --- Executes on button press in greybutton.
function greybutton_Callback(hObject, eventdata, handles)
% hObject    handle to greybutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global im
imgray=(im(:,:,1)+im(:,:,2)+im(:,:,2))/3;
axes(handles.axes1);
imshow(imgray);
axes(handles.axes2);

hist(imgray);

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

global im2
val=0.5*get(hObject,'Value')-0.5;
imbright=im2+val;
axes(handles.axes1);
imshow(imbright);
axes(handles.axes2);

hist(imbright);

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in savebutton.
function savebutton_Callback(hObject, eventdata, handles)
% hObject    handle to savebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[file,path]=uiputfile({'*.bmp','BMP'},'Save Image As');
f=getframe(handles.axes1); 
[x,map]=frame2im(f); 
imwrite(x,fullfile(path, file),'bmp');


% --- Executes on button press in black.
function black_Callback(hObject, eventdata, handles)
% hObject    handle to black (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
global im
xb=im;
b=im2bw(xb);
imshow(b);

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点,但也许一个好方法是在图的应用程序数据中存储图像(在应用过滤器之后)。您可以使用guidata

执行此操作

在每个过滤器之后存储图像;

% --- Executes on button press in black.
function black_Callback(hObject, eventdata, handles)
% hObject    handle to black (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
global im
xb=im;
b=im2bw(xb);
imshow(b);
% Store the image
handles.image = b;
guidata(hObject, handles);

然后在savebutton_Callback中,只需参阅handles.image;