import cv2 import numpy as np import traceback import os def gavl_img_diff(uid,polygon_id, dates_arr): # import required libraries # load the input images # file_name = uid + '/' + polygon_id + '/' + dates_arr[(len(dates_arr)-1)]+'_TCI.png' # print(file_name) # img1 = cv2.imread(file_name) # img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) uprooting_dates = [] for temp_date_int in range((len(dates_arr)-1)): try: #img1 = cv2.imread('TCXcp5VIsfhHZrh0nm2VsgBtcGy2/1611807451303/2023-01-15_TCI.png') #img2 = cv2.imread('TCXcp5VIsfhHZrh0nm2VsgBtcGy2/1611807451303/2023-01-10_TCI.png') file_name = uid + '/' + polygon_id + '/' + dates_arr[temp_date_int]+'_TCI.png' #print(file_name) img1 = cv2.imread(file_name) img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) file_name = uid + '/' + polygon_id + '/' + dates_arr[(temp_date_int+1)]+'_TCI.png' #print(file_name) img2 = cv2.imread(file_name) orig_img2 = img2 # convert the images to grayscale img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) error, diff = mse(img1, img2) if error > 100: uprooting_dates.append(dates_arr[temp_date_int]) isdir = os.path.isdir('Uprooting') if isdir != True: os.mkdir('Uprooting') isdir = os.path.isdir(('Uprooting/' + polygon_id)) if isdir != True: os.mkdir(('Uprooting/' + polygon_id)) cv2.imwrite(('Uprooting/' + polygon_id+'/'+dates_arr[(temp_date_int+1)]+'.png'),orig_img2) print("Image matching Error between the two images:",error) except: print(traceback.format_exc()) #cv2.imwrite('TCXcp5VIsfhHZrh0nm2VsgBtcGy2/diff.png', diff) print(uprooting_dates) return uprooting_dates # define the function to compute MSE between two images def mse(img1, img2): h, w = img1.shape diff = cv2.subtract(img1, img2) err = np.sum(diff**2) mse = err/(float(h*w)) return mse, diff