How to make it not to go dark?

1 view (last 30 days)
Stelios Fanourakis
Stelios Fanourakis on 28 May 2018
I use the below function:
function SliderDemo
clc
clear all
NumFrames = 15; %// Check below for dummy 4D matrix/image sequence
hFig = figure('Position',[100 100 500 500],'Units','normalized');
handles.axes1 = axes('Units','normalized','Position',[.2 .2 .6 .6]);
%// Create slider and listener object for smooth visualization
handles.SliderFrame = uicontrol('Style','slider','Position',[60 20 400 50],'Min',1,'Max',NumFrames,'Value',1,'SliderStep',[1/NumFrames 2/NumFrames],'Callback',@XSliderCallback);
handles.SliderxListener = addlistener(handles.SliderFrame,'Value','PostSet',@XListenerCallBack);
handles.Text1 = uicontrol('Style','Text','Position',[180 420 60 30],'String','Current frame');
handles.Edit1 = uicontrol('Style','Edit','Position',[250 420 100 30],'String','1');
%// Create dummy image sequence, here 4D sequence of grayscale images.
Img = DicomReader('S0000009.dcm')
isoImage = makeImIsoRGB(Img, [1,1,15], 1.5, 'cubic')
MyImage = isoImage;
MyMatrix = MyImage;
%// Use setappdata to store the image stack and in callbacks, use getappdata to retrieve it and use it. Check the docs for the calling syntax.
setappdata(hFig,'MyMatrix',MyMatrix); %// You could use %//setappdata(0,'MyMatrix',MyMatrix) to store in the base workspace.
%// Display 1st frame
imshow(MyMatrix(:,:,1))
%// IMPORTANT. Update handles structure.
guidata(hFig,handles);
%// Listener callback, executed when you drag the slider.
function XListenerCallBack
%// Retrieve handles structure. Used to let MATLAB recognize the
%// edit box, slider and all UI components.
handles = guidata(gcf);
%// Here retrieve MyMatrix using getappdata.
MyMatrix = getappdata(hFig,'MyMatrix');
%// Get current frame
CurrentFrame = round((get(handles.SliderFrame,'Value')));
set(handles.Edit1,'String',num2str(CurrentFrame));
%// Display appropriate frame.
imshow(MyMatrix(:,:,CurrentFrame),'Parent',handles.axes1);
guidata(hFig,handles);
end
%// Slider callback; executed when the slider is release or you press
%// the arrows.
function XSliderCallback(~,~)
handles = guidata(gcf);
%// Here retrieve MyMatrix using getappdata.
MyMatrix = getappdata(hFig,'MyMatrix');
CurrentFrame = round((get(handles.SliderFrame,'Value')));
set(handles.Edit1,'String',num2str(CurrentFrame));
imshow(MyMatrix(:,:,CurrentFrame),'Parent',handles.axes1);
guidata(hFig,handles);
end
end
Although, it perfectly operates, still from Frame 1 to 15 (which is the last frame), only the first and the last frames are visible. In among the transition of the frames (2-14) they appear black/ dark, like they fade and come back again at the final frame.
Where is the wrong part? How to make it appear normal throughout the transition of all frames?
  4 Comments
Stelios Fanourakis
Stelios Fanourakis on 28 May 2018
Now I get that error "Warning: Error executing listener callback for PostSet event on Value dynamic property in object of matlab.ui.control.UIControl class:"Error using SliderDemo/XListenerCallBack Too many input arguments. >> "
Jan
Jan on 28 May 2018
by the way: A clear all on top of a function is a waste of time only. Beside deleting many useful things like loaded functions and persistent variables, clear all cleans up the current workspace. But this is empty on top of a function. Therefore clear all is "cargo cult programming".
It needs a lot of time to reload the M-files from the slow hard disk after a clear all. This can costs seconds.
We cannot run your code. Therefore it is impossible for us to debug it. But it is easy for you. Simply set some breakpoints to find out, where the observed effect happens.
It is much more efficient to run imshow once only and update the cdata afterwards.
You use a nested function and share the data using guidata. This is an overkill. One of the both methods is sufficient.
But most of all consider Rik's comment: The code does not contain command, which modify a color. This means, that the source of a color change must be somewhere else in code, you did not show.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 28 May 2018
Use [] in imshow:
imshow(yourImage, []);

