How do I save an image generated by imshow(image) into a variable? - image

This is my code. I want to save the image displayed on imshow(img) into a variable to use it later. Thanks!
img=imread('image1.bmp');
figure(1), imshow(img);
[r c]=ginput(4);
Bw=roipoly(img,r,c);
% figure,imshow(Bw)
[R C]=size(Bw);
for i=1:R
for j=1:C
if Bw(i,j)==1
img(i,j)=img(i,j);
else
img(i,j)=0;
end
end
end
figure,
imshow(img); title ('Output Image');

You can use the classic getframe / cdata idiom. With the figure window open, simply do this:
figure;
imshow(img); title('Output Image');
h = getframe;
im = h.cdata;
h is a handle to the current frame that is open, and the cdata field contains image data for the frame. The above code stores the frame image data into a variable called im for use for later.
Minor Comment
That for loop code to set the output is a bit inefficient. You can do this completely vectorized and you'll notice significant speedups.
This code:
for i=1:R
for j=1:C
if Bw(i,j)==1
img(i,j)=img(i,j);
else
img(i,j)=0;
end
end
end
... can be replaced with:
img(~BW) = 0;
I also don't understand why you'd need to store the image data inside the frame of imshow... when img already contains your data and you are ultimately showing the data contained in img. Why can't you just use img directly for your application? Nevertheless, the above solution will work.

Related

for loop causing bad text segmentation in matlab

the input images are a.jpg and b.jpg
these two image stored in for example comp folder.and want to write the segmented image in segment folder.but I think for looping problem segmentation repeated for so many times for each image.And I could't solve the problem.
here is my code
Resultado='C:\Users\Nurul\Desktop\picsegment';
srcFiles = dir('C:\Users\Nurul\Desktop\comp\*.jpg');
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\Nurul\Desktop\comp\',srcFiles(i).name);
a = imread(filename);
LLL=a;
s=regionprops(LLL);
figure,imshow(LLL); title('segmented Image');
hold on
for J=1:numel(s)
rectangle('Position',s(J).BoundingBox,'edgecolor','g')
end
im1=LLL;
baseFileName = sprintf('%d.jpg', i); % e.g. "1.png"
fullFileName = fullfile(Resultado, baseFileName);
imwrite(im1, fullFileName);
end
plz help
thanks
You are saving your data as jpg, big mistake!
Still, if you want to keep the data saved as jpg, remember that it will not be saved as a binary image, and that means you need to binarize it again! Otherwise, every little pixel noise will be detected as data by regionprops, thats why you get so many squares.
Just add
a = imread(filename);
a=im2bw(a,0.5); % Add this line. The fancy way would be im2bw(a,graythresh(a)), but 0.5 will do in your case
LLL=a;

How to make gif images from a set of images in matlab?

How to make '.gif' image from a set of '.jpg' images (say: I1.jpg, I2.jpg,..., I10.jpg) in matlab?
Ok here is a simple example. I got an image with a unicorn on it and remove 2 part to create 3 different images, just for the sake of creating an animated gif. Here is what it looks like:
clear
clc
%// Image source: http:\\giantbomb.com
A = rgb2gray(imread('Unicorn1.jpg'));
B = rgb2gray(imread('Unicorn2.jpg'));
C = rgb2gray(imread('Unicorn3.jpg'));
ImageCell = {A;B;C};
figure;
subplot(131)
imshow(A)
subplot(132)
imshow(B)
subplot(133)
imshow(C)
%// Just to show what the images look like (I removed spots to make sure there was an animation created):
%// Create file name.
FileName = 'UnicornAnimation.gif';
for k = 1:numel(ImageCell)
if k ==1
%// For 1st image, start the 'LoopCount'.
imwrite(ImageCell{k},FileName,'gif','LoopCount',Inf,'DelayTime',1);
else
imwrite(ImageCell{k},FileName,'gif','WriteMode','append','DelayTime',1);
end
end
As you see, its not that different from the example on the Mathworks website. Here my images are in a cell array but yours might be in a regular array or something else.That should work fine; when I open 'UnicornAnimation.gif' it is indeed a nice animation!
Hope that helps!

Matlab Zooming Using Pixel Replication

