Created
August 8, 2018 12:29
-
-
Save strrife/ffca7e183735e1c9b14c3999ea15ae60 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import numpy as np | |
import matplotlib | |
import argparse | |
import imutils | |
import cv2 | |
import numpy as np | |
import sys | |
import matplotlib.pyplot as plt | |
def mask_range(hsv, from_hue, to_hue): | |
our_range = [np.array([from_hue, 40, 40]), np.array([to_hue, 255, 255])] | |
mask = cv2.inRange(hsv, our_range[0], our_range[1]) | |
# return mask | |
return cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8)) | |
# kernel = x | |
# mask = cv2.erode(mask, kernel, iterations=1) | |
def cv_hsv_2_wavelen(x): | |
return 625 - int(2 * x * 175. / 240.) | |
def count_hues(hsv2): | |
unique, counts = np.unique(np.asarray(hsv2[..., 0]), return_counts=True) | |
res = dict(zip(unique, counts)) | |
res[0] = 0 | |
return res | |
def add_cover_mask(enter_img, mask, height, width, color_scheme): | |
img = enter_img.copy() | |
roi = img[0:height, 0:width] | |
mask_inv = cv2.bitwise_not(mask) | |
color_img = np.zeros(img.shape, img.dtype) | |
color_img[:, :] = color_scheme | |
red_mask = cv2.bitwise_and(color_img, color_img, mask=mask) | |
img[0:height, 0:width] = cv2.add(red_mask, cv2.bitwise_and(roi, roi, mask=mask_inv)) | |
return img | |
def get_trees(image): | |
green = (16, 82) | |
real_green = (20, 59) | |
img = cv2.imread(image) | |
width = 1500 | |
height = 1000 | |
# height, width, channels = img.shape | |
# Resize | |
img = cv2.resize(img, (width, height)) | |
cv2.imwrite(image.replace(".jpg", ".resized.jpg").replace(".JPG", ".resized.jpg"), img) | |
# Noise removal | |
dst = cv2.fastNlMeansDenoisingColored(img, None, 5, 5) | |
cv2.imwrite(image.replace(".jpg", ".blurred1.jpg").replace(".JPG", ".blurred1.jpg"), dst) | |
# dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10) | |
# cv2.imwrite(image.replace(".jpg", ".blurred2.jpg").replace(".JPG", ".blurred2.jpg"), dst) | |
# dst = cv2.filter2D(img, -1, np.array([[1, 2, 1], [2, 8, 2], [1, 2, 1]], np.float32) / 20) | |
# cv2.imwrite(image.replace(".jpg", ".blurred3.jpg").replace(".JPG", ".blurred3.jpg"), dst) | |
# dst = cv2.filter2D(img, -1, np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], np.float32) / 9) | |
# cv2.imwrite(image.replace(".jpg", ".blurred4.jpg").replace(".JPG", ".blurred4.jpg"), dst) | |
img = dst | |
# height, width, channels = img.shape | |
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
mask_tree = mask_range(hsv, green[0], green[1]) | |
mask_healthy = mask_range(hsv, real_green[0], real_green[1]) | |
# Render extracted tree image | |
extracted = add_cover_mask(img, cv2.bitwise_not(mask_tree), height, width, (255, 255, 255)) | |
cv2.imwrite(image.replace(".jpg", ".extracted-mask.jpg").replace(".JPG", ".extracted-mask.jpg"), mask_tree) | |
cv2.imwrite(image.replace(".jpg", ".extracted.jpg").replace(".JPG", ".extracted.jpg"), extracted) | |
# Render extracted healthy image | |
extracted = add_cover_mask(extracted, cv2.bitwise_not(mask_healthy), height, width, (255, 255, 255)) | |
cv2.imwrite(image.replace(".jpg", ".extracted-g.jpg").replace(".JPG", ".extracted-g.jpg"), extracted) | |
cv2.imwrite(image.replace(".jpg", ".extracted-g-mask.jpg").replace(".JPG", ".extracted-g-mask.jpg"), mask_healthy) | |
img2 = np.invert(img*mask_tree[:,:,np.newaxis]) | |
# HSV paletter | |
hsv2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV) | |
# cv2.imwrite(image.replace(".jpg", ".extracted.jpg").replace(".JPG", ".extracted.jpg"), img2) | |
# Mask extraction | |
img3 = np.invert(img*mask_healthy[:,:,np.newaxis]) | |
# hsv2 = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV) | |
cv2.imwrite(image.replace(".jpg", ".extracted-g.jpg").replace(".JPG", ".extracted-g.jpg"), img3) | |
#ddddddddd | |
# print img.shape | |
# img4 = cv2.addWeighted(redMask, 1, img, 1, 0, img) | |
mask_bad = cv2.bitwise_xor(mask_tree, mask_healthy) | |
img4 = add_cover_mask(img, mask_bad, height, width, (0, 0, 255)) | |
cv2.imwrite(image.replace(".jpg", ".extracted-bad.jpg").replace(".JPG", ".extracted-bad.jpg"), img4) | |
res = count_hues(hsv2) | |
active_area = np.count_nonzero(mask_tree) | |
green_area = np.count_nonzero(mask_healthy) | |
fig = plt.figure() | |
fig.suptitle(image, fontsize=14, fontweight='bold') | |
green_percent = int(green_area * 10000. / active_area) * 1. / 100 | |
if green_percent > 94: | |
condition = "excellent"; | |
elif green_percent > 79 : | |
condition = "good" | |
elif green_percent > 62 : | |
condition = "satisfactory" | |
elif green_percent > 44 : | |
condition = "poor" | |
else : | |
condition = "terminal" | |
plt.title("(active area: " + str(int(active_area * 10000. / np.size(mask_tree)) * 1. / 100) + "%, green: " + str(green_percent) + "%, condition: " + condition + ")", fontsize=11) | |
ax = fig.add_subplot(111) | |
ax.set_xlabel('Wavelength') | |
ax.set_ylabel('% in active area') | |
plt.bar( | |
range(len(res)), | |
np.asarray(res.values()) * 100. / active_area, | |
align='center' | |
) | |
wavelengths_for_axis_legend = np.asarray(map(lambda x: x * 10, range(int(green[0] / 10), int(green[1] / 10)))) | |
plt.xticks(wavelengths_for_axis_legend, reversed(map(cv_hsv_2_wavelen, wavelengths_for_axis_legend))) | |
plt.savefig(image.replace(".jpg", ".plot.png").replace(".JPG", ".plot.png")) | |
if __name__ == "__main__": | |
get_trees(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment