0% found this document useful (0 votes)
23 views25 pages

Image processing Lab

The document outlines a list of experiments related to image processing and computer vision, detailing various techniques such as image simulation, pixel relationships, transformations, and filtering methods. Each experiment includes specific aims, methodologies, and example code for implementation. Additionally, it covers advanced topics like image compression using DCT, DPCM, and Huffman coding.

Uploaded by

harshalsharma212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views25 pages

Image processing Lab

The document outlines a list of experiments related to image processing and computer vision, detailing various techniques such as image simulation, pixel relationships, transformations, and filtering methods. Each experiment includes specific aims, methodologies, and example code for implementation. Additionally, it covers advanced topics like image compression using DCT, DPCM, and Huffman coding.

Uploaded by

harshalsharma212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

LIST OF EXPERIMENTS

IMAGE PROCESSING AND COMPUTER VISION

S.No. NAME OF EXPERIMENT Page


Sign.
no.
Simula on and Display of an Image, Nega ve of an Image(Binary &
1. 1-3
Gray Scale)

2. Implementa on of Rela onships between Pixels 4

3. Implementa on of Transforma ons of an Image 5-6

Contrast stretching of a low contrast image, Histogram, and Histogram


4. 7
Equaliza on

5. Display of bit planes of an Image 8

6. Display of FFT (1-D & 2-D) of an image 9

Computa on of Mean, Standard Devia on, Correla on coefficient of


7. 10-11
the given Image

Implementa on of Image Smoothening Filters (Mean and Median


8. 12-13
filtering of an Image)

Implementa on of image sharpening filters and Edge Detec on using


9. 14-15
Gradient Filters

10. Image Compression by DCT, DPCM, HUFFMAN coding 16-17

11. Implementa on of image restoring techniques 18-20

Implementa on of Image Intensity slicing technique for image


12. 21
enhancement

13. Canny edge detec on Algorithm 22-24


