You are on page 1of 11

Lecture # 3: Code Imlementation

A= imread('Coins.JPG');
imshow(A);
pause(2);
imhist(A); Original
pause(2);
b=rgb2gray(A);
imshow(b);
pause(2);
imhist(b);
pause(2);
a=imadjust(b); Gray-Level
imshow(a);
pause(2);
imhist(a);
pause(2);
i=im2bw(A);
imshow(i);
pause(2); Enhanced
imhist(i);
pause(2);
I2 = histeq(A);
imshow(I2)
pause(2);
imhist(I2)

Black & White

Histogram Eq.
Lecture # 4: Code Imlementation

Negative Imaging:
A=imread('pexels-photo-1280162.jpeg');
imshow(A);
pause(2);
%First Method to find Negative Image explicitly
negative_image=255-A;
imshow(negative_image);
pause(2);
imshow(A);
pause(2);

%Second method to find Negative image build in function


neg_img=imcomplement(A);
imshow(neg_img);

Threshold: Original Threshold

I = imread('Coins.jpg');
imshow(I);
pause(2);
im=rgb2gray(I);
imshow(im);
pause(2);
level = graythresh(im);
BW = im2bw(im,level);
imshowpair(I,BW,'Montage');
Log Transformation:

a1 = imread('log.JPG'); % Read the image


a = double(a1)/255; % Normalized Image
c = 4; % Constant
f = c*log(1 + (a)); % Log Transform
imshowpair(a1,f,'Montage')
title('Original Log Transformation');

Power Transformation:
a=imread('power.jfif');
a1 = im2double(a);
c=1;
y=c*(a1.^5);
imshowpair(a,y,'Montage');
title('Original Power Transformation');

Bit Plane Slicing:


A=imread('bit.jpg');
A=rgb2gray(A);
imshow(A);
B=bitget(A,1);
figure,subplot(2,2,1);imshow(B);title('Bit plane 1');
B=bitget(A,2);
subplot(2,2,2);imshow(B);title('Bit plane 2');
B=bitget(A,3);
subplot(2,2,3);imshow(B);title('Bit plane 3');
B=bitget(A,4);
subplot(2,2,4);imshow(B);title('Bit plane 4');
B=bitget(A,5);
figure,
subplot(2,2,1);imshow(B);title('Bit plane 5');
B=bitget(A,6);
subplot(2,2,2);imshow(B);title('Bit plane 6');
B=bitget(A,7);
subplot(2,2,3);imshow(B);title('Bit plane 7');
B=bitget(A,8);
subplot(2,2,4);imshow(B);title('Bit plane 8');
Lecture # 5: Code Imlementation

Spatial Filtering:

x=imread('Capture.jpg');
f1=[1 0 0;0 0 0;1 0 0];
f2=[1 0 -1;1 0 -1;1 0 -1];
f3=[1 1 1;0 0 0;-1 -1 -1];
x1=x(1:256,1:256);
y1=conv2(x1,f1);
y2=conv2(x1,f2);
y3=conv2(x1,f3);
subplot(2,2,1);imshow(x);title('IMAGE ')
subplot(2,2,2);imshow(uint8((y1)));title('f1- IMAGE ')
subplot(2,2,3);imshow(uint8((y2)));title('f2- IMAGE ')
subplot(2,2,4);imshow(uint8((y3)));title('f3-IMAGE ')

Smooting Filter:
I = imread('Coins.jpg');
imshow(I);
pause(2);
I = imgaussfilt(I,3);
imshow(I);

Averaging Filter:
original=imread('Coins.jpg');
imshow(original);
pause(2);
h = fspecial('average', 3);
im = imfilter(original, h);
imshow(im);

Convolution and Correlation:


image=imread('Capture.jpg');
imshow(image);
pause(2);
h = fspecial('average', 5);
im = imfilter(image, h,'conv'); %imfilter(image,h,corr);
imshow(im);
Lecture # 6: Code Imlementation

Sharpening Filter: Original Sharp

a = imread('sharp.JPG');
b = imsharpen(a);
imshowpair(a,b,'montage');

Laplacian:
image=imread("lap.JPG");
k1=rgb2gray(image);
k1=double(k1);
Laplacian=[0 1 0; 1 -4 1; 0 1 0];
k2=conv2(k1, Laplacian, 'same');
imshowpair(k1,k2,'montage');

