0% found this document useful (0 votes)
3 views11 pages

Digital Image Processing Experiments Programs

The document outlines a series of ten digital image processing experiments using Python with OpenCV, NumPy, and Matplotlib. Each experiment includes objectives, concepts, and sample code for tasks such as thresholding, histogram equalization, image rotation, and edge detection. The document serves as a practical guide for implementing various image processing techniques.

Uploaded by

chetanmeena9708
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)
3 views11 pages

Digital Image Processing Experiments Programs

The document outlines a series of ten digital image processing experiments using Python with OpenCV, NumPy, and Matplotlib. Each experiment includes objectives, concepts, and sample code for tasks such as thresholding, histogram equalization, image rotation, and edge detection. The document serves as a practical guide for implementing various image processing techniques.

Uploaded by

chetanmeena9708
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

Digital Image Processing

RTU Laboratory Experiments - Complete Programs

Programs written in Python using OpenCV, NumPy, and Matplotlib.

Prepared for: Experiments 1 to 10

Contents
1. Thresholding an image
2. Image histogram
3. Histogram equalization
4. Rotate an image
5. Scale (resize) an image
6. Translate an image
7. Linear filtering using convolution
8. Spatial and frequency domain filtering
9. Edge detection
10. Morphological operations

Common dependency: pip install opencv-python numpy matplotlib

Digital Image Processing Experiments Page 1


Experiment 1 - Thresholding an Image
Objective: To understand and implement a program for thresholding an image.
Concept: Thresholding converts a grayscale image into a binary image by separating pixels using a chosen cutoff
value.
Program:
import cv2

img = [Link]('[Link]', 0)
if img is None:
print('Image not found')
quit()

_, th = [Link](img, 127, 255, cv2.THRESH_BINARY)


_, th_inv = [Link](img, 127, 255, cv2.THRESH_BINARY_INV)

[Link]('Original', img)
[Link]('Binary', th)
[Link]('Binary Inverse', th_inv)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 2


Experiment 2 - Image Histogram
Objective: To understand and implement a program to obtain the histogram of an image.
Concept: A histogram shows the distribution of pixel intensities in an image.
Program:
import cv2
import [Link] as plt

img = [Link]('[Link]', 0)
if img is None:
print('Image not found')
quit()

hist = [Link]([img], [0], None, [256], [0, 256])

[Link]()
[Link]('Grayscale Histogram')
[Link]('Pixel Value')
[Link]('Frequency')
[Link](hist)
[Link]([0, 256])
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 3


Experiment 3 - Histogram Equalization
Objective: To understand and implement histogram equalization of an image.
Concept: Histogram equalization improves contrast by redistributing intensity values more evenly.
Program:
import cv2

img = [Link]('[Link]', 0)
if img is None:
print('Image not found')
quit()

eq = [Link](img)

[Link]('Original', img)
[Link]('Equalized', eq)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 4


Experiment 4 - Rotate an Image
Objective: To understand and implement a program to rotate an image.
Concept: Rotation is done using an affine transform around the image center.
Program:
import cv2

img = [Link]('[Link]')
if img is None:
print('Image not found')
quit()

h, w = [Link][:2]
mat = cv2.getRotationMatrix2D((w/2, h/2), 45, 1.0)
rot = [Link](img, mat, (w, h))

[Link]('Original', img)
[Link]('Rotated', rot)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 5


Experiment 5 - Scale (Resize) an Image
Objective: To understand and implement a program to scale or resize an image.
Concept: Scaling changes the size of an image using interpolation methods.
Program:
import cv2

img = [Link]('[Link]')
if img is None:
print('Image not found')
quit()

small = [Link](img, None, fx=0.5, fy=0.5,


interpolation=cv2.INTER_AREA)
big = [Link](img, None, fx=2.0, fy=2.0,
interpolation=cv2.INTER_LINEAR)

[Link]('Original', img)
[Link]('Small', small)
[Link]('Big', big)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 6


