您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Python3+gdal如何讀取tiff格式數據,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
1、遇到的問題:numpy版本
im_data = dataset.ReadAsArray(0,0,im_width,im_height)#獲取數據 這句報錯
升級numpy:pip install -U numpy 但是提示已經是最新版本
解決:卸載numpy 重新安裝
2.直接從壓縮包中讀取tiff圖像
參考:http://gdal.org/gdal_virtual_file_systems.html#gdal_virtual_file_systems_vsizip
當前情況是2層壓縮: /'/vsitar/C:/Users/summer/Desktop/a_PAN1.tiff'
3.讀tiff
def readTif(fileName): merge_img = 0 driver = gdal.GetDriverByName('GTiff') driver.Register() dataset = gdal.Open(fileName) if dataset == None: print(fileName+ "掩膜失敗,文件無法打開") return im_width = dataset.RasterXSize #柵格矩陣的列數 print('im_width:', im_width) im_height = dataset.RasterYSize #柵格矩陣的行數 print('im_height:', im_height) im_bands = dataset.RasterCount #波段數 im_geotrans = dataset.GetGeoTransform()#獲取仿射矩陣信息 im_proj = dataset.GetProjection()#獲取投影信息 if im_bands == 1: band = dataset.GetRasterBand(1) im_data = dataset.ReadAsArray(0,0,im_width,im_height) #獲取數據 cdata = im_data.astype(np.uint8) merge_img = cv2.merge([cdata,cdata,cdata]) cv2.imwrite('C:/Users/summer/Desktop/a.jpg', merge_img) # elif im_bands == 4: # # im_data = dataset.ReadAsArray(0,0,im_width,im_height)#獲取數據 # # im_blueBand = im_data[0,0:im_width,0:im_height] #獲取藍波段 # # im_greenBand = im_data[1,0:im_width,0:im_height] #獲取綠波段 # # im_redBand = im_data[2,0:im_width,0:im_height] #獲取紅波段 # # # im_nirBand = im_data[3,0:im_width,0:im_height] #獲取近紅外波段 # # merge_img=cv2.merge([im_redBand,im_greenBand,im_blueBand]) # # zeros = np.zeros([im_height,im_width],dtype = "uint8") # # data1 = im_redBand.ReadAsArray # band1=dataset.GetRasterBand(1) # band2=dataset.GetRasterBand(2) # band3=dataset.GetRasterBand(3) # band4=dataset.GetRasterBand(4) data1=band1.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #r #獲取數據 data2=band2.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #g #獲取數據 data3=band3.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #b #獲取數據 data4=band4.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #R #獲取數據 # print(data1[1][45]) # output1= cv2.convertScaleAbs(data1, alpha=(255.0/65535.0)) # print(output1[1][45]) # output2= cv2.convertScaleAbs(data2, alpha=(255.0/65535.0)) # output3= cv2.convertScaleAbs(data3, alpha=(255.0/65535.0)) merge_img1 = cv2.merge([output3,output2,output1]) #B G R cv2.imwrite('C:/Users/summer/Desktop/merge_img1.jpg', merge_img1)
4.圖像裁剪:
import cv2 import numpy as np import os tiff_file = './try_img/2.tiff' save_folder = './try_img_re/' if not os.path.exists(save_folder): os.makedirs(save_folder) tif_img = cv2.imread(tiff_file) width, height, channel = tif_img.shape # print height, width, channel : 6908 7300 3 threshold = 1000 overlap = 100 step = threshold - overlap x_num = width/step + 1 y_num = height/step + 1 print x_num, y_num N = 0 yj = 0 for xi in range(x_num): for yj in range(y_num): # print xi if yj <= y_num: print yj x = step*xi y = step*yj wi = min(width,x+threshold) hi = min(height,y+threshold) # print wi , hi if wi-x < 1000 and hi-y < 1000: im_block = tif_img[wi-1000:wi, hi-1000:hi] elif wi-x > 1000 and hi-y < 1000: im_block = tif_img[x:wi, hi-1000:hi] elif wi-x < 1000 and hi-y > 1000: im_block = tif_img[wi-1000:wi, y:hi] else: im_block = tif_img[x:wi,y:hi] cv2.imwrite(save_folder + 'try' + str(N) + '.jpg', im_block) N += 1
關于“Python3+gdal如何讀取tiff格式數據”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。