I have this raster:
which I'd like to convert to vector data (linestring) (e.g. a Shapefile).
To this end, I'm using this Python code:
import rasterio
import geopandas as gpd
from skimage.morphology import skeletonize
from skimage.measure import label, regionprops
segmented_raster_path = '/path/to/segmented_raster.tif'
with rasterio.open(segmented_raster_path, "r") as src:
img = src.read(1)
transform = src.transform
crs = src.crs
binary = (img > 0.1).astype(np.uint8) * 255
skel = skeletonize(binary).astype(np.uint8) * 255
labeled = label(skel, connectivity=2)
regions = regionprops(labeled)
lines = []
for region in regions:
coords = region.coords # row, col
if len(coords) < 2:
continue
coords_xy = [transform * (col, row) for row, col in coords] # convert to (x, y)
if len(coords_xy) > 1:
line = LineString(coords_xy)
if line.length >= 1: # filter very small elements
lines.append(line)
gdf = gpd.GeoDataFrame(geometry=lines, crs=crs)
gdf.to_file('extracted_lines.shp')
This results in:
And if I zoom in one of the "large blue regions" in the middle-top, the line forms a zigzag pattern:
Why is the result so strange, and how can I get correct, smooth lines following the center of the white stripes?





