Holes detection using computer vision technique

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật