Saturday, November 19, 2011

Cropping images is fun

Now that we know how to display images in Matlab using imshow and imagesc, let's see if we can write some basic codes involving manipulation of pixel values. 


The very first thing that I would like to try is to be able to crop an image... starting with the point (x_TopLeft, y_TopLeft) to (x_BottomRight, y_BottomRight).

Let's write the code below to crop the image: mri.jpg.




%================================================================
clear all

clc; 

close all;



A=imread('mri.jpg');

x_TopLeft=64;

y_TopLeft=64;

x_BottomRight=192; 

y_BottomRight=192;



M=x_BottomRight-x_TopLeft+1;
N=y_BottomRight-y_TopLeft+1; 

for i=1:M
    for j=1:N
        B(i, j, :)=A(x_TopLeft+i, y_TopLeft+j, :);
    end
end

figure;
imshow(A);
figure;
imshow(B);


%================================================================

If you write the above code in the editor file and save it as cropping.m and run it you will get two images, one is the original image and the other is the cropped image.


One thing new in the code is the mysterious appearance of a semicolon in B(i, j, :). It basically gives the element stored in B at the location (i, j) be it a grayscale or RGB. This comes in handy. 


Nevertheless, there are many problems with the above code. 

1. It assumes too much about the user's knowledge of the code. 
2. Every time if you want to crop an image you will essentially have to change the code. That is pain in the neck, and nobody likes it.
3. I used Matlab's imshow function, which I don't quite like in this case. I would like to not only crop the image, but also would like to see the cropped part closely.
4. What happens if the points for cropping give a trivial image?


... and so on and so on.

So, let's write a function instead which takes image or the name of the image. Below is the code for doing that.

Here, we are also writing another function new_imagesc, in the same file named cropping_fun.m
Note, it this case one must end both the functions with the keyword end.


%===============================================================================

function cropping_fun(image, x_TopLeft, y_TopLeft, x_BottomRight, y_BottomRight)

% Author: Prashant Athavale
% Date: 11/19/2011
% Please acknowledge my name if you use this code
% Function to crop images

%==============================================================================

clc;
close all;

% let us handle cases if the user inputs incorrect or insufficient inputs

if nargin>0
    if (ischar(image))&&(exist(image, 'file')==2)
        A=double(imread(image));
    elseif exist('image', 'var')
        A=double(image);
    end
    size_A=size(A);
end

if nargin==0
    A=double(imread('mri.jpg'));
    size_A=size(A);
    x_TopLeft=ceil(size_A(1)/4);
    y_TopLeft=ceil(size_A(2)/4);
    x_BottomRight=floor(3*size_A(1)/4);
    y_BottomRight=floor(3*size_A(2)/4);
elseif nargin==1
    x_TopLeft=ceil(size_A(1)/4);
    y_TopLeft=ceil(size_A(2)/4);
    x_BottomRight=floor(3*size_A(1)/4);
    y_BottomRight=floor(3*size_A(2)/4);
elseif nargin==2
    y_TopLeft=ceil(size_A(2)/4);
    x_BottomRight=floor(3*size_A(1)/4);
    y_BottomRight=floor(3*size_A(2)/4);
elseif nargin==3
    x_BottomRight=floor(3*size_A(1)/4);
    y_BottomRight=floor(3*size_A(2)/4);
elseif nargin==4
    y_BottomRight=floor(3*size_A(2)/4);
end

%==========================================================================

% size of the cropped image 

M=x_BottomRight-x_TopLeft+1;
N=y_BottomRight-y_TopLeft+1;

%==========================================================================

% Memory allocation for the output image for faster implementation of the
% code

image_dim=size(size_A);

if image_dim(2)==3
    P=3;
    B=zeros(M, N, P);
else
    B=zeros(M, N);
end

%==========================================================================

if ((M>0)&&(N>0))
    
    for i=1:M
        for j=1:N
            B(i, j, :)=A(x_TopLeft+i, y_TopLeft+j, :);
        end
    end

    new_imagesc(A);
    title('Original image');
    
    new_imagesc(B);
    title('Cropped image');
else
    error('Check the dimensions of the cropped image.');
end

end % End of the cropping_fun

%==========================================================================

function new_imagesc(A)

%==========================================================================

% Author: Prashant Athavale
% Date 11/19/2011
% Please acknowledge my name if you use this code, thank you.
% This is a function to display images at the center of the screen using
% Matlab's imagesc function

%==========================================================================

figure;
screen_size=get(0,'screensize');
screen_width=screen_size(3);
screen_height=screen_size(4);
figure_width=screen_size(4)/2; % width of the figure
figure_height=screen_size(4)/2; % height of the figure
x_lowerbottom=(screen_width-figure_width)/2; 
y_lowerbottom=(screen_height-figure_height)/2;
set(gcf,'position',[x_lowerbottom y_lowerbottom figure_width figure_height]);
image_dim=size(size(A));

% What to do if the image is color/RGB or grayscale

if image_dim(2)==3
    M=max(max(max(A)));
elseif image_dim(2)==2
    M=max(max(A));
end




if M>1

    M=1;
else
    M=255;
end

% imagesc likes the images from 0 to 255
% if they are not then we make them so.


A=double(A);
imagesc(uint8(A*M));
if image_dim(2)==3
    colormap('colormap');
else
    colormap('gray');
end

end % end of function new_imagesc

%==========================================================================



Here is the screenshot of the output of the function cropping_fun:





2 comments:

  1. I need help on some code that I am doing dealing with cropped images in Matlab. I am letting the user choose where they want to crop there image to use it as a standard photo, then I want to take another image to compare it to using the same dimensions. Please help! Thanks!

    ReplyDelete
  2. Sorry I didn't check my blog for a while. Not sure exactly what you want. You can store what the user enters to crop the first image and use it for the next photo (?)

    ReplyDelete