Skip to content

Instantly share code, notes, and snippets.

@jackyyeh5111
Created January 1, 2022 14:03
Show Gist options
  • Save jackyyeh5111/41843780b02f3d337afa8a27d311b19c to your computer and use it in GitHub Desktop.
Save jackyyeh5111/41843780b02f3d337afa8a27d311b19c to your computer and use it in GitHub Desktop.
img_path = 'image/G2/6.jpg'# Detect and remove unrelevant regions (batch).
import cv2
import numpy as np
import pandas as pd
# Parameters.
group_lst = ['G1', 'G2', 'G3', 'G4']
group_dct = {'G1': 19, 'G2': 13, 'G3': 15, 'G4': 16}
kernel = np.ones((5,5), np.uint8)
img_path = 'image/G2/6.jpg'
img = cv2.imread(img_path)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Lower bound and upper bound for Green color.
for i in range(0, 255, 10):
lower_bound = np.array([36, i, 25])
upper_bound = np.array([86, 255, 255])
# Find the colors within the boundaries.
mask = cv2.inRange(hsv, lower_bound, upper_bound)
# # Remove unnecessary noise from mask
# mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
# # Segment only the detected region
segmented_img = cv2.bitwise_and(img, img, mask=mask)
# # Save the segmented image.
# img_path_to_store = 'image/p' + group + '/' + str(j) + '.jpg'
cv2.imwrite('hsv_{}.jpg'.format(str(i)), segmented_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment