I have one image with some structure (rectangle) That all of them are approximately green. I used this code to find a number of those objects in the image and make a contour around them. Their hue value will change over time. I need to add one code to extract the average of the new hue value in each counter that we detect in the previous step and save them in a new array. I want to find The average hue value inside the mask(or detected contour) after one hour. Thanks for your attention.
import cv2
import numpy as np
img = cv2.imread("Image-1.PNG")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hue = hsv[:,:,0]
lower_range = np.array([75, 50, 50])
upper_range = np.array([165, 200, 255])
thresh = cv2.inRange(hsv, lower_range, upper_range)
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
N2=0
count = img.copy()
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 160:
cv2.drawContours(count, [cnt], 0, (100, 0, 255), 2)
mask = np.zeros_like(thresh, dtype=np.uint8)
cv2.drawContours(mask, [cnt], 0, (255,255,255), -1)
ave_hue = np.mean(hue[np.where(mask==255)])
print("average hue:", ave_hue, "area:", area)
N2=N2 1
print("Number of section : ", N2)
cv2.imwrite('Image-1-contours.PNG', count)
cv2.imshow('thresh',thresh)
cv2.imshow('hsv',hsv)
cv2.imshow('count',count)
cv2.waitKey(0)
cv2.destroyAllWindows()Result:
Listing:
average hue: 92.6030534351145 area: 227.0
average hue: 92.32921810699588 area: 209.0
average hue: 92.48648648648648 area: 223.5
average hue: 93.0765306122449 area: 165.0
average hue: 92.99335548172758 area: 261.0
average hue: 92.44375 area: 280.0
average hue: 92.94326241134752 area: 244.0
average hue: 92.70454545454545 area: 267.0
average hue: 92.73374613003097 area: 282.5
average hue: 92.90847457627119 area: 255.0
average hue: 93.23026315789474 area: 266.0
average hue: 90.79323308270676 area: 231.0
average hue: 90.33333333333333 area: 253.0
average hue: 90.67405063291139 area: 276.5
average hue: 90.24846625766871 area: 285.0
average hue: 91.09375 area: 278.5
average hue: 90.29577464788733 area: 245.5
average hue: 90.89389067524115 area: 269.5
average hue: 90.12539184952978 area: 278.0
average hue: 90.67202572347267 area: 270.0
average hue: 90.28321678321679 area: 247.0
average hue: 89.54181818181819 area: 239.5
average hue: 89.75565610859728 area: 185.0
Number of section : 23