# Determining skeleton Centerline of the fish

I have implemented the below code to find the skeleton centerline of the fish; but due to the shape of the fish and other disturbances, I’m getting other branches attached to the center line.

How can i remove those extra branches.

``````import numpy as np
from skimage import morphology
from scipy.spatial import Voronoi
import matplotlib.pyplot as plt

binary_fish = cv2.imread('/content/data/fish12-masked.jpg', cv2.IMREAD_GRAYSCALE)

# Step 1: Preprocessing
# Apply erosion and dilation to the binary fish image
kernel = np.ones((3, 3), np.uint8)
binary_fish = cv2.erode(binary_fish, kernel, iterations=1)
binary_fish = cv2.dilate(binary_fish, kernel, iterations=1)

# Step 2: Threshold to create a binary image
_, binary_fish = cv2.threshold(binary_fish, 128, 255, cv2.THRESH_BINARY)

# Step 3: Skeletonize the binary fish image
skeleton = morphology.skeletonize(binary_fish > 0)

# Step 4: Find the centerline from the skeleton
skeleton = skeleton.astype(np.uint8) * 255

# Find the contours of the skeleton
contours, _ = cv2.findContours(skeleton, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Extract the largest contour (assuming it's the centerline)
centerline = max(contours, key=cv2.contourArea)

# Create a blank canvas
height, width = binary_fish.shape
centerline_image = np.zeros((height, width), dtype=np.uint8)

# Draw the centerline on the canvas
cv2.drawContours(centerline_image, [centerline], -1, 255, 1)

# Overlay the centerline on the original fish image
original_fish = cv2.imread('/content/data/fish12-masked.jpg')
overlay_image = original_fish.copy()
overlay_image[centerline_image > 0] = [0, 0, 255]  # Red color

# Display or save the image with the centerline
cv2_imshow(overlay_image)
``````