import numpy as np
import cv2
from PIL import Image
import urllib.parse
import urllib.request
import io
import matplotlib.pyplot as plt
from matplotlib import colors
from math import log, exp, tan, atan, pi, ceil
from calculate import distance
#from place_lookup import find_coordinates
#from calc_area import afforestation_area
#300
# deg 0
def afforestation_area():
img = cv2.imread('weeds1.jpg')
shifted = cv2.pyrMeanShiftFiltering(img,7,30)
gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
hsv = cv2.cvtColor(shifted,cv2.COLOR_BGR2HSV)
lower_trees = np.array([10,0,10])
higher_trees = np.array([180,180,75])
lower_houses = np.array([90,10,100])
higher_houses = np.array([255,255,255])
lower_roads = np.array([90,10,100])
higher_roads = np.array([100,100,100])
lower_feilds = np.array([20,20,70])
higher_feilds = np.array([90,255,255])
lower_feilds_blue = np.array([0,80,100])
higher_feilds_blue = np.array([255,250,255])
masktree = cv2.inRange(hsv,lower_trees,higher_trees)
maskhouses = cv2.inRange(hsv,lower_houses,higher_houses)
maskroads = cv2.inRange(hsv,lower_roads,higher_roads)
maskfeilds_houses = cv2.inRange(hsv,lower_feilds,higher_feilds)
blue_limiter = cv2.inRange(hsv,lower_feilds_blue,higher_feilds_blue)
maskfeilds = maskfeilds_houses
res = cv2.bitwise_and(img,img,mask=maskfeilds)
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh_img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
print(res.shape) # (640, 622, 3)
print(np.count_nonzero(res)) # 679089
print("number of pixels", res.size//3)
tot_pixels = res.size//3
# print("number of pixels: row x col", res.)
no_of_non_zero_pixels_rgb = np.count_nonzero(res)
row, col, channels = res.shape # 152886
print("percentage of free land : ", (no_of_non_zero_pixels_rgb/(row*col*channels))) # 0.5686369573954984
percentage_of_land = no_of_non_zero_pixels_rgb/(row*col*channels)
# https://www.unitconverters.net/typography/centimeter-to-pixel-x.htm
# says 1 cm = 37.795275591 pixels
cm_2_pixel = 37.795275591
print("row in cm ", row/cm_2_pixel)
print("col in cm ", col/cm_2_pixel)
row_cm = row/cm_2_pixel
col_cm = col/cm_2_pixel
tot_area_cm = tot_pixels/(row_cm*col_cm)
tot_area_cm_land = tot_area_cm*percentage_of_land
print("Total area in cm^2 : ", tot_area_cm_land)
# in google maps 2.2cm = 50m => 1cm = 22.727272727272727 m in real life at zoom 18
# 1cm^2 = (22.727272727272727m)^2 = 516.5289256198347 m^2
print("Total area in m^2 : ", tot_area_cm_land*(516.5289256198347))
tot_area_m_actual_land = tot_area_cm_land*(516.5289256198347)
# 1 m^2 = 0.000247105 acres :: source Google
tot_area_acre_land = tot_area_m_actual_land*0.000247105
print("Total area in acres : ", tot_area_acre_land)
# https://www.treeplantation.com/tree-spacing-calculator.html
# says if you have 2 ft between rows, and 2ft between trees will can take 10890 trees per acre.
number_of_trees = tot_area_acre_land*10890
print(f"{round(number_of_trees)} number of trees can be planted in {tot_area_acre_land} acres.")
#show the output image
true_area = 0 #threshold
#cnts, _ = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours, hierarchy = cv2.findContours(maskfeilds_houses.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
max_area = 0;
tot_area = 0;
#tot2_area = cv2.contourArea(contours)
for cnt in contours:
area = cv2.contourArea(cnt)
tot_area += area
max_area = max(area, max_area)
#approx = cv2.contourArea(cnt)
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(thresh_img,(x,y),(x+w,y+h),(0,255,0),2)
if(w*h > true_area):
# true_width = w
# true_height = h
# midpoint_x = (x + x + w)/2
# midpoint_y = (y + y + h)/2
#cv2.putText(res, "w={},h={}".format(w,h), (x,y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)
#real_cnt = cnt
true_area = w*h
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
#cv2.drawContours(res,[box],0,(0,0,255),2)
((x,y), (width, height), angle) = rect
print("Max area: " + str(max_area))
print("Total area: " + str(tot_area))
#print("Distance to center of contour")
#print(tot2_area)
output = cv2.drawContours(res, contours, -1, (0, 0, 255), 3)
cv2.imshow('res',res)
cv2.imshow('binary', thresh_img)
c = max(contours, key = cv2.contourArea)
x,y,w,h = cv2.boundingRect(c)
# draw the biggest contour (c) in green
cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2)
cv2.putText(res, "w={},h={}".format(w,h), (x,y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
center = "center: (" + str(x+w/2) + ", " + str(y+h/2) + ")"
cv2.putText(res, center, (int((x+w/2))-50,int((y+h/2))), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
d = distance(x, y, img.shape[1], img.shape[0], 45, 45, 10)
print(d)
print(img.shape[1], img.shape[0])
cv2.imshow("Output", output)
# h, s, v = cv2.split(res)
# fig = plt.figure()
# axis = fig.add_subplot(1, 1, 1, projection="3d")
# pixel_colors = img.reshape((np.shape(img)[0]*np.shape(img)[1], 3))
# norm = colors.Normalize(vmin=-1.,vmax=1.)
# norm.autoscale(pixel_colors)
# pixel_colors = norm(pixel_colors).tolist()
# axis.scatter(h.flatten(), s.flatten(), v.flatten(), facecolors=pixel_colors, marker=".")
# axis.set_xlabel("Hue")
# axis.set_ylabel("Saturation")
# axis.set_zlabel("Value")
# plt.show()
cv2.imshow('mask',maskfeilds)
#cv2.imshow('img', img)
#cv2.imshow("hsv", hsv)
cv2.waitKey(delay=0)
cv2.destroyAllWindows()
return tot_area_acre_land, round(number_of_trees)
print("hi")
afforestation_area()
# if __name__ == "__main__":
# main()
Comments