Hi, i have been working on a project where i have to find wires in the image. So, basically, i am working on a semi-conductor project where i have to find all existing wires. i have successfully found all possible contours in the image, whether it be wire or non-wire. I try to filter out non wire and only keep the wire contours using image size and opencv method. However, i tried using morphology ex to filter out non wire parts, it did great but some wire contours also went missing as well. but if i dont use morpho ex to filter, non-wire contours will appear, i dont know any methods i can use to filter non-wire. Below is what my morpho ex looks like:
using exactly this, i have filtered out non-wire and kept wire contours, but some wire contours went missing using this
# Load the image
image = cv2.imread(image_path)
img_with_annotations = np.copy(image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform Canny Edge Detection
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
# Create a mask for the central region to preserve wires in that area
mask = np.zeros_like(edges)
center_x, center_y = gray.shape[1] // 2, gray.shape[0] // 2
radius = int(min(gray.shape[1], gray.shape[0]) * 0.35)
cv2.circle(mask, (center_x, center_y), radius, 255, thickness=-1)
# Define the kernels for morphological operations
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 5))
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 1))
# Apply morphological operations to clean up noise
detected_horizontal_lines = cv2.morphologyEx(edges, cv2.MORPH_OPEN, horizontal_kernel)
detected_vertical_lines = cv2.morphologyEx(edges, cv2.MORPH_OPEN, vertical_kernel)
# Combine horizontal and vertical lines
combined_lines = cv2.add(detected_horizontal_lines, detected_vertical_lines)
# Dilate the lines to strengthen them
dilated_lines = cv2.dilate(combined_lines, np.ones((3, 3), np.uint8), iterations=2)
# Remove the dilated lines from the original edges
outside_region_edges = cv2.subtract(edges, dilated_lines)
# Apply the mask to keep only edges within the central region
outside_region_edges = cv2.bitwise_and(outside_region_edges, cv2.bitwise_not(mask))
final_edges = cv2.bitwise_or(outside_region_edges, cv2.bitwise_and(edges, mask))
# Use the original edges without morphological operations for testing
#final_edges = edges # Use this line if you have commented out the morpho operations
# Find all contours in the final edges
contours, _ = cv2.findContours(final_edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print(f"Total contours found in {filename}: {len(contours)}")