def find_palms(uid,fieldid, templateName): import cv2 import numpy as np import matplotlib.pyplot as plt from skimage import filters templates = ['template.png', 'template2.png', 'template3.png', 'template4.png','template5.png','template6.png','template7.png'] totalTrees = 0 for templateName in templates: #templateName = 'template.png' filename = uid + '/highres_' +str(fieldid)+'.png' # loading the image - let's take a look at it image = cv2.imread(filename) #plt.imshow(image) # converting the image to hsv color channel - V channel will be useful for us..!! hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) plt.imshow(hsv_image, cmap='hsv') fileName = uid +'/' + fieldid + '_highres_identified.png' plt.savefig(fileName) # Thresholding the imahge in range 120 - 210 with OTSU threshold, binary_image = cv2.threshold(hsv_image[:, :, 2], 120, 210, cv2.THRESH_BINARY | cv2.THRESH_OTSU) #plt.imshow(binary_image, cmap='binary') image_denoised = filters.median(binary_image, selem=np.ones((5, 5))) #plt.imshow(image_denoised, cmap='binary') binary_image = image_denoised # Let's make a kernal of 4x4 for mainpulating the image kernel = np.ones((4, 4), dtype='uint8') #plt.imshow(kernel, cmap='binary') # with the kernal we can do morphological closing to make the regions of interests - trees more dominant morph_closed_image = cv2.morphologyEx(binary_image, cv2.MORPH_CLOSE, kernel) #plt.imshow(morph_closed_image, cmap='binary') # let's go for distance transfrom dist_transform_image = cv2.distanceTransform(morph_closed_image, cv2.DIST_L2, cv2.DIST_MASK_PRECISE) #plt.imshow(dist_transform_image, cmap='binary') # let's give some padding to the image so that further kernal operations will be easy..! border_size = 25 dist_border = cv2.copyMakeBorder(dist_transform_image, border_size, border_size, border_size, border_size, cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0) #plt.imshow(dist_border, cmap='binary') gap = 8 templateName = 'Palm/' + templateName template_file = cv2.imread(templateName) #template_file = cv2.resize(template_file, (64,64)) template_file = cv2.cvtColor(template_file, cv2.COLOR_BGR2HSV) _, kernel2 = cv2.threshold(template_file[:, :, 2], 120, 210, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # median filtering the teamplate also..!! image_denoised = filters.median(kernel2, selem=np.ones((5, 5))) #plt.imshow(image_denoised, cmap='binary') kernel2 = image_denoised # for the opencv to consider the kernal template we made it must be a binary image with 0 and 1 # let's convert the pixel values to 0's and 1's - ofcourse there are better methods :D for i in range(len(kernel2)): for j in range(len(kernel2[i])): if kernel2[i][j] != 0: kernel2[i][j] = 1 kernel2 = cv2.copyMakeBorder(kernel2, gap, gap, gap, gap, cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0) #plt.imshow(kernel2, cmap='binary') dist_trans_template = cv2.distanceTransform(kernel2, cv2.DIST_L2, cv2.DIST_MASK_PRECISE) #plt.imshow(dist_trans_template, cmap='binary') template_matched = cv2.matchTemplate(dist_border, dist_trans_template, cv2.TM_CCOEFF_NORMED) #plt.imshow(template_matched, cmap='binary') # Now let's threshold the template matched image..!! mn, mx, _, _ = cv2.minMaxLoc(template_matched) th, peaks = cv2.threshold(template_matched, 0.1, 0.6, cv2.THRESH_BINARY) # let's go for the peak value in the template matched image..! peaks8u = cv2.convertScaleAbs(peaks) # find the coutnours in the peaks contours, hierarchy = cv2.findContours(peaks8u, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) peaks8u = cv2.convertScaleAbs(peaks) #plt.imshow(peaks8u, cmap='binary') copy = cv2.imread(filename) # Now let's count the contouurs - the trees.!! count = 0 for i in range(len(contours)): if cv2.contourArea(contours[i]) < 10: continue x, y, w, h = cv2.boundingRect(contours[i]) cv2.rectangle(copy, (x, y), (x+w, y+h), (0, 255, 255), 2) #cv2.drawContours(copy, contours, i, (0, 0, 255), 2) count += 1 print('Number of trees : ', count) totalTrees = totalTrees + count # also let's see how the countours are selected by plotting countours on the actual image.!! plt.axis('off') plt.imshow(copy) #plt.show() fileName = uid +'/' + fieldid + '_highres_num.png' plt.savefig(fileName) totalTrees = round(totalTrees/7) print('FinalTrees:') print(totalTrees) return totalTrees