0% found this document useful (0 votes)
10 views

Lab 9 (1)

The document outlines a lab session for coin detection using image processing techniques in Python with OpenCV. It details steps including converting an image to grayscale, applying Gaussian blur, thresholding, performing morphological operations, and detecting contours, resulting in the detection of 42 coins. Additionally, it explains the functions cv2.erode() and cv2.dilate() for morphological operations to clean up images by removing noise and enhancing features.

Uploaded by

Abdul Rasheed
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)
10 views

Lab 9 (1)

The document outlines a lab session for coin detection using image processing techniques in Python with OpenCV. It details steps including converting an image to grayscale, applying Gaussian blur, thresholding, performing morphological operations, and detecting contours, resulting in the detection of 42 coins. Additionally, it explains the functions cv2.erode() and cv2.dilate() for morphological operations to clean up images by removing noise and enhancing features.

Uploaded by

Abdul Rasheed
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/ 4

Lab Session 9

Ans 1
Step 1: Read and Convert Image to Grayscale
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('coins.jpg') # Replace with actual path
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray, cmap='gray')
plt.title("Grayscale Image")
plt.axis("off")
plt.show()

Step 2: Apply Gaussian Blur


blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(blurred, cmap='gray')
plt.title("Blurred Image")
plt.axis("off")
plt.show()

Step 3: Thresholding
_, thresh = cv2.threshold(blurred, 130, 255, cv2.THRESH_BINARY_INV)
plt.imshow(thresh, cmap='gray')
plt.title("Thresholded Image")
plt.axis("off")
plt.show()

Step 4: Morphological Operations (optional)


kernel =
cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
morphed = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
plt.imshow(morphed, cmap='gray')
plt.title("After Morphological Operation")
plt.axis("off")
plt.show()

Step 5: Find and Draw Contours


contours, _ = cv2.findContours(morphed, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
output = img.copy()
cv2.drawContours(output, contours, -1, (0,255,0), 2)
plt.imshow(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
plt.title(f"Coins Detected: {len(contours)}")
plt.axis("off")
plt.show()

Processed Images

Here are the processed images showing each step of the coin detection pipeline:

1. Grayscale – Converted to black and white to simplify detection.


2. Blurred – Smoothed to reduce noise.
3. Threshold – Binary image to highlight the coin-like structures.
4. Morphology – Cleaned using morphological operations.
5. Contours Detected – 42 coins were detected in total.
Images
Ans 2
The cv2.erode() and cv2.dilate() functions are morphological operations used to process
binary or grayscale images. cv2.erode() shrinks the white regions in an image, helping to
remove small white noise and separate connected objects by eroding their boundaries. In
contrast, cv2.dilate() expands white regions, useful for filling small holes, connecting
broken parts of objects, and making features more prominent. When used together, erosion
followed by dilation (opening) or vice versa (closing), they help clean up an image by reducing
noise or closing gaps in detected objects.

You might also like