I was trying to zoom an image using pixel replication method. The code below has a "Warning: Image is too big to fit on screen;". I don't know if this is the problem with the output. The output is totally displaying something else. Here is a link to the output... https://www.dropbox.com/s/ixl80jwutra8e1a/Q.PNG
Img = handles.Image;
temp = double(imread(Img));
b=temp;
[m,n,colormap]=size(b);
%If RGB Image is given at Input
if colormap==3
x=b(:,:,1);
y=b(:,:,2);
z=b(:,:,3);
end
k=1; %Counter for Row and
l=1; %Column replication
f=2; %Replica factor
for i=1:m %Loop for reading row and
for t=1:f %Row replication
for j=1:n %Loop for reading column and
for t=1:f %Column replication
if colormap==3 %If Image is RGB
c1(k,l)= x(i,j);
c2(k,l)= y(i,j);
c3(k,l)= z(i,j);
else %If Image is grayscale
c(k,l)=b(i,j);
end
l=l+1;
end
end
l=1;
k=k+1;
end
end
if colormap==3 %If Image is RGB
c(:,:,1)=c1;
c(:,:,2)=c2;
c(:,:,3)=c3;
end
axes(handles.axes2);
imshow(c);
What exactly is the problem, is there any way out?
You get a warning because imshow automatically scale the images to fit the screen if they are two big. And it tells you it did.
If you want to force the output to be scaled 1:1, you can use:
imshow(c, 'InitialMagnification', 100);

How can I modify an image without showing it?

I have a code that loads an image to a plot, draws a rectangle on it an after this saves the image into a png file:
figure('Visible', 'off');
imshow(im)
hold on
for n=1:size(windowCandidates,1)
rectangle('Position',[x,y,w,h],'EdgeColor','g','LineWidth',2)
end
f=getframe;
[img_bound,map]=frame2im(f);
imwrite(img_bound, strcat(directory, 'name.', 'png'));
hold off
How can I do the same without displaying it in a figure? Just modifying it and saving, I dont want the user to see all this process)
Thanks!
You can make a figure invisible with:
figure('Visible', 'off');
And then just write it out as Matlab fig via:
saveas(gcf, 'path/to/filename');
or using the print command to png is this case
print('-dpng', 'path/to/filename');
Similar question with good answers and explanations else where on stackoverflow
Update
Thanks to Steve for pointing to this undocumented matlab function
function so;
close all;
im = imread('cameraman.tif');
hfig = figure('Visible', 'off'), imshow(im, 'Border', 'tight');
for n=1:2
rectangle('Position', [20*n, 20*n, 50, 50], 'EdgeColor', 'g', 'LineWidth', 2)
hold on;
end
F = im2frame(zbuffer_cdata(gcf));
imwrite(F.cdata, 'test.png');
% Function copied from
% http://www.mathworks.com/support/solutions/en/data/1-3NMHJ5/?solution=1
% -3NMHJ5
%
function cdata = zbuffer_cdata(hfig)
% Get CDATA from hardcopy using zbuffer
% Need to have PaperPositionMode be auto
orig_mode = get(hfig, 'PaperPositionMode');
set(hfig, 'PaperPositionMode', 'auto');
cdata = hardcopy(hfig, '-Dzbuffer', '-r0');
% Restore figure to original state
set(hfig, 'PaperPositionMode', orig_mode);

Matlab: How can I display several outputs in the same image?

Let's say my image is img=zeros(100,100,3), my outputs are several ellipse which i get using a created function [ret]=draw_ellipse(x,y,a,b,angle,color,img), I can display one ellipse using imshow(ret).For the moment, I'm trying to show serval ellipse in the image. But i don't know how to code it. will ‘for loop’ work or I need to hold them?
If this is related to what you were doing in your previous question, then what you need to do is to pass the result of one iteration as input to the next.
So assuming that the function [ret]=draw_ellipse(x,y,a,b,angle,color,img) you mentioned takes as input an image img and returns the same image with an ellipse drawn on it, you could do this:
%# ellipses parameters
%#x = {..}; y = {..};
%#a = {..}; b = {..};
%#angle = {..}; color = {..};
img = zeros(200,100,'uint8'); %# image to start with
for i=1:10
img = draw_ellipse(x{i},y{i}, a{i},b{i}, angle{i}, color{i}, img);
end
imshow(img)
I'm a bit unsure of what you want. You want to show several ellipse in one image, like plotting several graphs with hold on?
There is no equivalent command for images, but a simple solution is to add the ellipses into one image and show that one:
several_ellipse = ellipse1 + ellipse2 + ellipse3;
imshow(several_ellipse)
Presumably you want to pass ret as the final input to the next call to draw_ellipse.

Resources