Matlab Zooming Using Pixel Replication - image

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);

Related

Image blending with mask

I'm trying to combine the two images based on the information from the mask. I'm using the color information from the background image if the mask is 0 and color information from foreground image if the mask is 1. Because the mask and both
Images are of the same size, I would like to use logical indexing of matrices to achieve this.
My attempt:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,~]=size(mask);
A = zeros(size(mask));
for i=1:r
for j=1:c
if mask(i,j) == 0
A(i,j,:) = background(i,j,:);
end
if mask(i,j) > 0
A(i,j,:) = foreground(i,j,:);
end
end
end
imshow(A);
The result looks like a flickering blue image, but I don't want that. Please help.
You can do this a bit more concisely:
f = double(foreground).*double(mask);
b = double(background).*double(~mask);
blend = f+b;
imshow(blend, []);
Using logical indexing you could also do
foreground(logical(mask)) = 0;
background(logical(~mask)) = 0;
blend = foreground+background;
The ISNOT operator '~' inverts your matrix in the second line, so you cut out the area you would like for background.
NOTE: This works for black and white (one channel). For coloured images see rayryeng's solution.
There are two problems with your code. The first problem is that you are trying to assign colour pixels to the output image A, yet this image is only two-dimensional. You want an image with three channels, not two. In addition, the output image type you are specifying is wrong. By default, the output image A is of type double, yet you are copying values into it that aren't double... most likely unsigned 8-bit integer.
As such, cast the image to the same type as the input images. Assuming both input images are the same type, initialize your A so that:
A = zeros(size(foreground), class(foreground));
This correctly makes a colour image with the same type as any of the inputs, assuming that they're both the same type.
Now, your for loop is fine, but it's better if you do this in one shot with logical indexing. If you want to use logical indexing, create a new image that's initially blank like what you've done, but then make sure your mask has three channels to match the number of channels the other images have. After, you simply need to index into each image and set the right locations accordingly:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,d]=size(mask); %// Change
%// If your mask isn't three channels, make it so
%// Change
if d ~= 3
mask = cat(3, mask, mask, mask);
end
A = zeros(size(foreground), class(foreground)); %// Change
A(mask) = foreground(mask); %// Assign pixels to foreground
A(~mask) = background(~mask); %// Assign pixels to background
imshow(A);

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

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.

Change Range Of Displayed Pixel Values from Command Line?

I'm working with some MRI data in Matlab 2014b, but the data is formed of intensity values not RGB. To get around this I use the code below to form a movie out of the MRI frames (I'm working on dynamic data here)
My problem is that the images need to have altered display values for the pixels, as the default only displays between -Inf and Inf, and I need between 0 and 0.25 to get a sensible image out of my data.
Are there any ways to pass that change from the script in to the movie, and then to write to file? I can only seem to do this per image in implay, and I'd like an automated way to edit each image and then store as a frame for a movie..?
%Code for producing movie.
graymap = gray(256);
for i = 1:32
a(:,:,i) = cmunique(Reformed_Data_Colourmap(:,:,i));
end
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
image(a(:,:,1))
colormap 'gray'
%The change needs to be here, to display pixel values from 0 to 0.25, to allow for a sensible image from the MR data.
frames(1,i) = getframe;
end
movie(frames)
The solution is provided:
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
clims = [0 250];
%image(a(:,:,1),clims)
colormap 'gray'
imagesc(a(:,:,1),clims);
%set('window', [0 400])
frames(1,i) = getframe;
end
clims solves the issue.

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!

Writing a greyscale video using Videowriter/avifile

I am writing a function that generates a movie mimicking a particle in a fluid. The movie is coloured and I would like to generate a grayscaled movie for the start. Right now I am using avifile instead of videowriter. Any help on changing this code to get grayscale movie? Thanks in advance.
close all;
clear variables;
colormap('gray');
vidObj=avifile('movie.avi');
for i=1:N
[nx,ny]=coordinates(Lx,Ly,Nx,Ny,[x(i),-y(i)]);
[xf,yf]=ndgrid(nx,ny);
zf=zeros(size(xf))+z(i);
% generate a frame here
[E,H]=nfmie(an,bn,xf,yf,zf,rad,ns,nm,lambda,tf_flag,cc_flag);
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
imagesc(nx/rad,ny/rad,Ecc);
writetif(Ecc,i);
if i==1
cl=caxis;
else
caxis(cl)
end
axis image;
axis off;
frame=getframe(gca);
cdata_size = size(frame.cdata);
data = uint8(zeros(ceil(cdata_size(1)/4)*4,ceil(cdata_size(2)/4)*4,3));
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
frame.cdata = data;
vidObj = addframe(vidObj,frame);
end
vidObj = close(vidObj);
For your frame data, use rgb2gray to convert a colour frame into its grayscale counterpart. As such, change this line:
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
To these two lines:
frameGray = rgb2gray(frame.cdata);
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = ...
cat(3,frameGray,frameGray,frameGray);
The first line of the new code will convert your colour frame into a single channel grayscale image. In colour, grayscale images have all of the same values for all of the channels, which is why for the second line, cat(3,frameGray,frameGray,frameGray); is being called. This stacks three copies of the grayscale image on top of each other as a 3D matrix and you can then write this frame to your file.
You need to do this stacking because when writing a frame to file using VideoWriter, the frame must be colour (a.k.a. a 3D matrix). As such, the only workaround you have if you want to write a grayscale frame to the file is to replicate the grayscale image into each of the red, green and blue channels to create its colour equivalent.
BTW, cdata_size(3) will always be 3, as getframe's cdata structure always returns a 3D matrix.
Good luck!

Resources