Create variable from function output to use in another function
2 views (last 30 days)
Show older comments
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
Answers (1)
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!