-
Notifications
You must be signed in to change notification settings - Fork 0
/
percentGreen.py
50 lines (37 loc) · 1.8 KB
/
percentGreen.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 9 07:59:12 2019
@author: kyle
"""
import numpy as np
def percentGreen(image, lightboxHue = None, turfHue = None,
turfSaturation = None):
"""
value = percent_green(image, lightboxHue, turfHue, turfSaturation)
Takes a HSV image, and returns a value of percent green and a mask of
turf pixels.
Equation:
value = green pixels with turfHue AND turfSaturation range
/ (total pixels - colorboxHue range)
INPUT:
image: a scikit image, 3D-array with [:, :, 0] being hue, [:,:,1] is
saturation, [:, : ,2] is value
lightBoxHue: a list of two values from min hue to max hue in range
0 to 255
turfHue: a list of two values from min hue to max hue in range
0 to 255
turfSaturation: a list of two values from min saturation to max saturation
in 0 to 255
"""
# hue and saturation values are range 0-179, need to scale it to
# 0-255 for what is commonly in literature when using fiji/imagej
hue_img = np.around(((image[:, :, 0] / 179) * 255),0)
saturation_img = np.around((image[:, :, 1] * 255),0)
# create a threshold mask that refer to turfgrass that match the hue
# and saturation range
turf_mask = ((hue_img > turfHue[0]) & (hue_img < turfHue[1])) * ((saturation_img
> turfSaturation[0]) & (saturation_img < turfSaturation[1]))
lightbox_mask = ((hue_img > lightboxHue[0]) & (hue_img < lightboxHue[1]))
# sum is used twice to sum in both directions of the array
value = sum(sum(turf_mask)) / (np.size(hue_img) - sum(sum(lightbox_mask)))
return (value, turf_mask, lightbox_mask)