Image doesn't display correctly when using global variable
1 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153850/image.jpeg)
What happens when I do call the variable as global:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153851/image.jpeg)
Any suggestions/help would be very appreciated. Thanks! -A guy in need of help!
0 个评论
回答(1 个)
Ben11
2014-7-31
编辑:Ben11
2014-7-31
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!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!