Stelios Fanourakis
Stelios Fanourakis on 28 May 2018
function varargout = testImageSlider(varargin)
% TESTIMAGESLIDER MATLAB code for testImageSlider.fig
% TESTIMAGESLIDER, by itself, creates a new TESTIMAGESLIDER or raises the existing
% singleton*.
%
% H = TESTIMAGESLIDER returns the handle to a new TESTIMAGESLIDER or the handle to
% the existing singleton*.
%
% TESTIMAGESLIDER('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TESTIMAGESLIDER.M with the given input arguments.
%
% TESTIMAGESLIDER('Property','Value',...) creates a new TESTIMAGESLIDER or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before testImageSlider_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to testImageSlider_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 testImageSlider
% Last Modified by GUIDE v2.5 21-Feb-2012 15:13:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @testImageSlider_OpeningFcn, ...
'gui_OutputFcn', @testImageSlider_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 testImageSlider is made visible.
function testImageSlider_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 testImageSlider (see VARARGIN)
% Choose default command line output for testImageSlider
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes testImageSlider wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = testImageSlider_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;
function numImages_Callback(hObject, eventdata, handles)
% hObject handle to numImages (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,'String') returns contents of numImages as text
% str2double(get(hObject,'String')) returns contents of numImages as a double
updateSlider(handles);
function updateSlider(handles)
% This function updates the slider to have the correct min, max, value, and
% step size
%get the current slice number which were stored in the figure axes
sliceNum = getappdata(handles.imageAxes,'sliceNum');
if(isempty(sliceNum))%may be empty if the figure has not been initialized
sliceNum = 1; %set it to a default
end
%get the number written in the text box which is the maximum number of
%images to be viewed
NumImageslice = str2double(get(handles.numImages,'String'));
%there are only NumImageslice - 1 images total, because we start at 1
step = 1/(NumImageslice - 1);
%set values for the slider bar
set(handles.imageSlider, 'Max', NumImageslice);
set(handles.imageSlider, 'Min', 1);
set(handles.imageSlider, 'SliderStep', [0 1]);
%set current value to the slice we are viewing
set(handles.imageSlider, 'Value', sliceNum);
% --- Executes during object creation, after setting all properties.
function numImages_CreateFcn(hObject, eventdata, handles)
% hObject handle to numImages (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function imageSlider_Callback(hObject, eventdata, handles)
% hObject handle to imageSlider (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
%get the current value on the slider
imageSlider_value = get(hObject,'Value');
%get the current max value from the slider
numImages = get(handles.imageSlider, 'Max');
%calculate the image number to display
imageNum = floor(imageSlider_value)...
+ (sign(imageSlider_value)...
* ( abs(imageSlider_value) - floor(imageSlider_value) )...
* numImages);
%create a string from the number which always has 2 digits
str = sprintf('%02.0f',imageNum);
%retrieve filename data from app data
ptNum = getappdata(handles.imageAxes, 'ptNum');
imageType = getappdata(handles.imageAxes, 'imageType');
filepath = getappdata(handles.imageAxes, 'filePath');
%create the filename as a cell string
filename = strcat(ptNum, '.', str, '.', imageType);
%create full path to the image
imageStr = (filename{1});
%read in image data
image = dicomread(imageStr);
%bring current axes in focus and show image
axes(handles.imageAxes);
imshow(image, []); %[] = [Imin Imax]
%store image data and slice number in axes
setappdata(handles.imageAxes, 'image', image);
setappdata(handles.imageAxes, 'sliceNum', imageSlider_value);
% --- Executes during object creation, after setting all properties.
function imageSlider_CreateFcn(hObject, eventdata, handles)
% hObject handle to imageSlider (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 loadImage.
function loadImage_Callback(hObject, eventdata, handles)
% hObject handle to loadImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global image
%intercatively choose an image to display
[fileName, isCancelled] = imgetfile();
image = DicomReader(fileName)
image = makeImIsoRGB(image, [1,1,15], 2.1, 'cubic')
%scan the filename and parse the data int a cell string array, C
C = textscan(fileName, '%s %d %s', 'delimiter', '.');
%bring axes into focus anf show image
axes(handles.imageAxes);
imshow(image(:), []); %[] = [Imin Imax]
%store relevant data in the axes
setappdata(handles.imageAxes, 'fileName', fileName);
setappdata(handles.imageAxes, 'image', image);
setappdata(handles.imageAxes, 'ptNum', C{1});
setappdata(handles.imageAxes, 'sliceNum', C{2});
setappdata(handles.imageAxes, 'imageType', C{3});
%update slider
updateSlider(handles);
This code gives me the errors "Index exceeds matrix dimensions.
Error in testImageSlider>imageSlider_Callback (line 157) imageStr = (filename{1});" and " Warning: 'slider' control cannot have a 'Value' outside of 'Min'-'Max' range Control will not be rendered until all of its parameter values are valid >> "
Any idea?
  4 Comments
Image Analyst
Image Analyst on 28 May 2018
Can't really tell. Zip up the m file, fig file, and any data files we'd need to run this and we might give it a try.
Stelios Fanourakis
Stelios Fanourakis on 28 May 2018
@Image Analyst. Here is the zip. Hope I included anything you need.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!