Kanishka Ganguly
Kanishka Ganguly

Reputation: 1298

Apply `flipud` function to image obtained from `frame2im` in MATLAB

I have a basic figure obtained from plot() which I converted to an image using getframe(gcf) and frame2im.

plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);
F = getframe(gcf); 
[X, Map] = frame2im(F);

imshow(X,Map) works just fine, but when I try to apply other image related functions like flipud or rot90 to X MATLAB says the image must be a 2D matrix.

How can I go about this?

Upvotes: 3

Views: 286

Answers (3)

Kanishka Ganguly
Kanishka Ganguly

Reputation: 1298

Thanks a lot to Daniel and rayryeng for their help. :)

Here is what I did finally.

if isempty(Map)
    rgb = X;
else                       
    rgb = ind2rgb(X,Map);
end
rgb = rgb2gray(rgb);
rgb = im2bw(rgb);

rgb = flipud(rgb);

As said by rayryeng, this converts my image from RGB to Binary and thus is a 2D matrix I can manipulate.

Upvotes: 0

rayryeng
rayryeng

Reputation: 104535

The reason why is because you have a multi-channel image. It works for "some" images because they are most likely grayscale, and are only a 2D matrix. Those methods won't work if you have a 3D matrix (a.k.a a multi-channel image). If you really want to use flipud or rot90, consider using a for loop to iterate through each channel and flip the channels by themselves.

As such, given your image X, do something like this:

Xout = [];

for i = 1 : size(X, 3)
    Xout = cat(3, Xout, flipud(X(:,:,i))); %// or Xout = cat(3, Xout, rot90(X(:,:,i)));
end

Xout will contain your fully transformed image.

FWIW: Using imrotate is cleaner (à la Daniel's method). I would recommend you use that instead.

Aside

In your code, getframe and frame2im will return RGB data for your frame. As such, your image will naturally be multi-channel :)

Upvotes: 7

Daniel
Daniel

Reputation: 36710

To rotate use imrotate (image processing functions begin with im), to flip a dimension use flipdim

Upvotes: 0

Related Questions