I'm wondering how can I save or read all the frames of the image as a image. I wrote the following code, but the result from the movie_frame(fr) is a structure while I need a image with all frames as I need it for doing block matching between different frames of the image.
Thank you very much.
I tried imwrite() which is not working, because Movie_Frames(fr) type was struct. Movie_Frames is giving me a cdata which is different with the size of the image.
clc
clear
close all
load im_sim
load info
for fr =1:size(im_sim,3);
clf;imagesc(info.x,info.y,im_sim(:,:,fr)),colormap gray, axis image,
hold on; title(num2str(fr));
pause(0.05);
Movie_Frames(fr) = getframe;
end
Related
I need to read images from a large stack of Tiff files while the stack is being updated with additional images. I don't wanna load the stack multiple times for performance issues. Is there a trick do read a single image at a time in Matlab?
You can use the Tiff class in Matlab to manipulate a multi-tiff file, i.e. a single tiff file containing many tiff images. Please note that I'm no expert in this regard, but when I first encountered this I was completely lost and after some fooling around with it I could get it to work so I hope it will help you. This should hopefully get you started; and you can easily put this in a loop to select multiple frames form your stack.
In the following code, TiffName refers to the name of your multi-tiff file.
%// Set up Tiff object in 'read' mode
Stack_TiffObject = Tiff(TiffName,'r');
%// Frame you wish to read
FrameToRead = 4;
%// Use setDirectory method to access the image in the stack
Stack_TiffObject.setDirectory(FrameToRead)
%// Read image data
YourImage = Stack_TiffObject.read();
%// Close the Tiff object when you're done
Stack_TiffObject().close
Unless I missed something completely obvious that should load only selected frames from your stack.
I have a image and some contours as bellow figure. I want to save the output into image (png or jpg). The saved image only contains the image region without the matlab window. Let see my example in the figure. Could you have me implement it by matlab? This is my code to make output figure
img = imread('coins.png');
mask_red=zeros(size(img));
mask_green=zeros(size(img));
mask_red(30:160,40:170)=1;
mask_green(70:100,60:130)=1;
imagesc(uint8(img),[0 255]),colormap(gray),axis off;axis equal,
hold on;
[c1,h1] = contour(mask_red,[0 0],'r','Linewidth',3);
[c2,h2] = contour(mask_green,[0 0],'g','Linewidth',3);
hold off;
%% Save output figure
Use the getframe and cdata idiom. If the figure is open, simply do this:
f = getframe;
im = f.cdata;
im will contain the image that was contained inside your frame as a RGB image. Running your code in your post, then the above code, then doing imshow(im), we get:
If you want to save the image, just use imwrite:
imwrite(im, 'coins_final.png');
The image will be saved in a file called coins_final.png.
I have grayscale satellite image which is processed from spectral data (band classifications). If i use jet colormap in imshow it will show absolute colormapped image. But if i try to imwrite in particular place it is saved like a bluish image. I saw one example in matlab central, but i didnt get. can anyone help me to write my image with colorscaled image.
Matlab central link: http://www.mathworks.in/matlabcentral/answers/25026-saving-grayscale-image-as-it-appears-in-jet-colormap-of-imagesc
there accepted answer link is : http://www.mathworks.com/matlabcentral/fileexchange/7943
I have tried many times, this will show colormaped images in plots (imshow) they didnt write anywhere with colormaped. Now i want to write my image with colormaped.
example code:
I= imread('image path');
imshow(I,'colormap',jet);
imwrite(I,'path','jpg'); /not working
or
imwrite(I,jet,'path','jpg'); /not working
Please help to solve this issue.
When you use imshow the colormap is always adjusted to the range of values in your image. imwrite however assumes your image has a value range of [0,1] if you are using single or double data types. Try to scale your image to the range [0,1] before saving.
If you provide a colormap in the call to imwrite, MATLAB assumes you are using an indexed image. Thus you will have to convert the image to the indexed format first. The following snippet worked for a test image I of mine:
% scale to [0,1]
I = I - min(I(:));
I = I ./ max(I(:));
% Create indexed image
[J,~] = gray2ind(I);
% Save image
imwrite(J,jet,'path','jpg');
Solution by hbaderts worked well for me, but later I found out that some images were still scaled slightly different way from imshow.
However, I might found a reason of an original problem. Just after Matlab starts, its default colormaps (including 'jet') are set to 64 colors (64x3). Then, if any image is shown with a colormap, for example if imshow('cameraman.tif'), colormap('jet') is executed, all default colormaps become 256x3 (can be verified with jetMap=jet; before and after). Then it might happen that an image was written with a colormap different from the one applied to image figure (for example, if a figure called after imwrite).
Finally I found this solution (no image pre-scaling needed):
% Create indexed image, explicitly using 256 colors
imInd=gray2ind(im,256);
% Convert indexed image to RGB using 256-colors jet map
jetRGB=ind2rgb(imInd,jet(256));
% Save image
imwrite(jetRGB,'jet.png');
The images I used have the same color scale now, both the saved one and the one shown in figure.
I am writing a function that generates series of images. I am using the imwrite function to write each image to a file:
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
Q=imagesc(nx/rad,ny/rad,Ecc);
if i==1
cl=caxis;
else
caxis(cl)
end
imwrite(Q,['Frame-',num2str(i),'.tif'],'tif');
But I am not getting the images. The files are generated just fine, but they are just white images with dimension 1x1. Any help please?
Thank you
Use imwrite on Ecc instead of Q. The output of imagesc (as I recall) is a handle to the figure, which is not what you want to write out. Write out Ecc instead.
Adding to what user3817401 has written.
Completly white images can result from data not being scaled prior to being sent to imwrite. Consider following:
Ecc = (Ecc - min(min(Ecc))) / (max(max(Ecc)) - min(min(Ecc)));
promply before imwrite. This will guarantee, that the image is in range 0-1 and should solve the problem.
The function imagesc returns a handle (you store it as Q), not scaled image data. Then, the function imwrite is interpreting Q as an image. Because it is a handle, it is just 1x1 and it's value is not meaningful as an image. Try scaling Ecc as desired and then writing that instead.
I use the below command to display the image
imshow(img,[]);
when i use the following command to save the image it is saved as an empty white page
imsave;
how to save the image in this case any command would do
You are probably running into an issue with matrix type and range. If img is type double it needs to be scaled between 0 and 1.
A common issue is to load an image in uint8 (scaled between 0 and 255), convert to double in order to do some processing on it, without scaling, and then try and save it out. When you do that, MATLAB tries to convert back to uint8, and any values in the image outside the [0 1] range are clipped. On many images this means that the file comes out all white.
To get around this, use functions like im2double and im2uint8 rather than just double or uint8 when converting images.
Try at the command line the difference between:
img = imread('pout.tif');
img = double(img);
imshow(img,[]);
imsave;
and
img = imread('pout.tif');
img = im2double(img);
imshow(img,[]);
imsave;
Convert image data into an actual image and try again:
h = image(img); %Convert to object
imsave(h); %Save image object
Notice that if you close the figure window generated by image(), the object is deleted and the handle has will point to nothing. Though this may be beyond of what you are asking for.
Hope this adjustment solved your problem
First convert the image to rgb using
img1=label2rgb(img);
then again convert the image into an gray image using
img2=rgb2gray(img1);
then u can use imshow to show the image and save it using imsave
imshow(img2);
imsave();