[CT404]: Finish Assignment 2
This commit is contained in:
@ -0,0 +1,40 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
# Load and pre-process binary image
|
||||
binary_image = cv2.imread("./output/kernel_size_17.jpg", cv2.IMREAD_GRAYSCALE)
|
||||
binary_image = cv2.medianBlur(binary_image, 3)
|
||||
|
||||
# Step 1.5: Extraction of Binary Regions of Interest / connected components
|
||||
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binary_image, connectivity=8)
|
||||
|
||||
# Initialize an empty mask for filtered regions
|
||||
filtered_mask = np.zeros(binary_image.shape, dtype=np.uint8)
|
||||
|
||||
total_globules = 0
|
||||
|
||||
# Task 1.6: Filtering of Fat Globules
|
||||
for i in range(1, num_labels):
|
||||
area = stats[i, cv2.CC_STAT_AREA]
|
||||
|
||||
# Calculate compactness
|
||||
perimeter = cv2.arcLength(cv2.findContours((labels == i).astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0][0], True)
|
||||
compactness = (perimeter ** 2) / area if area > 0 else 0
|
||||
|
||||
if (300 < area) and (compactness < 27):
|
||||
total_globules += 1
|
||||
filtered_mask[labels == i] = 255
|
||||
|
||||
cv2.imwrite("./output/filtered_fat_globules.jpg", filtered_mask)
|
||||
print("Total globules: " + str(total_globules))
|
||||
|
||||
# Task 1.7: Calculation of the Fat Area
|
||||
# Total area of the image in pixels (excluding the background)
|
||||
total_image_area = binary_image.shape[0] * binary_image.shape[1]
|
||||
|
||||
# Total fat area (in pixels)
|
||||
fat_area = np.sum(filtered_mask == 255)
|
||||
|
||||
# Calculate fat percentage
|
||||
fat_percentage = (fat_area / total_image_area) * 100
|
||||
print(f"Fat Area Percentage: {fat_percentage:.2f}%")
|
@ -1,23 +0,0 @@
|
||||
# Task 1.5: Extraction of Binary Regions of Interest / Connected Components
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
# read in noise-reduced image
|
||||
image = cv2.imread("./output/kernel_size_25.jpg", cv2.IMREAD_GRAYSCALE)
|
||||
|
||||
# Find connected components
|
||||
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4)
|
||||
|
||||
# Create an output image (color) to label components
|
||||
output_img = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
|
||||
|
||||
# Apply a single color (e.g., gray) to each component in the output image
|
||||
for label in range(1, num_labels): # Skip background (label 0)
|
||||
output_img[labels == label] = (200, 200, 200) # Light gray color for each component
|
||||
|
||||
# Overlay red text labels at component centroids
|
||||
for i in range(1, num_labels): # Skip background (label 0)
|
||||
x, y = int(centroids[i][0]), int(centroids[i][1])
|
||||
cv2.putText(output_img, str(i), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) # Red color (BGR: (0, 0, 255))
|
||||
|
||||
cv2.imwrite("./output/region_of_interest.jpg", output_img)
|
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
Binary file not shown.
After Width: | Height: | Size: 6.9 KiB |
Reference in New Issue
Block a user