def process_images(new_string_json): TileMaxLat = new_string_json['TileMaxLat'] TileMinLat = new_string_json['TileMinLat'] TileMaxLong = new_string_json['TileMaxLong'] TileMinLong = new_string_json['TileMinLong'] FieldMaxLat = new_string_json['FieldMaxLat'] FieldMinLat = new_string_json['FieldMinLat'] FieldMaxLong = new_string_json['FieldMaxLong'] FieldMinLong = new_string_json['FieldMinLong'] UID = new_string_json['UID'] sizeB4 = [10980 10980] LatsPerPixel = (TileMaxLat - TileMinLat)/sizeB4(1) LongsPerPixel = (TileMaxLong - TileMinLong)/sizeB4(1) PixelsPerLat = abs(sizeB4(1)/(TileMaxLat - TileMinLat)) PixelsPerLong = abs(sizeB4(2)/(TileMaxLong - TileMinLong)) StartPixelLat = PixelsPerLat*(1-abs(FieldMaxLat-TileMinLat)) StartPixelLat = round(abs(StartPixelLat)) EndPixelLat = PixelsPerLat*(1-abs(TileMinLat- FieldMinLat)) EndPixelLat = round(abs(EndPixelLat)) if StartPixelLat > EndPixelLat: temp = EndPixelLat EndPixelLat = StartPixelLat StartPixelLat = temp if StartPixelLat > 300: StartPixelLat = StartPixelLat - 300 else: StartPixelLat = 1 if EndPixelLat EndPixelLong: temp = EndPixelLong EndPixelLong = StartPixelLong StartPixelLong = temp if StartPixelLong > 300: StartPixelLong = StartPixelLong - 300 else: StartPixelLong =1 if EndPixelLong < sizeB4(1) - 300: EndPixelLong = EndPixelLong+300 else: EndPixelLong = sizeB4(1) LatLength = EndPixelLat - StartPixelLat LongLength = EndPixelLong - StartPixelLong LatNDVI = LatLength LongNDVI = LongLength if StartPixelLat > sizeB4(1) | EndPixelLat > sizeB4(1) | StartPixelLong > sizeB4(1) | EndPixelLong > sizeB4(1): result_status2 = 'failed due to unbound start end' result_status = strcat(UID,"failed0") break return try: B4 = cv2.imread('TileB4.jp2',0) B5 = cv2.imread('TileB5.jp2',0) B8 = cv2.imread('TileB8.jp2',0) TCI = imread('TileTCI.jp2') B8A = cv2.imread('TileB8A.jp2',0) B11 = cv2.imread('TileB11.jp2',0) except: result_status2 = 'failed due to image decode Error' result_status = UID+"failed0" return clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong] clip_tci = clip B4 = B4(clip(1):clip(2),clip(3):clip(4)) B8 = B8(clip(1):clip(2),clip(3):clip(4)) TCI = TCI(clip(1):clip(2),clip(3):clip(4),:) %find field coordinates for smaller resolution images sizeB4 = [5490 5490] rr = 11 LatsPerPixel = (TileMaxLat - TileMinLat)/sizeB4(1) LongsPerPixel = (TileMaxLong - TileMinLong)/sizeB4(1) PixelsPerLat = abs(sizeB4(1)/(TileMaxLat - TileMinLat)) PixelsPerLong = abs(sizeB4(2)/(TileMaxLong - TileMinLong)) StartPixelLat = PixelsPerLat*(1-abs(FieldMaxLat-TileMinLat)) StartPixelLat = round(abs(StartPixelLat)) EndPixelLat = PixelsPerLat*(1-abs(TileMinLat- FieldMinLat)) EndPixelLat = round(abs(EndPixelLat)) if StartPixelLat > EndPixelLat: temp = EndPixelLat EndPixelLat = StartPixelLat StartPixelLat = temp if StartPixelLat > 150: StartPixelLat = StartPixelLat - 150 else: StartPixelLat = 1 if EndPixelLat EndPixelLong: temp = EndPixelLong EndPixelLong = StartPixelLong StartPixelLong = temp if StartPixelLong > 150: StartPixelLong = StartPixelLong - 150 else: StartPixelLong = 1 if EndPixelLong < sizeB4(1) - 150: EndPixelLong = EndPixelLong+150 else: EndPixelLong = sizeB4(1) LatLength = EndPixelLat - StartPixelLat LongLength = EndPixelLong - StartPixelLong clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong] B8A = B8A(clip(1):clip(2),clip(3):clip(4),:) B11 = B11(clip(1):clip(2),clip(3):clip(4),:) B5 = B5(clip(1):clip(2),clip(3):clip(4),:) B8NDRE = B8 B8NDRE = cv2.imresize(B8NDRE,[abs(clip(2)-clip(1)+1) abs(clip(4)-clip(3)+1)]) NDVI = (B8 - B4)./(B8 + B4) NDRE = (B8NDRE-B5)./(B8NDRE+B5) NDWI = (B11 - B8A)./(B8A + B11) sN = size(NDVI) pixelcount = sN(1)*sN(2) negcount = 0 for i = 1:sN(1): for j = 1:sN(2): if NDVI(i,j) < 0: negcount=negcount+1 if negcount>pixelcount*0.2: result_status = UID+"failed1" result_status2 = 'Too much cloud in image' return SizeNewB4 = size(B4) IsNaNMat = isnan(NDVI) IsNaNCount =sum(sum(IsNaNMat)) if IsNaNCount > SizeNewB4(1)*SizeNewB4(2)*0.5: result_status = UID+"failed1" result_status2 = 'Too much black in image' return octave.proceess_tci(UID) startx=25 endx = 900-25 starty=181 endy=1200-188 TCIFilename = UID+"_ETCI.png" imwrite(uint8(img.*255),TCIFilename) TCIFilenameL = UID+"_ETCI_large.png" imwrite(uint8(img.*255),TCIFilenameL) TCIFilename2 = UID+"_TCI.png" imwrite(TCI,TCIFilename2) TCIFilename2L = UID+"_TCI_large.png" imwrite(TCI,TCIFilename2L); NDVI_new = cv2.bitwise_not(NDVI) colormap(jet) imagesc(NDVI_new, [0 0.5]); axis square tight axis off ndviname = strcat(UID,"_ndvi") saveas(1,ndviname,'png') %read NDVI image, crop, resize and mask on MNDVI ndviname=strcat(ndviname,".png"); aa = imread(ndviname); size_aa = size(aa) sizeNDVI = size(NDVI) aa_new = aa(startx:endx, starty:endy,:); aa_new = imresize(aa_new,[sizeNDVI(1) sizeNDVI(2)]); imwrite(aa_new,ndviname); ndvinameL = strcat(UID,"_ndvi_large.png"); imwrite(aa_new,ndvinameL); aa_new = imread(ndviname); aa_new = single(aa_new); Rnew = zeros(sizeNDVI(1),sizeNDVI(2),3); for i = 1:sizeNDVI(1) for j = 1:sizeNDVI(2) if NDVI(i,j) < 0.4 && NDVI(i,j)>0.05 Rnew(i,j,:) = aa_new(i,j,:); else Rnew(i,j,:) = img(i,j,:); end end end image_name = strcat(UID,"_mndvi.png") imwrite(Rnew,image_name); image_nameL = strcat(UID,"_mndvi_large.png") imwrite(Rnew,image_nameL); %create colormap NDRE image NDRE_new = (1/(max(max(NDRE))))*NDRE; NDRE_new = imcomplement(NDRE); colormap(jet) imagesc(NDRE_new, [0 1]); axis square tight axis off NDRE_name = strcat(UID,"_ndre") saveas(1,NDRE_name,'png') %read NDRE image, crop, resize and mask on MNDRE i_name = strcat(UID,"_ndre.png"); aa = imread(i_name); aa_new = aa(startx:endx, starty:endy,:); sizeNDRE = size(NDRE) aa_new = imresize(aa_new,[sizeNDRE(1) sizeNDRE(2)]); imwrite(aa_new,i_name); i_nameL = strcat(UID,"_ndre_large.png"); imwrite(aa_new,i_nameL); aa_new = single(aa_new); size_aa = size(aa_new) AlphaMatndre = zeros(sizeNDRE(1),sizeNDRE(2)); Rnew = zeros(sizeNDRE(1),sizeNDRE(2),3); sizeaanew=size(aa_new) for i = 1:sizeNDRE(1) for j = 1:sizeNDRE(2) if NDRE(i,j) < 0.4 && NDRE(i,j)>0.05 Rnew(i,j,:) = aa_new(i,j,:); else AlphaMatndre(i,j)=1; end end end sizeRnewndre = size(Rnew) i_namendre = strcat(UID,"_mndre.png"); imwrite(Rnew,i_namendre,'Alpha',AlphaMatndre); i_namendreL = strcat(UID,"_mndre_large.png"); imwrite(Rnew,i_namendreL,'Alpha',AlphaMatndre); %create colormap NDWI image NDWI_new = (1/(max(max(NDWI))))*NDWI; NDWI_new = imcomplement(NDWI); colormap(jet) imagesc(NDWI_new, [0 1]); axis square tight axis off imagename =strcat(UID,"_ndwi"); saveas(1,imagename,'png') %read NDRE image, crop, resize and mask on MNDRE i_name =strcat(UID,'_ndwi.png'); aa = imread(i_name); aa_new = aa(startx:endx, starty:endy,:); aa_new = imresize(aa_new,[sizeNDRE(1) sizeNDRE(2)]); imwrite(aa_new,i_name); i_nameL = strcat(UID,'_ndwi_large.png'); imwrite(aa_new,i_nameL); aaaa = 'came here' aa_new = single(aa_new); sizeN = size(NDWI) Rnew = zeros(sizeNDRE(1),sizeNDRE(2),3); AlphaMat = zeros(sizeNDRE(1),sizeNDRE(2)); for i = 1:sizeNDRE(1) for j = 1:sizeNDRE(2) if NDWI(i,j) < 0.4 && NDWI(i,j)>0.05 Rnew(i,j,:) = aa_new(i,j,:); else AlphaMat(i,j)=1; end end end i_mndwi = strcat(UID,"_mndwi.png"); imwrite(Rnew,i_mndwi,'Alpha',AlphaMat); i_mndwiL = strcat(UID,"_mndwi_large.png"); imwrite(Rnew,i_mndwiL,'Alpha',AlphaMat); result_status = strcat(UID,"successful")