Soble Order:

a = imread('soble.JPG');
k=rgb2gray(a);
imshow(a);
pause(2);
aa = edge(k,'sobel')
imshow(aa);
Lecture No # 7 to 13

Image Restoration
I=imread("Coins.JPG");
gauss=imnoise(I,'Gaussian');
a=im2double(I);
Ray = raylrnd(a)
gamma=gamrnd(a,a)
exp = exprnd(a)
salt=imnoise(I,'salt & pepper');
subplot(3,2,1);imhist(I);title('Original')
subplot(3,2,2);imhist(gauss);title('Gaussion')
subplot(3,2,3);imhist(Ray);title('Rayleigh')
subplot(3,2,4);imhist(gamma);title('Erlang')
subplot(3,2,5);imhist(exp);title('Exponential')
subplot(3,2,6);imhist(salt);title('Impule')

Removing Noise:

I=imread("Capture.JPG");
salt=imnoise(I,'salt & pepper');
salt1=salt(1:256,1:256)
f1=[1/9 1/9 1/9;1/9 1/9 1/9;1/9 1/9 1/9];
ss=conv2(salt1,f1);
subplot(1,2,1);imshow(salt);title('Noisy image')
subplot(1,2,2);imshow(uint8((ss)));title('Filtered Image')

Arithmetic Mean:

I=imread("Capture.JPG");
salt=imnoise(I,'salt & pepper');
m=3;
n=3;
w=ones(m,n)/(m*n);
imshow(salt);
pause(2)
F=imfilter(salt,w,'replicate');
imshow(F)
GEOMETRIC MEAN
I=imread("flower.jpg");
I=rgb2gray(I);
SI=imnoise(I,'salt & pepper');
Kr=3;Kc=3;
Sg=im2double(SI);
SF=exp(imfilter(log(Sg),ones(Kr,Kc),'replicate')).^(1/(Kr*Kc));
figure
subplot(121)
imshow(SI)
title('image with salt & pepper Noise')
subplot(122)
imshow(SF)
title(' Filtered Image')

CONTRA HARMONIC MEAN:


I=imread("alone.jpg");
I=rgb2gray(I);
SI=imnoise(I,'salt & pepper');
Ord=2;
Kr=3;Kc=3;
Sg=im2double(SI);
SF=imfilter(Sg.^(Ord+1),ones(Kr,Kc),'replicate')./imfilter(Sg.^(Ord),ones(Kr,Kc),'replicate');
figure
subplot(1, 2 ,1)
imshow(SI)
title('Image with Salt and Pepper Noise')
subplot(1, 2 ,2)
imshow(SF)
title('Filtered Salt and pepper noise')

HARMONIC MEAN
I=imread("pretty.jpg");
I=rgb2gray(I);
SI=imnoise(I,'salt & pepper');
Kr=3;
Kc=3;
Sg=im2double (SI);
SF=(Kr*Kc) ./imfilter(1./(Sg+eps),ones(Kr,Kc),('replicate'));
figure
subplot (121)
imshow(SI)
title(' Image with salt and pepper noise')
subplot (122)
imshow(SF)
title(' Filtered Image')
Stretching:
image=imread('Geo.JPG');
stimg = imadjust(image, stretchlim(image, [0.05, 0.95]),[]);
subplot(2,2,1);imshow(image);title('Original')
subplot(2,2,2);imshow(stimg);title('Stretch')
subplot(2,2,3);imhist(image);title('Original histogram')
subplot(2,2,4);imhist(stimg);title('Stretch histogram')

SlidingRight:
im=imread('Geo.JPG')
offset=5;
slidingR=im+offset;
subplot(2,2,1);imshow(im);title('Original')
subplot(2,2,2);imshow(slidingR);title('Sliding-Right')
subplot(2,2,3);imhist(im);title('Original histogram')
subplot(2,2,4);imhist(slidingR);title('Slide histogram')

SlidingLeft:

im=imread('Geo.JPG')
offset=50;
slidingL=im-offset;
subplot(2,2,1);imshow(im);title('Original')
subplot(2,2,2);imshow(slidingL);title('Sliding-Right')
subplot(2,2,3);imhist(im);title('Original histogram')
subplot(2,2,4);imhist(slidingL);title('Slide histogram')

