2 Star 4 Fork 3

汉塞大叔/Geospatial_Analysis_By_Python

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
7.2.1 生成图片直方图.py 2.47 KB
Copy Edit Raw Blame History
汉塞大叔 authored 2021-01-20 20:59 +08:00 . 上传py
"""
直方图可以显示一个数据集中数据分布频率的统计结果;在遥感领域,数据集就是图片。其中的
数据分布就是区间在0~255之间的像素数出现的次数;
在RGB图形中,颜色是由数字组成的3位元祖表达,其中(0,0,0)代表黑色,(255,255,255)
代表白色;
直方图 y轴代表频率值,x轴代表区间中256种像素值;
"""
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)
hist = n[1:] - n[:-1]
print("a:", a)
print("fa:", fa)
# print("1:", n[1:])
# print("2:", n[:-1])
# print("3:", hist)
return hist
def draw_histogram(hist, scale=True):
# 画x、y轴坐标系
t.color("black")
axes = ((-355, -200), (355, -200), (-355, -200), (-355, 250))
t.up()
for p in axes:
t.goto(p)
t.down()
# 画 x轴的名称
t.up()
t.goto(0, -250)
t.write("VALUE", font=("Arial", 12, "bold"))
# 画 x轴的刻度
t.up()
x = -355
y = -200
for i in range(1,11):
x = x + 65
t.goto(x, y)
t.down()
t.goto(x, y-10)
t.up()
t.goto(x, y-25)
t.write("{}".format((i*25)), align="center")
# 画 y轴的刻度
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
t.goto(x, y)
t.down()
t.goto(x-10, y)
t.up()
t.goto(x-15, y-6)
t.write("{}".format((i*label)), align="right")
# 画R、G、B三色曲线
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()
t.goto(x, y)
t.down()
t.color(colors[j])
for i in range(255):
x = i * x_ratio
y = h[i] * y_ratio
x = x - (709/2)
y = y + -199
t.goto(x, y)
arr = gdal_array.LoadFile("TIF/FalseColor/swap.tif")
histograms = []
for b in arr:
histograms.append(histogram(b))
draw_histogram(histograms)
t.pen(shown=False)
t.done()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/fengfeng233/geospatial_-analysis_-by_-python.git
git@gitee.com:fengfeng233/geospatial_-analysis_-by_-python.git
fengfeng233
geospatial_-analysis_-by_-python
Geospatial_Analysis_By_Python
master

Search