Clear Filters
Clear Filters

Image doesn't display correctly when using global variable

2 views (last 30 days)
I'm trying to use a global variable to store an image loaded from a .tif file. However, when I display the image, the brightness/contrast become skewed. When I remove the global declaration, the images load just fine, however that prevents me from using it in my other functions, which are used to edit/save.
Here is the relevant snippet of code:
global rgbI %<----Change this from global to not global
fname = 'test.tif';
info = imfinfo(fname);
num_images = numel(info);
for k = 1:num_images
I(:,:,k) = imread(fname, k);
rgbI(:,:,:,k) = repmat(I(:,:,k),[1 1 3]);
end
imshow(rgbI(:,:,:,150))
I'm using this in a MATLAB GUI (written using GUIDE), along with several other functions which are attached to various buttons etc. However, this issue shows up even in just this little script, outside of the GUI.
What happens when I don't call the variable as global:
What happens when I do call the variable as global:
Any suggestions/help would be very appreciated. Thanks! -A guy in need of help!

Answers (1)

Ben11
Ben11 on 31 Jul 2014
Edited: Ben11 on 31 Jul 2014
I'm not sure why using/not using global variables results in this behavior, but if you are using a GUI you could do the following:
1) instead of storing rgbI in a global variable, store it in the handles structure of your GUI, using for example:
handles.MyImage = rgbI;
guidata(hObject,handles); %IMPORTANT, used to update the structure of the GUI
2) Then you can retrieve it in other callbacks by accessing directly the handles structure, which is part of the GUI:
%Get image:
rgbI = handles.MyImage;
% Add your code
Since all your callbacks have access to the data stored in guidata (eg. the handles structure), you don't necessarily need global variables in this case.
Hope that helps!

Community Treasure Hunt

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

Start Hunting!