遥感图像处理中常用的python操作

遥感图像处理中常⽤的python操作
本章节主要参考《python地理空间分析指南》第六章。
⽂章中的所有操作都可以在ENVI中完成,这⾥只提供⼀个图像处理的思路。
⼀、图像波段变换
波段变换最常⽤的地⽅就是进⾏图像显⽰,例如使⽤假彩⾊图像以凸显植被信息。图像波段变换即将图像波段的组合顺序重新排列并显⽰的⽅法,主要使⽤GDAL库。下⾯以⼀个例⼦进⾏实验,⾸先给出数据的下载地址:
打开原图可以看到,植被明显呈现红⾊(植被在绿光波段反射率⾼,⽽图像将绿光波段放到了红⾊通道),说明该图为假彩⾊影像
在本⽰例中,将使⽤gdal_array模块加载图⽚到numpy数组中,然后将其另存为新的geotiff⽂件。需要注意的是,numpy引⽤数组的⽅式是(y,x)(⾏,列)型的,常⽤的电⼦表格及其他软件则是(x,y)(⾏,列)型。
下⾯给出波段变换的代码:
from osgeo import gdal_array
# 源图⽚的名称
src = "FalseColor.tif"
# 将源图⽚载⼊到数组中
arr = gdal_array.LoadFile(src)
# 交换波段1和波段2的位置,使⽤“⾼级分⽚”功能直接对波段进⾏重新排列
output = gdal_array.SaveArray(arr[[1, 0, 2], :], "swap.tif", format="GTiff",prototype=src)
# 取消输出,避免在某些平台上损坏⽂件
output = None
波段变换后的影像为:
⼆、创建直⽅图
这⾥创建的直⽅图的是像元值得频数直⽅图,横轴代表像元值,纵轴代表像元数量。本⽰例使⽤的数
据是上述波段变换的结果swap.tif影像。⾸先给出创建直⽅图的代码:
from osgeo import gdal_array
import turtle as t
def histogram(a, bins=list(range(0, 256))):
fa = a.flat
n = gdal_array.numpy.searchsorted(gdal_array.numpy.sort(fa), bins)
n = gdal_atenate([n, [len(fa)]])调盘
hist = n[1:] - n[:-1]
return hist
def draw_histogram(hist, scale=True):
axes = ((-355, -200), (355, -200), (-355, -200), (-355, 250))
t.up()
for p in axes:
<(p)
t.down()
t.up()
<(0, -250)
t.write("VALUE", font=("Arial, ", 12, "bold"))
t.up()
<(-400, 280)
t.write("FREQUENCY", font=("Arial, ", 12, "bold"))
e通话
x = -355
y = -200
t.up()
for i in range(1, 11):
x = x+65
<(x, y)
t.down()
<(x, y-10)
t.up()
<(x, y - 25)
t.write("{}".format((i*25)), align="center")
x = -355
x = -355
y = -200
t.up()
pixels = sum(hist[0])
if scale:
max = 0
for h in hist:
hmax = h.max()
if hmax > max:
max = hmax
pixels = max
label = int(pixels/10)
for i in range(1, 11):
y = y+45
钢制重力式无阀过滤器<(x, y)
t.down()
<(x-10, y)
t.up()
<(x-15, y-6)
t.write("{}".format((i*label)), align="right")    x_ratio = 709.0 / 256
y_ratio = 450.0 / pixels
colors = ["red", "green", "blue"]
for j in range(len(hist)):
h = hist[j]
x = -354
y = -199
t.up()
<(x, y)
t.down()
for i in range(256):
x = i * x_ratio
y = h[i] * y_ratio
x = x - (709/2)
y = y + -199
<((x, y))
img = "swap.tif"
histograms = []
arr = gdal_array.LoadFile(img)
for b in arr:
histograms.append(histogram(b))
高频电子水处理器draw_histogram(histograms)
t.pen(shown=False)
t.done
测绘工具执⾏上述代码,创建出的直⽅图为:
为了将上述代码进⾏绝对⽐例显⽰,我们将上述代码的倒数第三⾏修改为:
draw_histogram(histograms, scale=False)
执⾏上述代码,显⽰的结果为:
可以看到,图像的灰度分布明显偏暗,因此我们需要对直⽅图进⾏均衡化处理。直⽅图均衡化处理后的影像的明暗程度更为均匀。下⾯给出直⽅图均衡化的代码:
from osgeo import gdal_array
金属表面喷涂
import operator
from functools import reduce
def histogram(a, bins=list(range(0, 256))):
fa = a.flat
n = gdal_array.numpy.searchsorted(gdal_array.numpy.sort(fa), bins)
n = gdal_atenate([n, [len(fa)]])
hist = n[1:] - n[:-1]
return hist
def stretch(a):
"""
在gdal_array上传的图像数组中执⾏直⽅图均衡化操作
"""
hist = histogram(a)
lut = []
for b in range(0, len(hist), 256):
# 步长尺⼨
step = reduce(operator.add, hist[b:b+256]) / 255
# 创建均衡的查表
n = 0
for i in range(256):
lut.append(n/step)
n = n + hist[i+b]
gdal_array.numpy.take(lut, a, out=a)
return a
src = "swap.tif"
arr = gdal_array.LoadFile(src)
stretched = stretch(arr)
output = gdal_array.SaveArray(arr, "stretched.tif", format="GTIFF", prototype=src) output = None
经过直⽅图均衡化后的影像结果为:

本文发布于:2024-09-20 23:48:01,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/147941.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:波段   图像   变换   给出   影像
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议