Showing posts with label Add an image in matlab. Show all posts
Showing posts with label Add an image in matlab. Show all posts

Monday, March 28, 2016

Bit Plane Slicing in MATLAB

       Read fractal.jpg file. Display individual bits as binary image. Display and comment on the result.  (Hint: use bitget built-in function)

CODE:

clc; clear;

A=imread('fractal.jpg');
[row,col]=size(A);
subplot(3,3,1), imshow(A), title('Original Image');
C=zeros(row,col,8);
for k=1:8
    for i=1:row
        for j=1:col
            C(i,j,k)=bitget(A(i,j),k);        %Bit slicing
        end
    end
    subplot(3,3,k+1), imshow(C(:,:,k)), title(['Bit Plane ',num2str(k-1)]);
end


OUTPUT RESULT :

We observe that the three higher order planes, especially the 8th bit plane, contain a significant amount of the visually significant data. The lower order planes contribute to more subtle intensity details in the image. Decomposing an image into its bit planes is useful for analyzing the relative importance of each bit in the image and is useful for image compression.

Sunday, March 27, 2016

Gray Level Slicing in MATLAB

 Gray Level Slicing: High-light specific range of gray values without background and with background.

       Read kidney.tif file. Enter lower and upper threshold value from the user. Apply gray level slicing with and without background. Display the result.

 CODE:
clc; clear all;

i=imread('kidney.tif');          % should be graylevel image
j=double(i);
k=double(i);
[row,col]=size(j);
T1=input('Enter the Lowest threshold value:');
T2=input('Enter the Highest threshold value:');
for x=1:row            
    for y=1:col        
        if((j(x,y)>T1) && (j(x,y)<T2))
            j(x,y)=i(x,y);
            k(x,y)=255;
        else
            j(x,y)=0;
            k(x,y)=0;
        end
    end
end

subplot(311), imshow(i), title('Original image')   
subplot(312), imshow(uint8(j)), title('Graylevel slicing with background')
subplot(313), imshow(uint8(k)), title('Graylevel slicing without background')

OUTPUT RESULT:

Thursday, March 24, 2016

How to apply Threshold to an image in MATLAB ?

Exercise: Apply thresholding to the original image such that r1= r2=mean gray level. Display the result. (Hint: use built-in function im2bw.)

 CODE:

clc; clear all;

%% Reading an image
a1=imread('contrast.jpg');
a=double(a1);
[row,col]=size(a);

%% Calculating mean gray level
sum = 0;
for i=1:row
    for j=1:col
        sum=sum+a(i,j);
    end
end
avg=sum/(row*col);

%% Transformation function
t=0:255;
x1=0*(t>=0 & t<avg); 
x2=255*(t>=avg & t<=255);
x=x1+x2;

%% Obtaining contrast stretched image
for n=1:row
    for m=1:col
        out(n,m)=x(a1(n,m)+1);
    end
end

out1=im2bw(a1);

plot(x)
grid on;
xlabel('Intensity in input image');
ylabel('Intensity in output image')
title('Transformation function')

figure()
subplot(311), imshow(a1), title('Original image')
subplot(312), imshow(uint8(out)), title('Thresholding(using code)')
subplot(313), imshow(out1), title('Thresholding(using im2bw)')


OUTPUT RESULT:


Transformation Function

Sunday, March 20, 2016

Perform Arithmetic operations on an Image

Exercise 1) Write a MATLAB program to perform the following.
1)     Read cameraman.tif and rice.png image and display the image
2)     Add the two images and display.
3)     Subtract the two images and display
4)     Add a constant value of 50 to one of the images and display
5)     Subtract a constant value 100 from one of the images and display.
6)     Obtain negative of one of the image. (Subtract 255 from the image) and display.
Create a binary image of size 256 x 256 as shown below and save as ‘mask.tif 


                8)Perform I= uint8(I1).*uint8(M)  ,where I1 is cameraman.tif and M is mask.tif 
                    Images.

clear
close all;

a=imread('cameraman.tif');
b=imread('rice.png');
c=a+b;                  % addition
d=a-b;                  % subtraction
e=a.*b;                 % multiplication
f=b+50;                % adding a constant
g=a-100;              % subtracting a constant
h=255-b;              % negative of a image

%% creating a mask and saving it
i=zeros([256 256]);
j = 64:192;
k = 64:192;
i(j,k) = i(j,k)+1;
imwrite(i,'mask.tif');

l=uint8(a).*uint8(i);
subplot(331),imshow(a),title('Image 1')
subplot(332),imshow(b),title('Image 2')
subplot(333),imshow(c),title('Addition')
subplot(334),imshow(d),title('Subtraction')
subplot(335),imshow(e),title('Multiplication')
subplot(336),imshow(f),title('Adding a constant(50) to Image 2')
subplot(337),imshow(g),title('Subtracting a constant(100) from Image 1')
subplot(338),imshow(h),title('Negative of Image 2')
subplot(339),imshow(l),title('Masking of Image 1')

Fig 1. Arithmetic operations on image using code


Exercise 2) Write the above program using imadd() function for addition, imsubtract() for Subtraction, immultiply() for multiplication operations. Use imcomplement() function to get complement of an image.
clear
close all;

a=imread('cameraman.tif');
b=imread('rice.png');

c=imadd(a,b);                   % addition
d=imsubtract(a,b);           % subtraction
e=immultiply(a,b);                         % multiplication
f=imadd(b,100);                              % adding a constant
g=imsubtract(a,50);                        % subtracting a constant
h=imcomplement(b);                     % complement of image

subplot(331),imshow(a),title('Image 1')
subplot(332),imshow(b),title('Image 2')
subplot(333),imshow(c),title('Addition')
subplot(334),imshow(d),title('Subtraction')
subplot(335),imshow(e),title('Multiplication')
subplot(336),imshow(f),title('Adding a constant(100) to Image 2')
subplot(337),imshow(g),title('Subtracting a constant(50) from Image 1')
subplot(338),imshow(h),title('Complement of Image 2')


Fig. 2: Arithmetic operations on image using built-in functions