|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
from tifffile import imread
from skimage import filters, feature, img_as_ubyte
import matplotlib.pyplot as plt
import numpy as np
from skimage.transform import hough_line, hough_line_peaks
# 使用tifffile库读取TIFF格式的RGB影像
image = imread('F:\\duneline\\dune\\dune.tif')
# 将影像转换为8位无符号整数格式,因为后续的图像处理函数需要这种格式
image = img_as_ubyte(image)
# 使用Sobel边缘检测
edges = filters.sobel(image)
# 使用Hough变换找出可能的直线
tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 360)
h, theta, d = hough_line(edges, theta=tested_angles)
# 在原图上画出沙丘脊线
fig, ax = plt.subplots()
ax.imshow(image, cmap=plt.cm.gray)
for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
y0 = (dist - 0 * np.cos(angle)) / np.sin(angle)
y1 = (dist - image.shape[1] * np.cos(angle)) / np.sin(angle)
ax.plot((0, image.shape[1]), (y0, y1), '-r')
plt.show()
报错:
Traceback (most recent call last):
File "F:/duneline/duneline.py", line 29, in <module>
h, theta, d = hough_line(edges, theta=tested_angles)
File "C:\安装软件\Python\lib\site-packages\skimage\transform\hough_transform.py", line 218, in hough_line
raise ValueError('The input image `image` must be 2D.')
ValueError: The input image `image` must be 2D.
本帖最后由 学习编程中的Ben 于 2023-8-4 16:03 编辑
错误提示显示 ValueError: The input image 'image' must be 2D.,即输入的图像必须是二维的。这个错误很可能是因为影像加载后转换为了 RGB 格式,而 Hough 变换函数需要的是灰度图像(即二维图像)。
要解决这个问题,可以将 RGB 彩色图像转换为灰度图像,然后再进行边缘检测和 Hough 变换。下面是修改后的代码:
- from tifffile import imread
- from skimage import filters, feature, img_as_ubyte, color
- import matplotlib.pyplot as plt
- import numpy as np
- from skimage.transform import hough_line, hough_line_peaks
- # 使用tifffile库读取TIFF格式的RGB影像
- image = imread('F:\\duneline\\dune\\dune.tif')
- # 将影像转换为灰度图像
- gray_image = color.rgb2gray(image)
- # 将灰度图像转换为8位无符号整数格式,因为后续的图像处理函数需要这种格式
- edges = filters.sobel(img_as_ubyte(gray_image))
- # 使用Hough变换找出可能的直线
- tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 360)
- h, theta, d = hough_line(edges, theta=tested_angles)
- # 在原图上画出沙丘脊线
- fig, ax = plt.subplots()
- ax.imshow(image, cmap=plt.cm.gray)
- for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
- y0 = (dist - 0 * np.cos(angle)) / np.sin(angle)
- y1 = (dist - image.shape[1] * np.cos(angle)) / np.sin(angle)
- ax.plot((0, image.shape[1]), (y0, y1), '-r')
- plt.show()
复制代码
修改的部分是将图像转换为灰度图像 gray_image = color.rgb2gray(image),然后使用灰度图像进行边缘检测和 Hough 变换。执行以上修改后的代码,应该可以解决报错问题并正确运行。
如果回答对你有帮助,请给我一个最佳答案! 
|
|