# -*- coding: utf-8 -*-
"""
Created on Tue Mar 24 16:21:26 2020

@author: corey
"""


import cv2
import matplotlib.pyplot as plt
import numpy as np
import imutils

def contrast_stretch(im):
    """
    Performs a simple contrast stretch of the given image, from 5-100%.
    """
    in_min = np.percentile(im, 5)
    in_max = np.percentile(im, 100)

    out_min = 0.0
    out_max = 255.0

    out = im - in_min
    out *= ((out_min - out_max) / (in_min - in_max))
    out += in_min

    return out

file = 'C:/xampp/htdocs/Contracts/shpfiles/Image002.jpg'
img = cv2.imread(file)
b, g, r = cv2.split(img)

# Calculate the NDVI
bottom = (r.astype(float) + b.astype(float))
bottom[bottom == 0] = 0.00001  # Make sure we don't divide by zero!

ndvi = (r.astype(float) - b.astype(float)) / bottom
ndvi = contrast_stretch(ndvi)
 
# perform the actual resizing of the image
ndvi = imutils.resize(ndvi, height=850)
ndvi = ndvi.astype(np.uint8)

# Display
cv2.imshow('NDVI Image', ndvi)
cv2.waitKey(0)
