惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
L
LINUX DO - 热门话题
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
H
Hacker News: Front Page
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
Scott Helme
Scott Helme
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyberwarzone
Cyberwarzone
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
Cloudbric
Cloudbric
Simon Willison's Weblog
Simon Willison's Weblog
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
T
Tor Project blog
O
OpenAI News
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Exploit Database - CXSecurity.com
MyScale Blog
MyScale Blog
罗磊的独立博客
美团技术团队
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
P
Privacy International News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Blog — PlanetScale
Blog — PlanetScale

HaoKunT的博客

如何用 ollama 快速下载 deepseek 模型 - HaoKunT的博客 Word中写LaTeX公式 - HaoKunT的博客 g++中的rpath和runpath - HaoKunT的博客 Ext4文件系统 - HaoKunT的博客 文件系统介绍 - HaoKunT的博客 理解shell - HaoKunT的博客 Hyper V安装ENVI - HaoKunT的博客 华为软件实习生笔试 - HaoKunT的博客 用C++实现一个命令行进度条 - HaoKunT的博客 PCA与GWPCA - HaoKunT的博客 字节后台实习生笔试题目 - HaoKunT的博客 Web终端仿真器 - HaoKunT的博客 阿里云API网关与函数计算的基础理解 - HaoKunT的博客 函数计算搭建DNS服务器 - HaoKunT的博客 DNS解析过程 - HaoKunT的博客 Github图片加载不出来 - HaoKunT的博客 将Elementary OS装在U盘中 - HaoKunT的博客 MacOS+Windows 双系统的安装 - HaoKunT的博客 IPXE+netboot+ISCSI 网络启动 - HaoKunT的博客 Esxi+NAS+Openwrt - HaoKunT的博客 Esxi的安装和使用 - HaoKunT的博客 利用acme自动更新证书 - HaoKunT的博客 Golang使用海康威视SDK - HaoKunT的博客 Filetools工具 - HaoKunT的博客 学习正则表达式 - HaoKunT的博客 Vugu View - HaoKunT的博客 Django Restframework 嵌套序列化 - HaoKunT的博客 看不了netlify的部署日志 - HaoKunT的博客 Go Modules的使用 - HaoKunT的博客 使用hugo+netlify部署个人主页 - HaoKunT的博客 关于我 - HaoKunT的博客 在线工具大全 - HaoKunT的博客 SWIG编译海康威视SDK 使用golang - HaoKunT的博客 SWIG 以Python为例 - HaoKunT的博客 Seafile - HaoKunT的博客 Golang的小技巧 - HaoKunT的博客 归档 - HaoKunT的博客 搜索 - HaoKunT的博客
python使用gdal - HaoKunT的博客
HaoKunT · 2021-02-26 · via HaoKunT的博客

本文为原创文章,转载注明出处,欢迎关注网站https://hkvision.cn

缘起

一个项目需要用GDAL这个库,然后去稍微深入看了下这玩意,感觉还是有很多坑的。

简介

就不介绍了,需要用这玩意的肯定知道这是啥

读写

最重要的肯定是读写

读:

1
2
3
4
5
6
7
8
9
from osgeo import gdal
ds = gdal.Open("{image_path}")
"""
有很多种Open的方法
OpenShared
OpenEx
还可以设置只读
flags=gdal.GA_ReadOnly
"""

写:

1
2
drv = gdal.GetDriverByName("GTiff")
drv.Create("{filepath}", xsize, ysize, bds_num, eType. options=[])

看起来一切都很美好,但是这里面蕴含着一个很大的坑,读影像还好,但是写影像需要指定路径,并且,gdal没有额外的方式能创建一个空的DataSet,这意味着如果你想弄一些临时的DataSet(这非常普遍,稍微复杂一点的算法都会需要有这个),你必须要存在硬盘上一份文件,这简直太蠢了

VRT

当然这也不是没法解决的,GDAL其实有解决方案,那就是VRT文件,这个是GDAL的一种虚拟文件,是对影像的一种抽象表述,这个设定是很有意思的,给大家看个VRT文件的例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<VRTDataset rasterXSize="4000" rasterYSize="4000" subClass="VRTPansharpenedDataset">
  <Metadata domain="IMAGE_STRUCTURE">
    <MDI key="INTERLEAVE">PIXEL</MDI>
  </Metadata>
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTPansharpenedRasterBand">
    <ColorInterp>Red</ColorInterp>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="2" subClass="VRTPansharpenedRasterBand">
    <ColorInterp>Green</ColorInterp>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="3" subClass="VRTPansharpenedRasterBand">
    <ColorInterp>Blue</ColorInterp>
  </VRTRasterBand>
  <BlockXSize>512</BlockXSize>
  <BlockYSize>512</BlockYSize>
  <PansharpeningOptions>
    <Algorithm>WeightedBrovey</Algorithm>
    <AlgorithmOptions>
      <Weights>0.3333333333333333,0.3333333333333333,0.3333333333333333</Weights>
    </AlgorithmOptions>
    <Resampling>Cubic</Resampling>
    <SpatialExtentAdjustment>Union</SpatialExtentAdjustment>
    <PanchroBand>
      <SourceFilename relativeToVRT="1">pan_mask_opencv.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </PanchroBand>
    <SpectralBand dstBand="1">
      <SourceFilename relativeToVRT="1">origin_mask_opencv.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SpectralBand>
    <SpectralBand dstBand="2">
      <SourceFilename relativeToVRT="1">origin_mask_opencv.tif</SourceFilename>
      <SourceBand>2</SourceBand>
    </SpectralBand>
    <SpectralBand dstBand="3">
      <SourceFilename relativeToVRT="1">origin_mask_opencv.tif</SourceFilename>
      <SourceBand>3</SourceBand>
    </SpectralBand>
  </PansharpeningOptions>
</VRTDataset>

这里我们定义了一个Pansharpened文件,也就是一个影像融合之后的影像,但是实际上在硬盘上的只是一个这样的描述文件,我如果想把他变成真正的影像文件,我可以这样做

1
2
3
4
5
from osgeo import gdal

ds = gdal.Open("pansharpened.vrt")
drv = gdal.GetDriverByName("GTiff")
drv.CreateCopy("pansharpened.tif", ds)

这样我就将一个很小的vrt文件变成了tif文件。

聪明的你肯定看出来了,这个vrt文件在实际编程的时候和这个tif文件没啥区别,但是不一样的是,vrt文件只占用这么小的空间,这是很爽的事情。

那么我们可以更进一步,这个vrt文件我都不想看到,怎么办?也可以,你只需要这样做

1
2
3
4
5
# doing something

# now we want to create a new DataSet
drv = gdal.GetDriverByName("VRT")
ds = drv.Create("", xoff, yoff, bds_num, eType. options=[])

是的,如果是vrt文件,大可以在filename这个参数上填空串,这样就不会出现这个文件了。至于这个文件是放在了内存中还是再怎么样,我也就不关心了(GDAL也有内存文件,可以参考这个

一些命令

GDAL除了提供库以外,还提供了一些命令行程序,可以对影像进行一些简单的操作,具体的内容就不细说了,看这里

B站上还有个视频教程,不过是全英文无字幕的,看着操作还是能明白是什么意思,在这里

part1

part2