Created
November 19, 2023 00:52
-
-
Save jere357/54354c16d1a9e7d13215e6abba0e6479 to your computer and use it in GitHub Desktop.
extracting green plant columns from the field image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import matplotlib.pyplot as plt | |
import numpy as np | |
img = cv2.imread("field.jpg") | |
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
lower_bound = np.array([35, 50, 50]) # lower bound green in HSV | |
upper_bound = np.array([75, 255, 255]) # upper bound green in HSV | |
mask = cv2.inRange(img_hsv, lower_bound, upper_bound) # this mask will filter out everything that is not green, green being defined by the lower and upper bounds in the HSV space | |
segmented_image = cv2.bitwise_and(img_hsv, img_hsv, mask=mask) # extract the green things from the image using the mask | |
segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_HSV2BGR) | |
rotation_matrix = cv2.getRotationMatrix2D((img.shape[1]/2, img.shape[0]/2), -35, 1) | |
rotated_image = cv2.warpAffine(segmented_image, rotation_matrix, (img.shape[1], img.shape[0])) | |
# draw a histogram of the green values in the rotated_image along the x axis | |
green_values = [] | |
for i in range(rotated_image.shape[1]): | |
for j in range(rotated_image.shape[0]): | |
if rotated_image[j, i, 1] > 0: # we are back in the BGR format | |
green_values.append(i) | |
f, plots = plt.subplots(2,1) | |
plots[0].hist(green_values, bins=range(0, rotated_image.shape[1], 1)) | |
plots[1].imshow(rotated_image) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment