-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasking.py
More file actions
25 lines (19 loc) · 792 Bytes
/
masking.py
File metadata and controls
25 lines (19 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import cv2
import numpy as np
# Load the images
colored_img = cv2.imread('./selected.jpg')
# green_img = cv2.imread('green_image.jpg')
# Convert the colored image to HSV color space
hsv_img = cv2.cvtColor(colored_img, cv2.COLOR_BGR2HSV)
# Define the lower and upper range for green color in HSV space
lower_green = np.array([35, 100, 100])
upper_green = np.array([85, 255, 255])
# Create a mask where green pixels in the colored image are white and all other pixels are black
mask = cv2.inRange(hsv_img, lower_green, upper_green)
# Apply the mask to the colored image
green_areas = cv2.bitwise_and(colored_img, colored_img, mask=mask)
# Display the resulting image
cv2.imshow('Green Areas', green_areas)
cv2.imwrite('green_areas.jpg', green_areas)
cv2.waitKey(0)
cv2.destroyAllWindows()