|
发表于 2023-8-7 13:45:36
|
显示全部楼层
根据您的要求,我已经对原始代码进行了改进。下面是改进后的代码:
- from tifffile import imread, imwrite
- from skimage import filters, feature, color
- import matplotlib.pyplot as plt
- import numpy as np
- # 读取TIFF格式无人机影像数据
- image = imread('F:\\duneline\\dune\\dune.tif')
- # 转换为灰度图像
- gray_image = color.rgb2gray(image)
- # 边缘检测,提取沙丘脊线
- edges = filters.sobel(gray_image)
- # 计算局部自适应阈值
- threshold = filters.threshold_local(edges, block_size=51, offset=0.03)
- # 将阈值应用于图像,得到二值化图像
- binary = edges > threshold
- # 叠加沙脊线在原始影像上
- overlay = np.copy(image)
- overlay[binary] = [255, 0, 0] # 将沙脊线部分标记为红色
- # 导出结果
- imwrite('overlay_image.tif', overlay)
复制代码
在改进的代码中,我使用了 filters.threshold_local 函数计算局部自适应阈值。这个函数可以根据图像的局部特征来确定每个像素点的阈值。我设置了 block_size 参数为51,表示每个像素点的阈值将根据其周围51x51的邻域计算得出。同时,我将 offset 参数设置为0.03,作为阈值的偏移量。
然后,将计算得到的阈值应用于边缘图像 edges ,得到二值化图像 binary 。最后,将二值化图像叠加在原始影像 image 上,将沙脊线部分标记为红色,并导出结果为 overlay_image.tif 。
这样,您就可以得到具有局部自适应阈值的沙脊线二值化图像。 |
|