wangcheng/threshold.py

75 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from osgeo import gdal
import cv2
import numpy as np
from osgeo import gdal
def main():
# 打开影像文件
image_00_00_path = "E:/vscode_code/fire_code/HS_H09_20230801_0000/HS_H09_20230801_0000.tif" # 替换为你的影像路径
dataset_00 = gdal.Open(image_00_00_path)
if dataset_00 is None:
print("HS_H09_20230801_0000.tif.")
return
image_00_10_path = "E:/vscode_code/fire_code/HS_H09_20230801_0010/HS_H09_20230801_0010.tif" # 替换为你的影像路径
dataset_10 = gdal.Open(image_00_10_path)
if dataset_10 is None:
print("HS_H09_20230801_0010.tif.")
return
# 获取00时00分 band 7 和 band 14 的 temperature 数据
band7_00_00_temperature = dataset_00.GetRasterBand(7).ReadAsArray()
band14_00_00_temperature = dataset_00.GetRasterBand(14).ReadAsArray()
# 获取00时10分 band 7 和 band 14 的 temperature 数据
band7_00_10_temperature = dataset_10.GetRasterBand(7).ReadAsArray()
band14_00_10_temperature = dataset_10.GetRasterBand(14).ReadAsArray()
#计算band7band14在00分到10分的差值
band7_temperature_difference = band7_00_10_temperature - band7_00_00_temperature
band14_temperature_difference = band14_00_10_temperature - band14_00_00_temperature
#计算band7与band14在00分和10分的差值
band7_band14_00temperature_difference = band7_00_00_temperature - band14_00_00_temperature
band7_band14_10temperature_difference = band7_00_10_temperature - band14_00_10_temperature
#计算10分的band7与00分的band14的差值
band7_band14_0010temperature_difference = band7_00_10_temperature - band14_00_00_temperature
#判断火点依据
fire_point_image = []
for y in range(len(band7_00_00_temperature)):
row = []
for x in range(len(band7_00_00_temperature[y])):
if band7_00_00_temperature[y][x] > 260 and band7_00_10_temperature[y][x] > 260 and band7_temperature_difference[y][x]>15 and band7_band14_10temperature_difference[y][x] >10 and band14_temperature_difference[y][x] >-1 and band7_band14_0010temperature_difference[y][x] >12:
row.append(0) # 白色
else:
row.append(225) # 黑色
fire_point_image.append(row)
fire_point_image = np.array(fire_point_image, dtype=np.uint8)
# 保存火点图像到本地
cv2.imwrite("fire_point_image_2.tif", fire_point_image)
# Open target TIFF for update
target_dataset = gdal.Open("fire_point_image_2.tif", gdal.GA_Update)
dataset_00_geo_transform = dataset_00.GetGeoTransform()
# Apply the source geo-transform to the target TIFF
target_dataset.SetGeoTransform(dataset_00_geo_transform)
# Close the datasets to save changes
dataset_00 = None
dataset_10 = None
target_dataset = None
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()