axis automatically resize to image

4 views (last 30 days)
robert arnold
robert arnold on 7 May 2020
Commented: Image Analyst on 7 May 2020
How to I make my axis automatically resize to fit any image? I am building a GUI
  4 Comments
robert arnold
robert arnold on 7 May 2020
function varargout = final4(varargin)
% FINAL4 MATLAB code for final4.fig
% FINAL4, by itself, creates a new FINAL4 or raises the existing
% singleton*.
%
% H = FINAL4 returns the handle to a new FINAL4 or the handle to
% the existing singleton*.
%
% FINAL4('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FINAL4.M with the given input arguments.
%
% FINAL4('Property','Value',...) creates a new FINAL4 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before final4_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to final4_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 final4
% Last Modified by GUIDE v2.5 07-May-2020 10:49:35
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @final4_OpeningFcn, ...
'gui_OutputFcn', @final4_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 final4 is made visible.
function final4_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 final4 (see VARARGIN)
% Choose default command line output for final4
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes final4 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = final4_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 slider movement.
function redslider_Callback(hObject, eventdata, handles)
% hObject handle to redslider (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
p = handles.p;
p(:,:,1) = get(handle.redslider,'Value')+128 + p(:,:,1) + get(handle.contrast,'Value')+128 + p(:,:,1);
p(:,:,2) = get(handle.greenslider,'Value')+128 + p(:,:,2) + get(handle.contrast,'Value')+128 + p(:,:,2);
p(:,:,3) = get(handle.blueslider,'Value')+128 + p(:,:,3) + get(handle.contrast,'Value')+128 + p(:,:,3);
imshow(p)
% --- Executes during object creation, after setting all properties.
function redslider_CreateFcn(hObject, eventdata, handles)
% hObject handle to redslider (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 slider movement.
function blueslider_Callback(hObject, eventdata, handles)
% hObject handle to blueslider (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
% --- Executes during object creation, after setting all properties.
function blueslider_CreateFcn(hObject, eventdata, handles)
% hObject handle to blueslider (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 slider movement.
function greenslider_Callback(hObject, eventdata, handles)
% hObject handle to greenslider (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
% --- Executes during object creation, after setting all properties.
function greenslider_CreateFcn(hObject, eventdata, handles)
% hObject handle to greenslider (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 slider movement.
function contrast_Callback(hObject, eventdata, handles)
% hObject handle to contrast (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
%p(:,:,1) = get(handle.redslider,'Value')+128 + p(:,:,1) + get(handle.contrast,'Value')+128 + p(:,:,1);
global im2
val = get(hObject, 'value') + get(handle.redslider,'Value');
c = im2 + val;
imshow(c);
% --- Executes during object creation, after setting all properties.
function contrast_CreateFcn(hObject, eventdata, handles)
% hObject handle to contrast (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 load.
function load_Callback(hObject, eventdata, handles)
% hObject handle to load (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
%import image to matlab
[filename, pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
fullname=[pathname, filename];
im=imread(fullname);
im = im2double(im);
imshow(im);
im2 = im;
%remove axis
axis off;
%auto resize axis to fit image
axis equal;
%im(im,'parent',handles.im);
%handles.p = im; % <--- new line to save the image to the handles object
guidata(hObject, handles); % <--- new line to save the updated handles objet
Image Analyst
Image Analyst on 7 May 2020
The size of your axes control on your GUI does adjust it's size somewhat in response to the image you put into it, while maintaining the aspect ratio and zooming it as much as it can while showing the entire image with the original aspect ratio. If you don't want that, and want to zoom further, see attached demo.

Sign in to comment.

Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!