Shrinking:

im=imread('Capture.JPG')
smax=170;
smin=50;
shrink=((smax-smin)/(max(im(:))-min(Iimg(:))))*(im-min(Iimg(:)))+smin;
subplot(2,2,1);imshow(im);title('Original')
subplot(2,2,2);imshow(shrink);title('Shrink')
subplot(2,2,3);imhist(im);title('Original histogram')
subplot(2,2,4);imhist(shrink);title('Shrink histogram')
Histogram Matching:

im=imread('Capture.jpg')
figure
imshow(im)
figure
imhist(im);
q=imread('Coins.jpg')
figure
imshow(q)
figure
imhist(q);
B = imhistmatchn(im,q)
figure
imshow(B);
figure
imhist(B);

Histogram Equalization:

im=imread('Capture.jpg');
figure
imshow(im);
figure
imhist(im);
B = histeq(im)
figure
imshow(B);
figure
imhist(B);

Segmentation:
Seg=imread('Seg.jpg');
subplot(1,3,1);
imshow(Seg);
title('Original Image');
[BW,maskedImage] = segmentImage(Seg)
subplot(1,3,2);
imshow(BW);
title('Segmented Binary Image');
subplot(1,3,3);
imshow(maskedImage);
title('Segmented Color Image');
Point Detection:
x=imread('Point.jpg');
f1=[-1 -1 -1;-1 8 -1;-1 -1 -1];
x1=x(1:256,1:256);
y1=conv2(x1,f1);
level = graythresh(y1);
BW = im2bw(y1,level);
subplot(2,2,1);imshow(x);title('IMAGE ')
subplot(2,2,2);imshow(uint8((y1)));title('Point detect')
subplot(2,2,3);imshow(uint8((BW)));title('Threshold')

Line Detection:
x=imread('Capture.jpg');
hor=[-1 -1 -1;2 2 2;-1 -1 -1];
ver=[-1 2 -1;-1 2 -1;-1 2 -1];
positive45=[-1 -1 2;-1 2 -1;2 -1 -1];
negative45=[2 -1 -1;-1 2 -1;-1 -1 2];
figure,imshow(x);
x1=x(1:256,1:256)
y1=conv2(x1,hor)
y2=conv2(x1,ver)
y3=conv2(x1,positive45)
y4=conv2(x1,negative45)
subplot(2,2,1);imshow(uint8((y1)));title('Horizontal')
subplot(2,2,2);imshow(uint8((y2)));title('Vertical')
subplot(2,2,3);imshow(uint8((y3)));title('Positive 45')
subplot(2,2,4);imshow(uint8((y4)));title('Negative 45')

Edge Detection:
I = imread('Capture.jpg');
BW1 = edge(I(1:255,1:256),'Canny');
BW2 = edge(I(1:255,1:256),'Prewitt');
BW3 = edge(I(1:255,1:256),'Roberts');
BW4 = edge(I(1:255,1:256),'sobel')
BW5=BW1+BW2+BW3+BW4;
subplot(3,2,1);imshow(I);title('Original')
subplot(3,2,2);imshow(BW1);title('Canny')
subplot(3,2,3);imshow(BW2);title('Prewitt')
subplot(3,2,4);imshow(BW3);title('Robert')
subplot(3,2,5);imshow(BW4);title('Sobel')
subplot(3,2,6);imshow(BW5);title('Combine')
GLOBAL THRESHOLDIG:
T=imread("threshold1.jpg");
threshold_value= graythresh(T);
threshold_image= im2bw(T,threshold_value);
subplot(1,2,1),imshow(T),title('Original Image');
subplot(1,2,2),imshow(threshold_image),title('Threshold Image');

MULTILEVEL THRESHOLDING
I = imread("alone.jpg");
imshow(I)
title('original image');
figure
I = rgb2gray(I);
thresh = multithresh(I,15);
seg_I = imquantize(I,thresh);
imshow(seg_I == 6)
title('multithresh image');

ADAPTIVE THRESHOLDING
I = imread('huny.jpg');
T = adaptthresh(I, 0.5);
BW = imbinarize(I,T);
figure
imshowpair(I, BW, 'montage')

You might also like