|
| 1 | +import cv2 |
| 2 | +from collections import deque |
| 3 | + |
| 4 | +def flood_fill(image, start_point, new_color): |
| 5 | + rows, cols, _ = image.shape |
| 6 | + visited = set() |
| 7 | + queue = deque([start_point]) |
| 8 | + |
| 9 | + start_color = image[start_point[1], start_point[0]] |
| 10 | + |
| 11 | + while queue: |
| 12 | + x, y = queue.popleft() |
| 13 | + if (0 <= x < cols and 0 <= y < rows and |
| 14 | + (x, y) not in visited and |
| 15 | + (image[y, x] == start_color).all()): |
| 16 | + image[y, x] = new_color |
| 17 | + visited.add((x, y)) |
| 18 | + queue.extend([(x+1, y), (x-1, y), (x, y+1), (x, y-1)]) |
| 19 | + |
| 20 | +def main(): |
| 21 | + |
| 22 | + image = cv2.imread('dog.jpeg') |
| 23 | + |
| 24 | + |
| 25 | + grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
| 26 | + |
| 27 | + # starting point for flood-fill operation |
| 28 | + start_point = (100, 100) |
| 29 | + |
| 30 | + # new color for flooded region |
| 31 | + new_color = (0, 255, 0) # Green color |
| 32 | + |
| 33 | + # flood-fill operation |
| 34 | + flood_fill(image, start_point, new_color) |
| 35 | + |
| 36 | + # Display |
| 37 | + cv2.imshow('Segmented Image', grayscale_image) |
| 38 | + cv2.imshow('Original Image', image) |
| 39 | + cv2.waitKey(0) |
| 40 | + cv2.destroyAllWindows() |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + main() |
0 commit comments