import cv2
import os
from osgeo import gdal
import numpy as np
def tif_jpg(rasterfile):
in_ds = gdal.Open(rasterfile)
xsize = in_ds.RasterXSize
ysize = in_ds.RasterYSize
block_data = in_ds.ReadAsArray(0, 0, xsize, ysize).astype(np.float32)
B = block_data[:, :]
data2 = cv2.merge([B]).astype(np.int16)
return data2
将envi输出的tif文件翻转并保存为jpg图像
path = './tif_folder'
nameList = os.listdir(path)
for i in nameList:
new_path = os.path.join(path,i)
img = tif_jpg(new_path)
img1 = cv2.flip(img,1)
new_name = new_path.split('\\')[-1].split('.')[0]+'.jpg'
cv2.imwrite(new_name,img1)
print(new_name,'----done')
0315VH.jpg
0327VH.jpg
利用滑动窗口裁剪大尺寸图片
def create_folder(path):
if not os.path.exists(path):
os.mkdir(path)
def slid_win(imgpp, out_folder, win = 256, step_num = 5):
create_folder(out_folder)
height, width, _ = img.shape
step = win//step_num
y, x = 0,0
count = 0
while y+win <= height:
while x+win <= width:
x += step
count+=1
new_img = imgpp[y:y+win,x:x+win]
new_name = str(count).zfill(5)+'.jpg'
new_path = os.path.join(out_folder,new_name)
cv2.imwrite(new_path,new_img)
y += step
print('left:',height-y,width-x,'total:',count)
path = './jpg_folder'
nameList = os.listdir(path)
for i in range(len(nameList)):
img_path = os.path.join(path,nameList[i])
img = cv2.imread(img_path)
new_folder = 'img'+str(i)
slid_win(img, win = 256, step_num = 5, out_folder=new_folder)
left: 253 236 total: 499
left: 253 236 total: 499
|