gpt4 book ai didi

python - 如何使用 Python 的 scikit-image 对 4 波段 geotiff 进行图像分割?

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:41 24 4
gpt4 key购买 nike

我正在尝试读取 4 波段(红色、绿色、蓝色、近红外)geotiff ( example data ) 并执行 quickshift segmentation在 Python 中使用 scikit-image 模块。

我创建了以下脚本(基于 scikit example ):

from __future__ import print_function
from osgeo import gdal
import matplotlib.pyplot as plt
import numpy as np

from skimage.segmentation import felzenszwalb, slic, quickshift
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float

image = r'C:\path\to\my\geotiff.tif'
img = io.imread(image, as_grey=False, plugin="gdal")
segments_quick = quickshift(img, kernel_size=3, max_dist=6, ratio=0.5)

我收到以下错误:

ValueError: the input array must be have a shape == (.., ..,[ ..,] 3)), got (4, 436, 553)

我很确定 numpy 数组需要以某种方式 reshape 。如何将多波段 geotiff 正确读入 numpy 数组并执行图像分割?

最佳答案

我认为您的问题是 quickshift() 认为您的图像是 rgb。我从您提供的链接下载了一张随机图片,并将其读入 skimage。

img = io.imread('./m_4111722_ne_11_1_20100704.tif')

我将它的大小调整为 128x128x4(以便于计算)

img = transform.resize(img, (128, 128, 4))

然后运行 ​​quickshift()

segments = quickshift(img, kernel_size=3, max_dist=6, ratio=0.5)

同样的错误。

ValueError: the input array must be have a shape == (.., ..,[ ..,] 3)), got (128, 128, 4)

在堆栈跟踪中更高的地方它说

skimage/segmentation/_quickshift.pyx inskimage.segmentation._quickshift.\
quickshift (skimage/segmentation/_quickshift.c:1710)()

/****/****/anaconda/lib/python2.7/site-packages/skimage/color/colorconv.pyc in rgb2lab(rgb)
901 This function uses rgb2xyz and xyz2lab.
902 """
--> 903 return xyz2lab(rgb2xyz(rgb))

因此您可以看到 _quickshift.pyx 正在尝试转换 rgb --> xyz 然后是 xyz --> lab。所以它假设您的图像是rgbskimage docs quickshift() 显示它有一个标志 convert2lab,默认为 True

convert2lab : bool, optional (default True) Whether the input should be converted to Lab colorspace prior to segmentation. For this purpose, the input is assumed to be RGB.

如果我将该标志设置为 False

重新运行您的函数
segments = quickshift(img, kernel_size=3, convert2lab=False, max_dist=6, ratio=0.5)

它运行。

plt.imshow(segments);

enter image description here

编辑:

顺便说一句,我注意到你的图像形状是 (4, 436, 553) 这也是有问题的。 skimage 期望颜色 channel 在最后。这可以通过以下方式解决

img = img.transpose(1, 2, 0)

关于python - 如何使用 Python 的 scikit-image 对 4 波段 geotiff 进行图像分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31036971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com