def analyzeTeaBuds(project_name,log_file): image = cv2.imread(sysconf.getRGBMapPath(project_name,'rgb_map_final.png')) sysconf.writeLogRecord("RGB Map path : " + sysconf.getRGBMapPath(project_name,'rgb_map_final.png'),log_file) print(sysconf.getRGBMapPath(project_name,'rgb_map_final.png')) i_height,i_width,i_channels = image.shape # Resize image and make a copy of the original (resized) image. if i_width > 0 : height = int((i_width / image.shape[1]) * image.shape[0]) image = cv2.resize(image, (i_width, height), interpolation=cv2.INTER_AREA) orig = image.copy() # Change image color space, if necessary. image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Flatten the 2D image array into an MxN feature vector, where M is # the number of pixels and N is the dimension (number of channels). reshaped = image.reshape(image.shape[0] * image.shape[1], image.shape[2]) # Perform K-means clustering. numClusters = max(2,3) kmeans = KMeans(n_clusters=numClusters, n_init=40, max_iter=500).fit(reshaped) # Reshape result back into a 2D array, where each element represents the clustering = np.reshape(np.array(kmeans.labels_, dtype=np.uint8), (image.shape[0], image.shape[1])) # Sort the cluster labels in order of the frequency with which they occur. sortedLabels = sorted([n for n in range(numClusters)], key=lambda x: -np.sum(clustering == x)) # Initialize K-means grayscale image; set pixel colors based on clustering. kmeansImage = np.zeros(image.shape[:2], dtype=np.uint8) for i, label in enumerate(sortedLabels): kmeansImage[clustering == label] = int((255) / (numClusters - 1)) * i # Concatenate original image and K-means image, separated by a gray strip. concatImage = np.concatenate((orig, 193 * np.ones((orig.shape[0], int(0.0625 * orig.shape[1]), 3), dtype=np.uint8), cv2.cvtColor(kmeansImage, cv2.COLOR_GRAY2BGR)), axis=1) #cv2.imshow('Original vs clustered', concatImage) sysconf.writeLogRecord('Clustered RGB map path : ' + sysconf.getTeaBudIndetifiedMapPath(project_name,'rgb_processed.png'),log_file) cv2.imwrite(sysconf.getTeaBudIndetifiedMapPath(project_name,'rgb_processed_before.png'),kmeansImage) image = cv2.imread(sysconf.getTeaBudIndetifiedMapPath(project_name,'rgb_processed_before.png'),1) image = cv2.cvtColor(image,cv2.COLOR_BGR2BGRA) width = image.shape[1] height = image.shape[0] for i in range(height): for j in range(width): if((image[i][j][0] != 127) & (image[i][j][1] != 127) & (image[i][j][2] != 127)): image[i][j] = (0,0,0,0) cv2.imwrite(sysconf.getTeaBudIndetifiedMapPath(project_name,'rgb_processed.png'),image)
def geotagImage(source_folder,source_file_name,target_folder,targer_file_name,coordinate,log_file): gps_coordinate = "" command = "" if(coordinate == "latitude"): command = "exiftool -tagsfromfile " + source_folder + "/" + source_file_name + " -GPSLatitude " + target_folder + "/" + targer_file_name elif(coordinate == "longtitude"): command = "exiftool -tagsfromfile " + source_folder + "/" + source_file_name + " -GPSLongitude " + target_folder + "/" + targer_file_name # print(command) os.system(command) sysconf.writeLogRecord('Geotagging coordinate type : ' + coordinate,log_file) sysconf.writeLogRecord('Geotagging coordinate command : ' + command,log_file)
def createGeoTiff(project_name,mode,log_file): rawMapPath = '' resultantMapName = [] if (mode == 'rgb'): rawMapPath = sysconf.getRGBRawMapPath(project_name) resultantMapName =[sysconf.getRGBMapPath(project_name,'rgb_map_final.png')] elif (mode == 'nir'): rawMapPath = sysconf.getNIRRawMapPath(project_name) resultantMapName = [sysconf.getNIRMapPath(project_name,'nir_map_final.png')] elif (mode == 'ndvi'): rawMapPath = sysconf.getRGBMapPath(project_name,'rgb_map_final.tif') resultantMapName = [sysconf.getNDVIMapPath(project_name,'ndvi_map_final_red_layer.png'),sysconf.getNDVIMapPath(project_name,'ndvi_map_final_green_layer.png'),sysconf.getNDVIMapPath(project_name,'ndvi_map_final_blue_layer.png')] elif (mode == 'cluster'): rawMapPath = sysconf.getRGBMapPath(project_name,'rgb_map_final.tif') resultantMapName = [sysconf.getTeaBudIndetifiedMapPath(project_name,'rgb_processed.png')] for index in range(len(resultantMapName)): # print() # print('gdalinfo ' + rawMapPath + ' > ' + resultantMapName[index] + '_gdaldata.txt') # print(resultantMapName[index] + '_gdaldata.txt') # print() sysconf.writeLogRecord('Source map is ' + rawMapPath,log_file) sysconf.writeLogRecord('Resultant map is ' + resultantMapName[index],log_file) sysconf.writeLogRecord('Geo data retrieval command : gdalinfo ' + rawMapPath + ' > ' + resultantMapName[index] + '_gdaldata.txt',log_file) os.system('gdalinfo ' + rawMapPath + ' > ' + resultantMapName[index] + '_gdaldata.txt') content = open(resultantMapName[index] + '_gdaldata.txt','r').read() content = content.split('Corner Coordinates:')[1].split('Band 1') content = content[0].split('\n') upperLeft = content[1].split('(')[1].split(')')[0].split(' ')[1].split(',') lowerRight = content[4].split('(')[1].split(')')[0].split(' ')[1].split(',') upperLeftX = upperLeft[0] upperLeftY = upperLeft[1].split(' ')[1] lowerRightX = lowerRight[0] lowerRightY = lowerRight[1].split(' ')[1] # print(upperLeftX) # print(upperLeftY) # print(lowerRightX) # print(lowerRightY) sysconf.writeLogRecord('GDAL translation command : gdal_translate -a_ullr ' + upperLeftX + ' ' + upperLeftY + ' ' + lowerRightX + ' '+ lowerRightY +' -co TILED=yes -co BIGTIFF=IF_SAFER -co COMPRESS=DEFLATE -co PREDICTOR=2 -co BLOCKXSIZE=512 -co BLOCKYSIZE=512 -co NUM_THREADS=8 -a_srs "+units=m +no_defs=True +datum=WGS84 +proj=utm +zone=17 " --config GDAL_CACHEMAX 31.6% ' + resultantMapName[index] + ' '+ resultantMapName[index].split('.')[0] +'.tif',log_file) if (mode == 'ndvi'): sysconf.writeLogRecord('##################################################################',log_file) os.system('gdal_translate -a_ullr ' + upperLeftX + ' ' + upperLeftY + ' ' + lowerRightX + ' '+ lowerRightY +' -co TILED=yes -co BIGTIFF=IF_SAFER -co COMPRESS=DEFLATE -co PREDICTOR=2 -co BLOCKXSIZE=512 -co BLOCKYSIZE=512 -co NUM_THREADS=8 -a_srs "+units=m +no_defs=True +datum=WGS84 +proj=utm +zone=17 " --config GDAL_CACHEMAX 31.6% ' + resultantMapName[index] + ' '+ resultantMapName[index].split('.')[0] +'.tif')
def Create_Orthomap(project_name,mode,log_file): ODM_EXEC_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),'ODMStarter.sh') sysconf.writeLogRecord('ODM execution shell script path : ' + ODM_EXEC_PATH,log_file) PROJECT_PATH = ''#sysconf.getRGBRawImagesPath(project_name) mapPath = '' if((mode == 'NIR') or (mode == 'nir')): PROJECT_PATH = (sysconf.getNIRRawImagesPath(project_name)).split('/' + project_name) mapPath = sysconf.getNIRRawMapPath(project_name) elif((mode == 'RGB') or (mode == 'rgb')): PROJECT_PATH = (sysconf.getRGBRawImagesPath(project_name)).split('/' + project_name) mapPath = sysconf.getRGBRawMapPath(project_name) sysconf.writeLogRecord('Project path : ' + str(PROJECT_PATH),log_file) sysconf.writeLogRecord('Map path : ' + mapPath,log_file) sysconf.writeLogRecord('ODM execution command : ' + 'gnome-terminal -x ' + ODM_EXEC_PATH + ' ' + project_name + ' ' + PROJECT_PATH[0] + ' ' + sysconf.getODMExecutablePath(),log_file) # print('PROJECT_PATH : ' + str(PROJECT_PATH)) # print('mapPath : ' + mapPath) os.system('gnome-terminal -x ' + ODM_EXEC_PATH + ' ' + project_name + ' ' + PROJECT_PATH[0] + ' ' + sysconf.getODMExecutablePath()) if(pathlib.Path(mapPath).exists() == False): while(pathlib.Path(mapPath).exists() == False): time.sleep(1)
def geotagging_images(project_name,log_file): RGB_PATH = sysconf.getRGBRawImagesPath(project_name) NIR_PATH = sysconf.getNIRRawImagesPath(project_name) RGB_IMG_GEODATA_LOG_PATH = sysconf.getRGBGeoDataLogPath(project_name,'rgb_img_log.txt') NIR_IMG_GEODATA_LOG_PATH = sysconf.getNIRGeoDataLogPath(project_name,'nir_img_log.txt') sysconf.writeLogRecord('RGB images path : ' + RGB_PATH,log_file) sysconf.writeLogRecord('RGB images geo data log path : ' + RGB_IMG_GEODATA_LOG_PATH,log_file) sysconf.writeLogRecord('NIR images path : ' + NIR_PATH,log_file) sysconf.writeLogRecord('NIR images geo data log path : ' + NIR_IMG_GEODATA_LOG_PATH,log_file) # os.system("exiftool -p '$filename' -q -f " + NIR_PATH + ">" + NIR_IMG_GEODATA_LOG_PATH) # os.system("exiftool -p '$filename' -q -f " + RGB_PATH + ">" + RGB_IMG_GEODATA_LOG_PATH) sysconf.writeLogRecord('exif command for RGB images : ' + "exiftool -p '$filename' -q -f " + RGB_PATH + ">" + RGB_IMG_GEODATA_LOG_PATH,log_file) sysconf.writeLogRecord('exif command for NIR images : ' + "exiftool -p '$filename' -q -f " + NIR_PATH + ">" + NIR_IMG_GEODATA_LOG_PATH,log_file) ###sort RGB_names.txt file rgb_band = writeImageNamesToTextFile(RGB_IMG_GEODATA_LOG_PATH) rgb_band = sortTheImagesInTextFiles(rgb_band,RGB_IMG_GEODATA_LOG_PATH) ####sort NIR_names.txt file nir_band = writeImageNamesToTextFile(NIR_IMG_GEODATA_LOG_PATH) nir_band = sortTheImagesInTextFiles(nir_band,NIR_IMG_GEODATA_LOG_PATH) ###geotagging for x in range(len(rgb_band)): geotagImage(RGB_PATH,rgb_band[x],NIR_PATH,nir_band[x],"latitude",log_file) geotagImage(RGB_PATH,rgb_band[x],NIR_PATH,nir_band[x],"longtitude",log_file)
def createTiles(project_name,mode,log_file): map_path = [] destination_map_path = [] map_name = [] tile_path = [] if mode == 'rgb': destination_map_path = [sysconf.getRGBMapPath(project_name,'rgb_map_final.tif')] map_path = [sysconf.getRGBRawMapPath(project_name)] map_name = ['rgb_map_final'] elif mode == 'nir': destination_map_path = [sysconf.getNIRMapPath(project_name,'nir_map_final.tif')] map_path = [sysconf.getNIRRawMapPath(project_name)] map_name = ['nir_map_final'] elif mode == 'ndvi': destination_map_path = [sysconf.getNDVIMapPath(project_name,'ndvi_map_final_red_layer.tif'),sysconf.getNDVIMapPath(project_name,'ndvi_map_final_green_layer.tif'),sysconf.getNDVIMapPath(project_name,'ndvi_map_final_blue_layer.tif')] map_path = [sysconf.getNDVIMapPath(project_name,'ndvi_map_final_red_layer.tif'),sysconf.getNDVIMapPath(project_name,'ndvi_map_final_green_layer.tif'),sysconf.getNDVIMapPath(project_name,'ndvi_map_final_blue_layer.tif')] map_name = ['ndvi_map_final_red_layer','ndvi_map_final_green_layer','ndvi_map_final_blue_layer'] elif mode == 'cluster': destination_map_path = [sysconf.getTeaBudIndetifiedMapPath(project_name,'rgb_processed.tif')] map_path = [sysconf.getTeaBudIndetifiedMapPath(project_name,'rgb_processed.tif')] map_name = ['rgb_processed'] for index in range(len(destination_map_path)): sysconf.writeLogRecord('Map path : ' + map_path[index],log_file) sysconf.writeLogRecord('Tile path : ' + destination_map_path[index],log_file) # os.system('cp ' + map_path + ' ' + destination_map_path) sysconf.writeLogRecord('gdal2tiles log file in ' + sysconf.createLogFile(project_name + '_' + mode + '_tiling.txt'),log_file) os.system('gdal2tiles.py -z 20-22 ' + destination_map_path[index] + ' > ' + sysconf.createLogFile(project_name + '_' + mode + '_tiling.txt')) sysconf.writeLogRecord('executing tile folder transfer command : ' + 'cp -r ' + map_name[index] + ' ' + os.path.join('/'.join(destination_map_path[index].split('/')[:-1])),log_file) sysconf.writeLogRecord('executing original tile folder removal command : ' + 'rm -r ' + map_name[index],log_file) os.system('cp -r ' + map_name[index]+ ' ' + os.path.join('/'.join(destination_map_path[index].split('/')[:-1]))) os.system('rm -r ' + map_name[index]) sysconf.writeLogRecord('Tile path : ' + destination_map_path[index],log_file) if mode == 'ndvi': sysconf.writeLogRecord('##################################################################',log_file) tile_path.append(os.path.join('http://localhost',destination_map_path[index].split('htdocs/')[1].split('.tif')[0])) return {'mode':mode,'path':str(tile_path)}
def Image_Overlap(latter_image,upper_image,map_destination_path,log_file): MIN_MATCH_COUNT = 4 ## prepare data img1 = cv2.imread(latter_image) img2 = cv2.imread(upper_image) gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) ## Create SIFT object sift = cv2.xfeatures2d.SIFT_create() ## Create flann matcher matcher = cv2.FlannBasedMatcher(dict(algorithm = 1, trees = 5), {}) ## Detect keypoints and compute keypointer descriptors kpts1, descs1 = sift.detectAndCompute(gray1,None) kpts2, descs2 = sift.detectAndCompute(gray2,None) ## knnMatch to get Top2 matches = matcher.knnMatch(descs1, descs2, 2) # Sort by their distance. matches = sorted(matches, key = lambda x:x[0].distance) ## Ratio test, to get good matches. good = [m1 for (m1, m2) in matches if m1.distance < 0.7 * m2.distance] canvas = img2.copy() ## find homography matrix if len(good)>MIN_MATCH_COUNT: ## (queryIndex for the small object, trainIndex for the scene ) src_pts = np.float32([ kpts1[m.queryIdx].pt for m in good ]).reshape(-1,1,2) dst_pts = np.float32([ kpts2[m.trainIdx].pt for m in good ]).reshape(-1,1,2) ## find homography matrix in cv2.RANSAC using good match points M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) h,w = img1.shape[:2] pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) cv2.polylines(canvas,[np.int32(dst)],True,(0,255,0),3, cv2.LINE_AA) else: print( "Not enough matches are found - {}/{}".format(len(good),MIN_MATCH_COUNT)) ## drawMatches matched = cv2.drawMatches(img1,kpts1,canvas,kpts2,good,None)#,**draw_params) ## Crop the matched region from Orthomap h,w = img1.shape[:2] pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) perspectiveM = cv2.getPerspectiveTransform(np.float32(dst),pts) found = cv2.warpPerspective(img2,perspectiveM,(w,h)) ## save and display cv2.imwrite("matched.png", matched) cv2.imwrite(map_destination_path, found) sysconf.writeLogRecord('Pre processed map image destination path : ' + map_destination_path,log_file) #Remove the black area resultant_file_name = map_destination_path.split('/').pop() img = cv2.imread(map_destination_path) grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) _, thresholded = cv2.threshold(grayscale, 0, 255, cv2.THRESH_BINARY) bbox = cv2.boundingRect(thresholded) x, y, w, h = bbox foreground = img[y:y+h, x:x+w] finalPath = map_destination_path.split('/')[:-1] finalPath = '/'.join(finalPath) cv2.imwrite(os.path.join(finalPath,resultant_file_name.split('.')[0] + '_final.png'),foreground) sysconf.writeLogRecord('Final produced map image : ' + os.path.join(finalPath,resultant_file_name.split('.')[0] + '_final.png'),log_file) return resultant_file_name.split('.')[0] + '_final.png'
def createNDVIMap(nir_path, rgb_path, ndvi_path, log_file): # Set debug mode to plot # pcv.params.debug = "plot" print("RGB Map Path : " + rgb_path) print("NIR Map Path : " + nir_path) # Read the RGB image #rgb, _, _ = pcv.readimage(rgb_path, mode="RGB") rgb = cv2.imread(rgb_path, 1) rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2BGRA) print('[' + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + '] ' + 'Converting the NIR and RGB maps to RGBA mode', end="") sysconf.writeLogRecord('Reading and converting RGB map to RGBA', log_file) sysconf.writeLogRecord( 'RGB image resolution is ' + str(rgb.shape[0]) + ' x ' + str(rgb.shape[1]), log_file) # rgb, _, _ = pcv.readimage(nir_path, mode="GRAY") # Read the NIR image # nir, _, _ = pcv.readimage(nir_path, mode="GRAY") # nir, _, _ = pcv.readimage(nir_path, mode="RGB") nir = cv2.imread(nir_path, 1) nir = cv2.cvtColor(nir, cv2.COLOR_BGR2BGRA) sysconf.writeLogRecord('Reading and converting non pure NIR map to RGBA', log_file) sysconf.writeLogRecord( 'NIR image resolution is ' + str(nir.shape[0]) + ' x ' + str(nir.shape[1]), log_file) # print("nir : " + str(nir.shape)) # print("rgb : " + str(rgb.shape)) print('.....done') width = rgb.shape[1] height = rgb.shape[0] flag = '' print('[' + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + '] ' + "Resizing the NIR and RGB map arrays", end="") sysconf.writeLogRecord('Resizing RGB and NIR map numpy arrays started', log_file) if (nir.shape[0] < rgb.shape[0]): # print('rgb height : ' + str(rgb.shape[0])) # print('nir height : ' + str(nir.shape[0])) height = nir.shape[0] flag = 'nir' else: # print('nir height : ' + str(nir.shape[0])) # print('rgb height : ' + str(rgb.shape[0])) height = rgb.shape[0] flag = 'rgb' if (nir.shape[1] < rgb.shape[1]): # print('rgb height : ' + str(rgb.shape[1])) # print('nir height : ' + str(nir.shape[1])) width = nir.shape[1] flag = 'nir' else: # print('nir height : ' + str(nir.shape[1])) # print('rgb height : ' + str(rgb.shape[1])) width = rgb.shape[1] flag = 'rgb' sysconf.writeLogRecord( 'New RGB and NIR image resolution is ' + str(height) + ' x ' + str(width), log_file) # sysconf.writeLogRecord('New NIR image resolution is ' + nir.shape[0] + ' x ' + nir.shape[1]) # if(flag == 'rgb'): nir = cv2.resize(nir, (width, height)) rgb = cv2.resize(rgb, (width, height)) print('.....done') print('New RGB and NIR image resolution is ' + str(height) + ' x ' + str(width)) sysconf.writeLogRecord('Resizing RGB and NIR map numpy arrays completed', log_file) # Calculate NDVI ndvi = np.zeros((height, width, 1), dtype=np.float64) print('[' + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + '] ' + "Constructing NDVI map layers", end="") sysconf.writeLogRecord( 'Constructing NDVI and NDVI map layer arrays..0 of 3 completed', log_file) ndvi_pixel_np_red_layer_array = np.zeros((height, width, 4), dtype=np.uint8) sysconf.writeLogRecord( 'Constructing NDVI and NDVI map layer arrays 1 of 3 completed', log_file) ndvi_pixel_np_green_layer_array = np.zeros((height, width, 4), dtype=np.uint8) sysconf.writeLogRecord( 'Constructing NDVI and NDVI map layer arrays..2 of 3 completed', log_file) ndvi_pixel_np_blue_layer_array = np.zeros((height, width, 4), dtype=np.uint8) sysconf.writeLogRecord( 'Constructing NDVI and NDVI map layer arrays..3 of 3 completed', log_file) for i in range(height): for j in range(width): ndvi[i][j] = (math.floor( ((nirIntensity - float(rgb[i][j][2])) / (nirIntensity + float(rgb[i][j][2]))) * 100)) / 100 value = ndvi_map[i][j] if ((value <= 1.00) & (value >= 0.20)): ndvi_pixel_np_green_layer_array[i][j] = (0, 255, 0, 255) ndvi_pixel_np_red_layer_array[i][j] = (0, 255, 0, 0) ndvi_pixel_np_blue_layer_array[i][j] = (0, 255, 0, 0) elif ((value > 0) & (value < 0.20)): ndvi_pixel_np_green_layer_array[i][j] = (0, 0, 255, 0) ndvi_pixel_np_red_layer_array[i][j] = (0, 0, 255, 255) ndvi_pixel_np_blue_layer_array[i][j] = (0, 0, 255, 0) elif ((value <= 0) & (value >= -0.5)): ndvi_pixel_np_green_layer_array[i][j] = (0, 255, 0, 255) ndvi_pixel_np_red_layer_array[i][j] = (0, 255, 0, 0) ndvi_pixel_np_blue_layer_array[i][j] = (0, 255, 0, 0) elif ((value <= -0.5) & (value >= -0.7)): ndvi_pixel_np_green_layer_array[i][j] = (255, 0, 0, 0) ndvi_pixel_np_red_layer_array[i][j] = (255, 0, 0, 0) ndvi_pixel_np_blue_layer_array[i][j] = (255, 0, 0, 255) elif ((value <= -0.7) & (value >= -1.00)): ndvi_pixel_np_green_layer_array[i][j] = (0, 0, 255, 0) ndvi_pixel_np_red_layer_array[i][j] = (0, 0, 255, 255) ndvi_pixel_np_blue_layer_array[i][j] = (0, 0, 255, 0) # Make a pseudocolored NDVI image # ndvi_pseudo = pcv.visualize.pseudocolor(gray_img=ndvi, min_value=-1, max_value=1, cmap="viridis") # cv2.imwrite(ndvi_path + '.png',ndvi_pixel_np_array) print('.....done') print('[' + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + '] ' + 'Writing NDVI map layers', end="") sysconf.writeLogRecord('Writing NDVI map layers', log_file) cv2.imwrite(ndvi_path + '_red_layer.png', ndvi_pixel_np_red_layer_array) cv2.imwrite(ndvi_path + '_green_layer.png', ndvi_pixel_np_green_layer_array) cv2.imwrite(ndvi_path + '_blue_layer.png', ndvi_pixel_np_blue_layer_array) print('.....done') # return ndvi_path,ndvi return ndvi