How to read the image data from particular axes of MATLAB GUI? - matlab

I have designed one MATLAB GUI in which there are two axes for displaying images. I am using 'imcontrast' function to adjust brightness/contrast of the images on first axes. Now, I want to store this enhanced output(brightness/contrast adjusted output) in some variable. As 'imcontrast' function does not returns the output image, so how can i get output image? or Is there any way to read image data from particular axes? I have tried with 'getimage' function but it returns the first image data contained in the Handle Graphics object (i.e previously displayed input image) and not latest brightness/contrast adjusted Image. Please help me out in saving the brightness/contrast adjusted Image given by 'imcontrast' function.

Use this -
imcontrast(gca) %// Perform imcontrast
waitfor(gcf); %// Wait for figure data to be updated with imcontrast
image_data = getimage(gcf);%// Store image data as image_data variable
How to use within a GUI
Just to showcase on how to use it, you can create a pushbutton with MATLAB-Guide and in it's callback function use this -
%// Show the image on an existing image axes of the GUI.
imshow('pout.tif') %// This image is available in MATLAB image library.
imc_figure = imcontrast(gca) %// Perform imcontrast
waitfor(imc_figure); %// Wait for the data to be updated in the current figure
image_data = getimage(gcf);%// image data stored into image_data variable
%// Open image_data on a separate figure window for verification.
%// Make sure this is the updated image.
figure,imshow(image_data)
How to use as a standalone code
Im = imread('cameraman.tif');%// This image is available in MATLAB image library.
h1 = imshow(Im)
h2 = imcontrast(gca); %// Perform imcontrast
waitfor(h2); %// Wait for figure data to be updated with imcontrast
image_data = getimage(gcf);%// Store modified image data
%// Show modified image data for verification
figure,imshow(image_data)

Related

Different Figure displayed between mapshow() & imshow() for (geo) tif file?

I'm trying to import data from a .tif file generated in ArcGIS into MATLAB. I know what the final image is supposed to look like because there is a .pdf version of the gridded data posted with the data I've downloaded (so I can check if I've done this correctly).
I have been able to load in the .tif file, but based on how I read the data in & how I plot it, the results look very different. I know that the image I want is closer to the one in imread() and plotted with imshow(). The two images genereated here and the .pdf version I'm trying to recreate are attached. I am wondering
(1) How to colourize this data and
(2) if the differences between these two images are simply the colour scale. Of note, the original .pdf is colourized, but neither my variables 'X' nor 'gridd' have a 3rd dimension that would contain colour info.
filename= 'Na_dep_2017.tif'; % My file
infoii = imfinfo(filename,'tif'); % Get info about my file
[gridd,R] = geotiffread(filename); % Load in geocoded tiff file.
gridd(gridd==str2num(infoii.GDAL_NODATA))=NaN; % Set NANs where we have no data.
[X,cmap] = imread(filename); % Load it in as an image.
X(X==str2num(infoii.GDAL_NODATA))=NaN; % Set NANs where we have no data.
figure(1)
subplot(1,2,1)
imshow(X,cmap); title('Loaded with imread(), plotted with imshow()')
subplot(1,2,2)
mapshow(double(gridd),R);
title('Loaded with geotiffread(), plotted with mapshow()')
Two dif images I make & actual image
I don't think how you load the data in actually changes the image that you get, but it does appear to influence the white-black max/min that the image is displayed with. The two images are therefore not fundamentally different. I got around using either of these methods & without worrying about colorizing the image using rgb channels, because I actually got the data surface that the image is made of by loading the data in. I used pcolor() to display the 'gridd' variable with a standard MATLAB colormap as follows.
filename= 'Na_dep_2017.tif'; % My file
infoii = imfinfo(filename,'tif'); % Get info about my file
[gridd,R] = geotiffread(filename); % Load in geocoded tiff file.
gridd(gridd==str2num(infoii.GDAL_NODATA))=NaN; % Set NANs where we have no data.
figure(1)
h=pcolor(flipud(double(gridd)));
set(h, 'EdgeColor', 'none'); % Makes tightly gridded figure not all black.
colormap(jet)

Get matrix from plot and subplot

I already had some figure that I saved before, Like this:
Now after some days, I need the original size of these images for copy these in my paper. I want to extract the main matrix of these three image for save these again with imwrite function.
I searched this problem on the internet but the people says I have to use getframe and frame2im functions. But how? I want the original matrix. Can anyone tell me how to extract the main matrix from the figured image in matlab??
Try using the following code:
imgs = findobj(gcf,'Type','image');
images = cell(1,numel(imgs));
for i = 1:numel(imgs)
images = get(imgs(i),'CData');
end
The image matrices should now be stored in the separate cells of images.

How to display image with axes using Simulink?

I want to display image with axes labelled with XData and YData as it possible with imshow.
My simulation generates new image each step.
I found no way to do this neither with Video Viewer nor with Matrix Viewer.
May it is possible to hook them somehow?

Matlab: How can I get an image handle for a matrix to be used in createMask?

I'm working with netCDF matrices representing satellite imagery. I'm trying to make a binary mask so I can analyze ROIs within the matrix. I've made an imellipse to use as the mask.
Now, the function createMask requires an image handle for the underlying data. How can I get an image handle for my data matrix? I've displayed in as a contourf plot.
The createMask() function works without an image handle as well. To get the binary mask just do like this:
imshow(Img,[]);
e = imellipse();
mask = createMask(e);
If you want to do it using an image handle, you can create an image handle by doing:
h_img = imshow(Img,[]);
You can then use this handle in the createMask() function. (But it is not really neccesary)

Problems with displaying edited pictures in a GUI created in MATLAB

I have an assignment to create a GUI using MATLAB GUIDE and am having a problem with displaying an edited picture. I need to have buttons that edit the picture (eg. remove red, blue, green components and rotate) and display that edited picture. I am using imshow to display the edited picture but it displays in a new window and shuts down the GUI I had running. Can anyone help?
I've been working on this and have tried numerous different ways of fixing the problem but none worked. However, I am using MATLAB 7.0.1, and 7.7.0 might have an update for this problem.
When you first plot the image with imshow, have it return a handle to the image object it creates:
A = (the initial matrix of image data);
hImage = imshow(A);
Then, to update the image with new data, try the following instead of calling imshow again:
B = (modification of the original image matrix A);
set(hImage, 'CData', B);
Using the set command will change the image object you already created (a list of image object properties can be found here).
Alternatively, you can also add additional parameters to a call to imshow to tell it which axes object to plot the image in:
hAxes = (the handle to an axes object);
imshow(A, 'Parent', hAxes);
EDIT:
Addressing your additional problem of sharing GUI data between functions, you should check out the MATLAB documentation here. As noted there, there are a few different ways to pass data between different functions involved in a GUI: nesting functions (mentioned on SO here), using the 'UserData' property of objects (mentioned on SO here), or using the functions setappdata/getappdata or guidata. The guidata option may be best for use with GUIs made in GUIDE.
The GUI m file functions automatically assign the image data to a variable called hObject. Once you have done your image alteration, you have to reassign the new data to hObject:
hObject = imshow(newimagedata)
Don't forget to update and save this operation by:
guidata(hObject, handles)