I am attempting to read a 4-band (red, green, blue, near-infrared) geotiff (example data) and perform a quickshift segmentation using the scikit-image
module in Python.
I have created the following script (based on the 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)
I get the following error:
ValueError: the input array must be have a shape == (.., ..,[ ..,] 3)), got (4, 436, 553)
I am pretty sure the numpy array needs to be reshaped somehow. How can I properly read the multiband geotiff into a numpy array and perform the image segmentation?
I believe your problem is that
quickshift()
thinks your image isrgb
. I downloaded a random image from the link you provided and read it into skimage.I resized it to 128x128x4 (to make computation easy)
then ran
quickshift()
and got the same error.
Higher up in the stack trace it says
So you can see
_quickshift.pyx
is trying to convertrgb --> xyz
and thenxyz --> lab
. So its assuming your image isrgb
. The skimage docs forquickshift()
shows it has a flagconvert2lab
that defaults toTrue
.If I re-run your function with that flag set to
False
it runs.
Edit:
Just as an aside, I noticed your image shape is
(4, 436, 553)
which is also problematic.skimage
expects the color channel to be last. This can be remedied with