Experiment 6 - Translate an Image
Objective: To understand and implement a program to translate an image.
Concept: Translation shifts the image horizontally and vertically by a fixed amount.
Program:
import cv2
import numpy as np

img = [Link]('[Link]')
if img is None:
print('Image not found')
quit()

h, w = [Link][:2]
mat = np.float32([[1, 0, 50], [0, 1, 30]])
shift = [Link](img, mat, (w, h))

[Link]('Original', img)
[Link]('Shifted', shift)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 7


Experiment 7 - Linear Filtering Using Convolution
Objective: To understand and implement linear filtering using convolution.
Concept: A convolution mask is applied over the image to smooth or sharpen it.
Program:
import cv2
import numpy as np

def convolve(img, k):


m, n = [Link]
pad = m // 2
out = np.zeros_like(img, dtype=np.float32)
img_p = [Link](img, pad, mode='constant')
for i in range([Link][0]):
for j in range([Link][1]):
roi = img_p[i:i+m, j:j+n]
out[i, j] = [Link](roi * k)
return [Link](out, 0, 255).astype(np.uint8)

img = [Link]('[Link]', 0)
if img is None:
print('Image not found')
quit()

kernel = [Link]([[1, 1, 1],


[1, 1, 1],
[1, 1, 1]], dtype=np.float32) / 9
res = convolve(img, kernel)

[Link]('Original', img)
[Link]('Filtered', res)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 8


Experiment 8 - Spatial and Frequency Domain Filtering
Objective: To understand and implement image filtering in spatial and frequency domain.
Concept: Spatial filtering uses neighborhood masks, while frequency filtering works on the Fourier transform of the
image.
Program:
import cv2
import numpy as np

img = [Link]('[Link]', 0)
if img is None:
print('Image not found')
quit()

spatial = [Link](img, (5, 5))

f = [Link].fft2(img)
fshift = [Link](f)
rows, cols = [Link]
crow, ccol = rows // 2, cols // 2
r = 40
mask = [Link]((rows, cols), np.uint8)
y, x = [Link][:rows, :cols]
mask[((y-crow)**2 + (x-ccol)**2) <= r*r] = 1

fshift = fshift * mask


back = [Link].ifft2([Link](fshift))
freq = [Link](back)
freq = np.uint8([Link](freq, 0, 255))

[Link]('Original', img)
[Link]('Spatial Filter', spatial)
[Link]('Frequency Filter', freq)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 9


Experiment 9 - Edge Detection
Objective: To understand and implement a program for edge detection in an image.
Concept: Edge detection finds sharp intensity changes using operators such as Sobel or Canny.
Program:
import cv2

img = [Link]('[Link]', 0)
if img is None:
print('Image not found')
quit()

sx = [Link](img, cv2.CV_64F, 1, 0, ksize=3)


sy = [Link](img, cv2.CV_64F, 0, 1, ksize=3)
sobel = [Link](sx, sy)
sobel = [Link](sobel)
canny = [Link](img, 100, 200)

[Link]('Original', img)
[Link]('Sobel', sobel)
[Link]('Canny', canny)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 10


Experiment 10 - Morphological Operations
Objective: To understand and implement morphological operations in analyzing image structures.
Concept: Morphological operations modify binary image shapes using a structuring element.
Program:
import cv2
import numpy as np

img = [Link]('[Link]', 0)
if img is None:
print('Image not found')
quit()

_, bin_img = [Link](img, 127, 255, cv2.THRESH_BINARY)


kernel = [Link]((5, 5), np.uint8)

erode = [Link](bin_img, kernel, iterations=1)


dilate = [Link](bin_img, kernel, iterations=1)
open_img = [Link](bin_img, cv2.MORPH_OPEN, kernel)
close_img = [Link](bin_img, cv2.MORPH_CLOSE, kernel)

[Link]('Original', bin_img)
[Link]('Erosion', erode)
[Link]('Dilation', dilate)
[Link]('Opening', open_img)
[Link]('Closing', close_img)
[Link](0)
[Link]()

Note: Replace [Link] with your own image file path before running.

Digital Image Processing Experiments Page 11

You might also like