Experiment 1
AIM: Simulation and Display of an Image, Negative of an Image(Binary & Gray
Scale
Red Blue and Green and Gray Components
i=imread('cancercell.jpg');
subplot(3,2,1); imshow(i); title('Original Image');

Red Component
r=i(:,:,1);
subplot(3,2,2); imshow(r);title('Red Component');

Green Component
g=i(:,:,2);
subplot(3,2,3); imshow(g); title('Green Component');

Blue Component
b=i(:,:,3);
subplot(3,2,4); imshow(b); title('Blue Component');

Color to Gray Image


rg=rgb2gray(i);
subplot(3,2,5); imshow(rg); title('Gray Image');

1
Complement, Converting and Simulation of an Image
Display color Image, find its complement and convert to gray scale
I=imread('cancercell.jpg');
subplot(2,2,1); imshow(I); subimage(I); title('Color Image');

c=imcomplement(I);
subplot(2,2,2); imshow(c); subimage(c); title('Complement of color Image');

r=rgb2gray(I);
subplot(2,2,3); imshow(r); subimage(r); title('Gray scale of color Image');

%Complement of Gray Image


b=imcomplement(r);
subplot(2,2,4); imshow(b); subimage(b); title('Complement of Gray Image');

%Simulation of an Image( Arithmetic & Logic Operation)


a=ones(40); b=zeros(40);
c=[a b;b a]; d=[b b;a a];
A=10*(c+d);
M=c.*d;
S=c-d;
D=c/4;
figure;
subplot(3,2,1); imshow(c);
subplot(3,2,2); imshow(d);
subplot(3,2,3); imshow(A);
subplot(3,2,4); imshow(M);
subplot(3,2,5); imshow(S);
subplot(3,2,6); imshow(D);

2
3
Experiment 2
AIM: Implementation of Relationships between Pixels

Neighbour of 4,8 and Diagonal point


% To find Neighbour of a given Pixel

a=magic(5);
disp(‘a=’); disp(a);
b=input('Enter the row < size of the Matrix');
c=input(' Enter the Column < size of matrix');
disp(‘Element’); disp(a(b,c));

% 4 Point Neighbour
N4=[a(b+1,c), a(b-1,c), a(b,c+1), a(b,c-1)];
disp(‘N4=’); disp(N4);

%8 Point Neighbour
N8=[a(b+1,c), a(b-1,c), a(b,c+1), a(b,c-1), a(b+1,c+1), a(b+1,c-1), a(b-1,c-1), a(b-1,c+1)];
disp(‘N8=’); disp(N8);

%Diagonal Neighbour
ND=[ a(b+1,c+1), a(b+1,c-1), a(b-1,c-1), a(b-1,c+1)];
disp(‘ND=’); disp(ND);

Output
a=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

Enter the row < size of the Matrix


3 Enter the Column < size of
matrix 3 Element =
13
N4=
19 7 20 6
N8=
19 7 20 6 21 12 5 14
ND=
21 12 5 14

4
Experiment 3
AIM: Implementation of Transformations of an Image
%Scaling & Rotation

% Scaling (Resize)
I=imread('earcell.jpg');
subplot(2,2,1); subimage(I); title('Original Image');

s=input('Enter Scaling Factor');


j=imresize(I,s);
subplot(2,2,2); subimage(j); title('Scaled Image');

% Rotation
K=imrotate(j,60);
subplot(2,2,3); imshow(K); title('Rotated Image 60deg');

R=imrotate(j,45);
subplot(2,2,4); imshow(R); title('Rotated Image 45deg');

5
%Display the color image and its Resized images by different methods

%Display the color image


I=imread('embryo.jpg');
figure,
subplot(2,2,1);
subimage(I);
title('Original Image');

%Display Resized image by Bilinear method


B=imresize(I,5);
subplot(2,2,2);
subimage(B);
title('Bilinear Image');

%Display Resized image by Nearest method


C=imresize(I,5,'nearest');
subplot(2,2,3);
subimage(C);
title('Nearest Image');

%Display Resized image by Bicubic method


D=imresize(I,5,'Bicubic');
subplot(2,2,4);
subimage(D);
title('Bicubic Image');

6
Experiment 4
AIM: Contrast stretching of a low contrast image, Histogram, and Histogram
Equalization

% Image Enhancement
I=imread('cancercell.jpg');
subplot(4,2,1); imshow(I); title('Original Image');

g=rgb2gray(I);
subplot(4,2,5); imshow(g); title('Gray Image');

J=imadjust(g,[0.3 0.7],[]);
subplot(4,2,3); imshow(J); title('Enhanced Image');

D= imadjust(I,[0.2 0.3 0; 0.6 0.7 1],[]);


subplot(4,2,4);imshow(D);title('Enhanced Image 2');

% Histogram and Histogram Equalization


subplot(4,2,7); imhist(g); title('Histogram of Gray Image');

m=histeq(g);
subplot(4,2,6); imshow(m); title('Equalized Image');
subplot(4,2,8); imhist(m); title('Histogram of Equalized Image');

7
Experiment 5
AIM: Display of bit planes of an Image

i=imread('earcell.jpg');
b0=double(bitget(i,1));
b1=double(bitget(i,2));
b2=double(bitget(i,3));
b3=double(bitget(i,4));
b4=double(bitget(i,5));
b5=double(bitget(i,6));
b6=double(bitget(i,7));
b7=double(bitget(i,8));

subplot(3,3,1);imshow(i);title('Original Image');
subplot(3,3,2);subimage(b0);title('BIT PLANE 0');
subplot(3,3,3);subimage(b1);title('BIT PLANE 1');
subplot(3,3,4);subimage(b2);title('BIT PLANE 2');
subplot(3,3,5);subimage(b3);title('BIT PLANE 3');
subplot(3,3,6);subimage(b4);title('BIT PLANE 4');
subplot(3,3,7);subimage(b5);title('BIT PLANE 5');
subplot(3,3,8);subimage(b6);title('BIT PLANE 6');
subplot(3,3,9);subimage(b7);title('BIT PLANE

8
Experiment 6
AIM: Display of FFT(1-D & 2-D) of an image

l=im2double(imread('cancercell.jpg'));
f1=fft(l);
f2=fftshift(f1);
subplot(2,2,1); imshow(abs(f1)); title('Frequency Spectrum');
subplot(2,2,2); imshow(abs(f2)); title('Centered Spectrum');

f3=log(1+abs(f2));
subplot(2,2,3); imshow(f3); title('log(1+abs(f2))');
l=fft2(f1);
l1=real(l);
subplot(2,2,4); imshow(l1);title(' 2-D FFT');

9
Experiment 7
AIM: Computation of mean, Standard Deviation, Correlation coefficient of the given
Image

i=imread('cancercell.jpg');
subplot(2,2,1); imshow(i);title('Original Image');

g=rgb2gray(i);
subplot(2,2,2); imshow(g);title('Gray Image');

c=imcrop(g);
subplot(2,2,3); imshow(c);title('Cropped Image');

m=mean2(c);disp('m'); disp(m);
s=std2(c); disp('s'); disp(s);

figure,
k=(checkerboard>0.8);
subplot(2,1,1); imshow(k); title('Image1');

k1=(checkerboard>0.5);
subplot(2,1,2); imshow(k1); title('Image2');

r=corr2(k,k1);
disp('r');disp(r);

m
74.5173

s
44.2327

r
0.5774

10
11
Experiment 8
AIM: Implementation of Image Smoothening Filters(Mean and
Median filtering of an Image)
% Median Filters
I=imread('nuron.jpg');
K = rgb2gray(I);
J= imnoise(K ,'salt & pepper',0.05);
f= medfilt2(J,[3,3]);
f1=medfilt2(J,[10,10]);

subplot(3,2,1); imshow(I); title('Original Image');


subplot(3,2,2); imshow(K); title('Gray Image');
subplot(3,2,3); imshow(J); title('Noise added Image');
subplot(3,2,4); imshow(f); title('3x3 Image');
subplot(3,2,5); imshow(f1); title('10x10 Image');

%Mean Filter and Average Filter


figure;
i=imread('nuron.jpg');
g=rgb2gray(i);
g1=fspecial('average',[3 3]);
b1 = imfilter(g,g1);
subplot(2,2,1); imshow(i); title('Original Image');
subplot(2,2,2); imshow(g); title('Gray Image');
subplot(2,2,3); imshow(b1); title('3x3 Image');
g2= fspecial('average',[10 10]);
b2=imfilter(g,g2);
subplot(2,2,4); imshow(b2); title('10x10 Image');

%Implementation of filter using Convolution


figure;
I= imread('earcell.jpg');
I=I(:,:,1); subplot(2,2,1); imshow(I); title('Original Image');

a=[0.001 0.001 0.001; 0.001 0.001 0.001; 0.001 0.001 0.001];


R=conv2(a,I);
subplot(2,2,2); imshow(R); title('Filtered Image');

b=[0.005 0.005 0.005; 0.005 0.005 0.005; 0.005 0.005 0.005];


R1=conv2(b,I);
subplot(2,2,3); imshow(R1); title('Filtered Image 2');

12
13
Experiment 9
AIM: Implementation of image sharpening filters and Edge Detection using
Gradient Filters
i=imread('cancercell.jpg');
subplot(4,2,1); imshow(i);
title('Original Image');

g=rgb2gray(i);
subplot(4,2,2); imshow(g); title('Gray Image');

f=fspecial('laplacian',0.05);
im=imfilter(g,f);
subplot(4,2,3); imshow(im); title('Laplacian ');

s=edge(g, 'sobel');
subplot(4,2,4); imshow(s); title('Sobel');

p=edge(g, 'prewitt');
subplot(4,2,5); imshow(p); title('Prewitt');

r=edge(g, 'roberts');
subplot(4,2,6); imshow(r); title('Roberts');

[BW,thresh,gv,gh]=edge(g,'sobel',[],'horizontal');
[BW1,thresh1,gv1,gh1]=edge(g,'sobel',[],'vertical');

subplot(4,2,7); imshow(BW); title('Sobel Horizontal');


subplot(4,2,8);
imshow(BW); title('Sobel Vertical');

14
15
Experiment 10
AIM: Image Compression by DCT, DPCM, HUFFMAN coding

Image Compression using DCT, DPCM, and Huffman Coding


1. DCT (Discrete Cosine Transform)
 Purpose: Converts spatial image data (pixels) into frequency components.
 How it works:
o Image is divided into 8x8 blocks.
o DCT is applied on each block.
o The result is a matrix with one DC coefficient (average value) and many AC coefficients
(high-frequency details).
o Most energy (important info) is concentrated in a few coefficients.
 Compression: High-frequency components (less important) are quantized aggressively or
discarded.
2. DPCM (Differential Pulse Code Modulation)
 Purpose: Compresses the DC coefficients obtained from DCT.
 How it works:
o The DC value of a block is predicted based on the previous block’s DC.
o The difference (not actual value) is encoded, as it often requires fewer bits.
 Compression: Reduces redundancy among neighboring blocks.
3. Huffman Coding
 Purpose: Lossless compression of quantized coefficients.
 How it works:
o Analyzes frequency of coefficient values.
o Assigns shorter codes to more frequent values and longer codes to rare ones.
o Builds a variable-length code tree (Huffman tree).
 Compression: Efficient encoding of symbol streams with minimal redundancy.

□ Summary Workflow
mathematica
CopyEdit
Image → Divide into 8x8 blocks
→ Apply DCT → Quantization
→ DPCM for DC coefficients
→ Zig-zag scan of AC coefficients
→ Huffman coding
→ Compressed image
16
If you want code or diagrams for this (like how DCT matrix looks or sample Huffman coding),
just say the word

17
Experiment 11
AIM: Implementation of image restoring techniques

Goal:
To restore an image that has been blurred, corrupted by noise, or degraded by other processes.

Common Image Restoration Techniques


1. Inverse Filtering
 Use Case: When degradation function (like motion blur) is known.
 Implementation:
python
CopyEdit
import numpy as np
import cv2
import matplotlib.pyplot as plt

image = cv2.imread('blurred_image.png', 0)
# Create a simple motion blur kernel
kernel = np.zeros((5, 5))
kernel[2, :] = np.ones(5)
kernel /= 5

# Apply inverse filtering using frequency domain


dft_img = np.fft.fft2(image)
dft_kernel = np.fft.fft2(kernel, s=image.shape)
restored = np.abs(np.fft.ifft2(dft_img / dft_kernel))

plt.imshow(restored, cmap='gray')
plt.title("Restored Image - Inverse Filtering")
plt.show()

2. Wiener Filtering
 Use Case: More robust when noise is also present.
 Formula:
Hw(u,v)=H∗(u,v)∣H(u,v)∣2+Sn(u,v)Sf(u,v)H_w(u,v) = \frac{H^*(u,v)}{|H(u,v)|^2 +
\frac{S_n(u,v)}{S_f(u,v)}}Hw(u,v)=∣H(u,v)∣2+Sf(u,v)Sn(u,v)H∗(u,v)
18
 Implementation (using scipy.signal.wiener):

python CopyEdit
from scipy.signal import wiener

noisy_image = cv2.imread('noisy_image.png', 0)
restored = wiener(noisy_image, (5, 5))
plt.imshow(restored, cmap='gray')
plt.title("Restored Image - Wiener Filter")
plt.show()

3. Median Filtering
 Use Case: Salt-and-pepper noise.
 Implementation:
python
CopyEdit
denoised = cv2.medianBlur(noisy_image, 3)
plt.imshow(denoised, cmap='gray')
plt.title("Restored Image - Median Filter")
plt.show()

4. Lucy-Richardson Deconvolution (Advanced)


 Use Case: Deconvolution with point spread function (PSF).
 Available in: scikit-image
python
CopyEdit
from skimage.restoration import richardson_lucy
from skimage import color, data

psf = np.ones((5, 5)) / 25


image = color.rgb2gray(data.camera())
image = convolve2d(image, psf, 'same')
restored = richardson_lucy(image, psf, iterations=30)

19
Summary Table
Technique Handles Noise? Complexity Library
Inverse Filtering Blur Low NumPy
Wiener Filtering Blur+Noise Medium SciPy
Median Filtering Salt-Pepper Low OpenCV
Lucy-Richardson PSF based blur High skimage

20
Experiment 12
AIM: Implementation of Image Intensity slicing technique for image enhancement

i=imread('earcell.jpg');
subplot(3,2,1);imshow(i); title(‘Original Image’);
l=im2double(i);

level=graythresh(l);
BW =
im2bw(l,level);
subplot(3,2,2); imshow(BW); title('Image graythresh');

level1=0.2*BW;
subplot(3,2,3); imshow(level1); title('0.2 Slice');

level2=0.4*BW;
subplot(3,2,4); imshow(level2);title('0.4 Slice');

level3=0.6*BW;
subplot(3,2,5); imshow(level3);title('0.6 Slice');

level4=0.8*BW;
subplot(3,2,6); imshow(level4); title('0.8 Slice');

21
Experiment 13
AIM: Canny edge detection Algorithm

i= imread('cancercell.jpg');
g=rgb2gray(i);
subplot(2,2,1); imshow(i); title('Original Image');
subplot(2,2,2); imshow(g); title('Gray Image');
c=edge(g,'canny');
subplot(2,2,3); imshow(c); title('Canny output');

22
Canny Edge Detection Algorithm

Goal:
To detect sharp intensity changes (edges) in an image while minimizing noise and false detections.

□ Steps Involved in Canny Edge Detection:


1. Noise Reduction (Gaussian Blur)
o Smooth the image to reduce noise.
o Use a Gaussian filter.
o Example kernel:
[121242121]/16\begin{bmatrix} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \end{bmatrix} / 16121242121/16
2. Finding Intensity Gradient
o Use Sobel operator to find gradients in X and Y directions.
o Compute:
G=Gx2+Gy2,θ=tan −1(GyGx)G = \sqrt{G_x^2 + G_y^2}, \quad \theta = \tan^{-
1}\left(\frac{G_y}{G_x}\right)G=Gx2+Gy2,θ=tan−1(GxGy)
3. Non-Maximum Suppression
o Thin out the edges by keeping only local maxima in gradient direction.
o Suppresses all non-edge pixels to 0.
4. Double Thresholding
o Apply two thresholds: high and low.
 Strong edge: Gradient > high threshold.
 Weak edge: Gradient between low and high.
 Non-edge: Gradient < low threshold.
5. Edge Tracking by Hysteresis
o Connect weak edges to strong edges if they are connected; else discard them.
□ Python Code (Using OpenCV)
python
import cv2
import matplotlib.pyplot as plt

# Read the image in grayscale


image = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Canny Edge Detection


edges = cv2.Canny(image, threshold1=100, threshold2=200)

# Show original and edge-detected image


plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title("Original")

23
plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
plt.title("Canny Edges")
plt.show()
Parameters to Tune:
 threshold1 (low) and threshold2 (high): Control edge sensitivity.
 Kernel size in Gaussian blur: affects noise reduction and detail.
Advantages:
 Low error rate (detects only real edges).
 Good localization (edge position is accurate).
 Minimal response (one edge per object).

24

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy