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

  1. def readTif(fileName):
  2. merge_img = 0
  3. driver = gdal.GetDriverByName(‘GTiff’)
  4. driver.Register()
  5. dataset = gdal.Open(fileName)
  6. if dataset == None:
  7. print(fileName+ “掩膜失敗,文件無法打開”)
  8. return
  9. im_width = dataset.RasterXSize #柵格矩陣的列數
  10. print(‘im_width:’, im_width)
  11. im_height = dataset.RasterYSize #柵格矩陣的行數
  12. print(‘im_height:’, im_height)
  13. im_bands = dataset.RasterCount #波段數
  14. im_geotrans = dataset.GetGeoTransform()#獲取仿射矩陣信息
  15. im_proj = dataset.GetProjection()#獲取投影信息
  16. if im_bands == 1:
  17. band = dataset.GetRasterBand(1)
  18. im_data = dataset.ReadAsArray(0,0,im_width,im_height) #獲取數據
  19. cdata = im_data.astype(np.uint8)
  20. merge_img = cv2.merge([cdata,cdata,cdata])
  21. cv2.imwrite(‘C:/Users/summer/Desktop/a.jpg’, merge_img)
  22. #
  23. elif im_bands == 4:
  24. # # im_data = dataset.ReadAsArray(0,0,im_width,im_height)#獲取數據
  25. # # im_blueBand = im_data[0,0:im_width,0:im_height] #獲取藍波段
  26. # # im_greenBand = im_data[1,0:im_width,0:im_height] #獲取綠波段
  27. # # im_redBand = im_data[2,0:im_width,0:im_height] #獲取紅波段
  28. # # # im_nirBand = im_data[3,0:im_width,0:im_height] #獲取近紅外波段
  29. # # merge_img=cv2.merge([im_redBand,im_greenBand,im_blueBand])
  30. # # zeros = np.zeros([im_height,im_width],dtype = “uint8”)
  31. # # data1 = im_redBand.ReadAsArray
  32. # band1=dataset.GetRasterBand(1)
  33. # band2=dataset.GetRasterBand(2)
  34. # band3=dataset.GetRasterBand(3)
  35. # band4=dataset.GetRasterBand(4)
  36. data1=band1.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #r #獲取數據
  37. data2=band2.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #g #獲取數據
  38. data3=band3.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #b #獲取數據
  39. data4=band4.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #R #獲取數據
  40. # print(data1[1][45])
  41. # output1= cv2.convertScaleAbs(data1, alpha=(255.0/65535.0))
  42. # print(output1[1][45])
  43. # output2= cv2.convertScaleAbs(data2, alpha=(255.0/65535.0))
  44. # output3= cv2.convertScaleAbs(data3, alpha=(255.0/65535.0))
  45. merge_img1 = cv2.merge([output3,output2,output1]) #B G R
  46. cv2.imwrite(‘C:/Users/summer/Desktop/merge_img1.jpg’, merge_img1)

4.圖像裁剪:

  1. import cv2
  2. import numpy as np
  3. import os
  4. tiff_file = './try_img/2.tiff'
  5. save_folder = './try_img_re/'
  6. if not os.path.exists(save_folder):
  7. os.makedirs(save_folder)
  8. tif_img = cv2.imread(tiff_file)
  9. width, height, channel = tif_img.shape
  10. # print height, width, channel : 6908 7300 3
  11. threshold = 1000
  12. overlap = 100
  13. step = threshold - overlap
  14. x_num = width/step + 1
  15. y_num = height/step + 1
  16. print x_num, y_num
  17. N = 0
  18. yj = 0
  19. for xi in range(x_num):
  20. for yj in range(y_num):
  21. # print xi
  22. if yj <= y_num:
  23. print yj
  24. x = step*xi
  25. y = step*yj
  26. wi = min(width,x+threshold)
  27. hi = min(height,y+threshold)
  28. # print wi , hi
  29. if wi-x < 1000 and hi-y < 1000:
  30. im_block = tif_img[wi-1000:wi, hi-1000:hi]
  31. elif wi-x > 1000 and hi-y < 1000:
  32. im_block = tif_img[x:wi, hi-1000:hi]
  33. elif wi-x < 1000 and hi-y > 1000:
  34. im_block = tif_img[wi-1000:wi, y:hi]
  35. else:
  36. im_block = tif_img[x:wi,y:hi]
  37. cv2.imwrite(save_folder + 'try' + str(N) + '.jpg', im_block)
  38. N += 1


https://blog.csdn.net/summermaoz/article/details/78346929 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章