基于python实现遥感影像预览图生成 | 您所在的位置:网站首页 › arcgis合成多波段图像 › 基于python实现遥感影像预览图生成 |
基于python实现遥感影像预览图生成
原创
GIS韩 2023-05-28 00:25:13 博主文章分类:Python ©著作权 文章标签 gdal python opencv 遥感影像 预览 文章分类 Python 后端开发 ©著作权归作者所有:来自51CTO博客作者GIS韩的原创作品,请联系作者获取转载授权,否则将追究法律责任 目的遥感影像通常无法直接查看,还需要经过专业软件打开,才能看到其具体内容。最近,需要开发对遥感影像直接生成预览图,就使用GDAL提取波段信息,通过opencv写入图片 环境python3.5gdal2.4.1opencv-python代码from osgeo import gdal import cv2 import numpy as np np.seterr(divide='ignore', invalid='ignore') def create_preview(dataset, target_path, red=1, green=2, blue=3): """ 生成遥感影像的预览图 :param dataset: 影像数据 :param red: 预览图红色采用影像的波段索引 :param green: 预览图绿色采用影像的波段索引 :param blue: 预览图蓝色采用影像的波段索引 :param target_path: 预览图存储地址 :return: """ band_count = dataset.RasterCount cols = dataset.RasterXSize # 列数 rows = dataset.RasterYSize # 行数 # 多波段影像根据红绿蓝索引合成图片 if band_count >= 3: band_red = dataset.GetRasterBand(red) data_red = translate(band_red, cols, rows) band_green = dataset.GetRasterBand(green) data_green = translate(band_green, cols, rows) band_blue = dataset.GetRasterBand(blue) data_blue = translate(band_blue, cols, rows) x = np.zeros([rows, cols, 3], dtype=np.uint8) for row_index in range(rows): for col_index in range(cols): x[row_index][col_index][0] = data_red[row_index][col_index] x[row_index][col_index][1] = data_green[row_index][col_index] x[row_index][col_index][2] = data_blue[row_index][col_index] cv2.imwrite(target_path, x) # 单波段影像直接合成灰度 else: band = dataset.GetRasterBand(1) cv2.imwrite(target_path, translate(band, cols, rows)) def translate(band, cols, rows): """将一个波段的值转到0-255的区间""" band_data = band.ReadAsArray(0, 0, cols, rows) min = band.GetMinimum() max = band.GetMaximum() nodata = band.GetNoDataValue() band_data[band_data == nodata] = min band_data = band_data / max * 255 return band_data.astype(np.uint8) driver = gdal.GetDriverByName('GTiff') # 多波段影像 dataset = gdal.Open("E:/商丘yx/t50smc_20181101t025839_b01.img") target_path = "C:\\Users\\Administrator\\Pictures\\test_copy1.png" create_preview(dataset, target_path, red=8, green=5, blue=4)结果 选择的波段可能不太适合,生成的颜色偏暗
结果预览 存在问题多波段影像效率低生成的预览图太大 赞 收藏 评论 分享 举报上一篇:RabbitMQ消息队列使用测试 下一篇:IOS swift开发——获取设备定位信息 |
CopyRight 2018-2019 实验室设备网 版权所有 |