最近在做关于遥感影像变化检测的作业,影像太大了,首先需要对遥感影像进行裁剪。
import cv2 as cv
src=cv.imread('before.tif')
print(src.shape)
cropImg = src[600:1100,600:1100]
cv.imwrite("./be.tif",cropImg)
print(cropImg.shape)
输出:
(15354, 32507, 3)
(500, 500, 3)
切片给出的坐标为需要裁剪的图片在原图片上的坐标,顺序为[y0:y1, x0:x1],其中原图的左上角是坐标原点。
参考博客:Python实现图片裁剪的两种方式——Pillow和OpenCV
补一个读文件函数,做笔记用
import cv2
#读取文件函数,返回变化前,变化后以及变化结果
def get_files(filename):
before=cv2.imread('./data/before.tif',-1)
after=cv2.imread('./data/after.tif',-1)
change_label=cv2.imread('./data/change_label.tif ',-1)
# print(before.dtype) 原格式是unit8
#转换成数组以及float1