Create variable from function output to use in another function

3 views (last 30 days)
I have a function which produces an image but I need to save this image as a tiff file so that the actual values of the image are retained (it contains + and - values). I've tested each part of the code and it works, but I just need to get the output from the first function to be a variable in the second function. I'm fairly new to functions, so I'm not sure how to get this to work. I've tried nested functions, but I don't know how to get the variable recognized. The code I have so far is:
function []= create_image(image,hdr)
[rows,cols]=size(image);
figure
x=[hdr.xfirst,hdr.xfirst+(cols-1)*hdr.xstep];
y=[hdr.yfirst,hdr.yfirst+(rows-1)*hdr.ystep];
imagesc(x,y,image);
set(gca,'YDir','normal');
colormap(jet);
colorbar;
axis equal;
axis image;
z=double(~isnan(image));
alpha(z);
function new_image
imgdata=output from above function;
t=Tiff('new.tif','w');
t.setTag('ImageLength',rows);
t.setTag('ImageWidth',cols);
t.setTag('Photometric',Tiff.Photometric.MinIsBlack);
t.setTag('BitsPerSample',64);
t.setTag('SamplesPerPixel',1);
t.setTag('SampleFormat',Tiff.SampleFormat.IEEEFP);
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
t.write(imgdata);
t.close();
figure
imagesc(imread('new.tif'));
end
end
Any advice on how to fix this? TIA
  2 Comments
Sarah
Sarah on 28 Jul 2015
The image I want as the variable is what appears in the graphics display after alpha(z) is applied.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 28 Jul 2015
You used imagesc() and a colormap. What shows up on the screen is going to be color. That requires multiple samples per pixel, but you have requested only 1 sample per pixel.
You set alpha(). Accurate representation of alpha in Tiff requires that you create Extra Samples to AssociatedAlpha or that you use the settings shown at http://www.mathworks.com/help/matlab/ref/tiff-class.html#btqyn4b-3 . Definitely not one sample per pixel.
But possibly what you want is what shows up visually. When you use alpha() with 0 values, the color underneath shows through completely. But you have not explicitly defined a background color for the figure or the axis so you are going to get the default. On my system that is get(0,'DefaultAxesColor') = [1 1 1], which is white; your system might be different. But at least we know and can account for this without having to do any fancy calculations.
What shows up on the screen is not going to have negative and positive values. What shows up on the screen is going to represent the data having been recentered and rescaled so that the lowest value corresponds to the first color and the highest value corresponds to the last color. The components are going to be in the range 0 to 1, because the colormap you used is a floating point colormap.
If you do not want the colorbar in the saved image, I suggest that what you use is the File Exchange Contribution "freezeColors", applied to the handle of the imagesc()
%I am assuming that image is 2D; if it isn't then your size() call is wrong
h = imagesc(x, y, image);
freezecolors(h);
recolored_image = get(h, 'CData');
%now handle the nan
background_color = get(gca, 'color');
nanpos = find(isnan(image));
recolored_image(nanpos) = background_color(1); %red channel
recolored_image(nanpos + rows*cols) = background_color(2); %green channel
recolored_image(nanpos + 2*rows*cols) = background_color(3); %blue channel
and now you can return recolored_image from the function by having originally specified
function recolored_image = create_image(image,hdr)
Then for your other function start it with
function new_image(imgdata)
And the use would be something like,
reci = create_image(image,hdr);
new_image(reci)
However I would recommend firmly against using "image" as the name of a variable, as that interferes with using the important graphics primitive image(). Even if you do not accidentally refer to the wrong thing yourself, using image as a variable is going to confuse other people that read the code.
  1 Comment
Sarah
Sarah on 28 Jul 2015
Thanks Walter, this is helpful. Yes, it is a 2D image and I also get the same values for get(0,'DefaultAxesColor'). I just used 'image' for the example but it's actually got a different name in the actual script, using 'image' was easier for the example, but I've changed it to 'map' for now. What I see in the image display is what I want to capture, so the +/- index values are preserved within the tif.
Might seem like a dumb question, I'm still not sure how to actually run the script from the command line. do I put
if true
reci = create_image(map,hdr);
new_image(reci)
end
in the script, or do I run both lines from the command line.
Given this, I can't run the 'new_image' part yet. I can get the first part of the script to run by using:
if true
create_image(map,hdr);
end
But, I get an error when it reaches these lines:
if true
recolored_image(nanpos + rows*cols) = background_color(2); %green channel
recolored_image(nanpos + 2*rows*cols) = background_color(3); %blue channel
Error: In an assignment A(I) = B, a matrix A cannot be resized.
end
So my script now looks like:
if true
function recolored_image = create_image(map,hdr)
[rows,cols]=size(map);
figure
x=[hdr.xfirst,hdr.xfirst+(cols-1)*hdr.xstep];
y=[hdr.yfirst,hdr.yfirst+(rows-1)*hdr.ystep];
h = imagesc(x, y, map);
freezeColors(h);
recolored_image = get(h, 'CData');
set(gca,'YDir','normal');
axis equal;
axis image;
%now handle the nan
background_color = get(gca, 'color');
nanpos = find(isnan(map));
recolored_image(nanpos) = background_color(1); %red channel
recolored_image(nanpos + rows*cols) = background_color(2); %green channel
recolored_image(nanpos + 2*rows*cols) = background_color(3); %blue channel
function new_image(imgdata)
t=Tiff('new.tif','w');
t.setTag('ImageLength',rows);
t.setTag('ImageWidth',cols);
t.setTag('Photometric',Tiff.Photometric.MinIsBlack);
t.setTag('BitsPerSample',64);
t.setTag('SamplesPerPixel',1);
t.setTag('SampleFormat',Tiff.SampleFormat.IEEEFP);
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
t.write(imgdata);
t.close();
figure
imagesc(imread('new.tif'));
end
end

Sign in to comment.

Categories

Find more on Images 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!