This is the description of the task that i am working on
first procedure (image processing)
1st step: Pre-process the Images
Convert images to grayscale to simplify the analysis.
Apply filters to reduce noise.
Perform thresholding to create a binary image where holes are distinguishable from the background.
2nd step: Detect Holes
Use contour detection to find holes in the binary image.
For closely situated holes, apply morphological operations or the watershed algorithm to separate
connected holes.
3rd step: Measure Holes
Approximate each detected contour to the closest circle or ellipse to determine the center and
radius/diameter.
Calculate the area of each hole using the radius or ellipse parameters.
4th Step: Calculate Distribution
Compute the distribution of holes across the image (e.g., hole area as a percentage of total area).
Perform spatial analysis to assess the clustering of holes.
5th Step: Data Visualization
Use Matplotlib or Seaborn to create histograms, scatter plots, and heatmaps of the distribution,
number, and size of holes
I have written some code but it is not working as expected
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
def preprocess_image(image_path):
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
blurred = cv2.GaussianBlur(image, (3, 3), 0)
_, binary_image = cv2.threshold(blurred, 50, 255, cv2.THRESH_BINARY)
plt.imshow(binary_image, cmap='gray')
plt.title('Binary Image')
plt.axis('off')
plt.show()
return binary_image
def detect_holes(binary_image):
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
cv2.drawContours(binary_image, [contour], -1, (0, 255, 255), 1)
plt.figure(figsize=(10, 8))
plt.imshow(binary_image, cmap='gray')
plt.title('Detected Holes')
plt.axis('off')
plt.show()
return contours
def measure_holes(contours):
hole_data = []
for contour in contours:
(x, y), radius = cv2.minEnclosingCircle(contour)
center = (int(x), int(y))
radius = int(radius)
area = cv2.contourArea(contour)
hole_data.append((center, radius, area))
return hole_data
def calculate_distribution(hole_data, total_area):
areas = [hole[2] for hole in hole_data]
total_hole_area = sum(areas)
hole_area_percentage = (total_hole_area / total_area) * 100
return hole_area_percentage
def visualize_data(image, hole_data, contours):
image_with_contours = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
for hole in hole_data:
center, radius, _ = hole
cv2.circle(image_with_contours, center, radius, (0, 255, 0), 2)
plt.imshow(image_with_contours, cmap='gray')
plt.title('Annotated Image with Hole Data')
plt.axis('off')
plt.show()
folder_path = "data"
total_area = 0
for filename in os.listdir(folder_path):
if filename.endswith(".tif") or filename.endswith(".tiff"):
image_path = os.path.join(folder_path, filename)
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
binary_image = preprocess_image(image_path)
contours = detect_holes(binary_image)
hole_data = measure_holes(contours)
total_area = image.shape[0] * image.shape[1]
hole_area_percentage = calculate_distribution(hole_data, total_area)
print("Percentage of hole area:", hole_area_percentage)
visualize_data(image, hole_data, contours)
break
input image
output image
I need help in making the holes detect either in white color or also the circular holes in possible using code i tried for almost a day and did’nt get any good result
I tried the above code to detect the holes in the image texture and failed to get the required result. I am expecting a code which will detect the holes from the image