把GDAL读取数据信息转换为OPENCV的Mat类型的python代码
时间: 2024-05-11 19:14:17 浏览: 113
以下是将GDAL读取数据信息转换为OPENCV的Mat类型的Python代码:
```python
import gdal
import numpy as np
import cv2
# 打开影像
dataset = gdal.Open('your_file.tif', gdal.GA_ReadOnly)
# 获取影像的宽度和高度
width = dataset.RasterXSize
height = dataset.RasterYSize
# 获取影像的通道数和数据类型
bands = dataset.RasterCount
data_type = dataset.GetRasterBand(1).DataType
# 读取影像数据
image_data = np.zeros((height, width, bands), dtype=np.uint8)
for i in range(bands):
band = dataset.GetRasterBand(i + 1)
image_data[:, :, i] = band.ReadAsArray()
# 将影像数据转换为OPENCV的Mat类型
if bands == 1:
image_data = cv2.cvtColor(image_data, cv2.COLOR_GRAY2BGR)
image_data_cv = cv2.cvtColor(image_data, cv2.COLOR_BGR2RGB)
image_data_cv = cv2.convertScaleAbs(image_data_cv)
# 显示影像
cv2.imshow('image', image_data_cv)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文