hello everyone
im new in python
i want to get this image

this is wall image segmented by network
and seperate each wall like this with canny

in summary i want seperate each wall with canny python and fill them like that
can anybody help me to do this?

Amira BedhiafiRank #601 · 4K reputationPosted Mar 3, 2025, 8:31 AM EST
Load your image and convert it to grayscale, as edge detection works on single-channel images.
import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('image.png') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)Then use the Canny algorithm to detect edges in the image.
edges = cv2.Canny(gray, threshold1=100, threshold2=200)Find contours from the edges detected. These contours will help you identify separate walls.
Draw each contour and fill them with a color.
filled_image = np.zeros_like(image) for contour in contours: cv2.drawContours(filled_image, [contour], -1, (255, 255, 255), thickness=cv2.FILLED)Then display the original image, edges, and the filled contours.
plt.figure(figsize=(10, 5)) plt.subplot(1, 3, 1) plt.title('Original Image') plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.subplot(1, 3, 2) plt.title('Canny Edges') plt.imshow(edges, cmap='gray') plt.subplot(1, 3, 3) plt.title('Filled Walls') plt.imshow(cv2.cvtColor(filled_image, cv2.COLOR_BGR2RGB)) plt.show()