Image processing Lab
Image processing Lab
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');
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');
2
3
Experiment 2
AIM: Implementation of Relationships between Pixels
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
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');
% 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
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');
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]);
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');
14
15
Experiment 10
AIM: Image Compression by DCT, DPCM, HUFFMAN coding
□ 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.
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
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()
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.
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