Justheartyoung 发表于 2023-8-4 15:51:40

请各位大佬帮弟弟看看问题在哪 怎么改正 谢啦

代码:
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 * np.cos(angle)) / np.sin(angle)
    ax.plot((0, image.shape), (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.

isdkz 发表于 2023-8-4 15:52:00

问题出在图像处理函数hough_line()中,报错信息显示输入的图像必须是2D的,而你的图像是RGB格式的,所以需要将图像转换为灰度图像。可以使用skimage库中的rgb2gray()函数将RGB图像转换为灰度图像。

修改后的代码如下:

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位无符号整数格式,因为后续的图像处理函数需要这种格式
gray_image = img_as_ubyte(gray_image)
# 使用Sobel边缘检测
edges = filters.sobel(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 * np.cos(angle)) / np.sin(angle)
    ax.plot((0, image.shape), (y0, y1), '-r')
plt.show()


通过将RGB图像转换为灰度图像,即可解决报错问题。

tyh小号 发表于 2023-8-4 15:52:02

从错误信息来看,报错的原因是输入图像 image 必须是二维的,而你的代码中读取的是RGB格式的影像,是三维的。

解决这个问题的方法是将 RGB 彩色影像转换为灰度影像,然后进行后续的图像处理操作。

修改后的代码如下所示:

from tifffile import imread
from skimage import filters, 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')
# 将RGB影像转换为灰度图像
image_gray = img_as_ubyte(image[:,:,0])# 取红色通道作为灰度图像

# 使用Sobel边缘检测
edges = filters.sobel(image_gray)
# 使用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 * np.cos(angle)) / np.sin(angle)
    ax.plot((0, image.shape), (y0, y1), '-r')

plt.show()


这样应该能够解决报错问题。我们将 RGB 彩色影像转换为灰度图像,并使用灰度图像进行后续的处理。希望能帮到你!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案

学习编程中的Ben 发表于 2023-8-4 15:52:03

本帖最后由 学习编程中的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 * np.cos(angle)) / np.sin(angle)
    ax.plot((0, image.shape), (y0, y1), '-r')

plt.show()

修改的部分是将图像转换为灰度图像 gray_image = color.rgb2gray(image),然后使用灰度图像进行边缘检测和 Hough 变换。执行以上修改后的代码,应该可以解决报错问题并正确运行。

如果回答对你有帮助,请给我一个最佳答案!{:10_254:}{:10_254:}{:10_254:}

编程追风梦 发表于 2023-8-4 15:56:38

没问题,我会用中文解释给您听。

您遇到的问题是因为在进行霍夫直线变换之前,`edges` 图像不是二维的。问题出在 `edges` 这个图像,它是由 Sobel 边缘检测生成的,通常是一个二维的梯度幅值图像(也称为边缘图)。

在 scikit-image 中,霍夫直线变换需要一个二值化的二维图像,其中边缘被表示为白色像素(值为255),背景为黑色像素(值为0)。然而,Sobel 滤波生成的是一个灰度图像,其中像素值表示梯度幅值。

要解决这个问题,您需要在应用霍夫直线变换之前对梯度幅值图像进行阈值化,将其转换为二值图像。以下是您可以修改代码的方式:

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')
# 将影像转换为8位无符号整数格式,因为后续的图像处理函数需要这种格式
image = img_as_ubyte(image)
# 转换为灰度图像
gray_image = color.rgb2gray(image)
# 使用Sobel边缘检测
edges = filters.sobel(gray_image)
# 设置阈值将梯度幅值图像转换为二进制图像
threshold = 0.1# 您可能需要根据您的具体图像调整此阈值
binary_edges = edges > threshold
# 使用Hough变换找出可能的直线
tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 360)
h, theta, d = hough_line(binary_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 * np.cos(angle)) / np.sin(angle)
    ax.plot((0, image.shape), (y0, y1), '-r')
plt.show()


通过将图像转换为灰度图像并进行阈值化,您将得到一个二值图像,可以作为输入传递给霍夫直线变换,而不会出现二维输入错误。根据需要调整阈值,以获得最佳边缘检测结果。

给我个最佳答案吧,求求你了,我这个月还没有业绩,要垫底那!!!

Justheartyoung 发表于 2023-8-4 15:57:09

学习编程中的Ben 发表于 2023-8-4 15:52
错误提示显示 ValueError: The input image 'image' must be 2D.,即输入的图像必须是二维的。这个错误很可 ...

F:\duneline\myplot.png
请教下大佬为什么 输出的结果看着不对

Justheartyoung 发表于 2023-8-4 15:58:20

学习编程中的Ben 发表于 2023-8-4 15:52
错误提示显示 ValueError: The input image 'image' must be 2D.,即输入的图像必须是二维的。这个错误很可 ...

请教大佬,为什么最后plt.show()的输出图片不对

Justheartyoung 发表于 2023-8-4 15:59:18

isdkz 发表于 2023-8-4 15:52
问题出在图像处理函数hough_line()中,报错信息显示输入的图像必须是2D的,而你的图像是RGB格式的,所以需 ...

请教下大佬为啥输出结果 明显不对

Justheartyoung 发表于 2023-8-4 16:00:30

编程追风梦 发表于 2023-8-4 15:56
没问题,我会用中文解释给您听。

您遇到的问题是因为在进行霍夫直线变换之前,`edges` 图像不是二维的。 ...

可以运行,但是结果图明显不对,这是为什么大佬

学习编程中的Ben 发表于 2023-8-4 16:02:20

输出结果看起来不正确的原因可能有以下几个方面:

影像读取问题:请确认dune.tif文件路径是否正确,并且确保该文件是可用的 TIFF 格式影像文件。

灰度图像转换问题:使用color.rgb2gray()函数将彩色图像转换为灰度图像时,确保转换后的图像灰度值在正确的范围内(0-255)。

边缘检测参数问题:filters.sobel()函数可以根据实际情况调整参数以获得更好的边缘检测效果。你可以尝试调整参数或尝试其他边缘检测算法。

Hough 变换参数问题:hough_line()函数的输入参数也可能影响到输出结果。你可以尝试调整 tested_angles 参数的范围,或者尝试其他 Hough 变换方法。

绘图问题:绘制沙丘脊线时,确保坐标轴和直线的绘制逻辑是正确的。你可以尝试在绘制直线前打印相关参数(如angle和dist)来检查其值是否正确。

如果你能提供更多关于输入数据和预期结果的信息,我将能够更具体地帮助你解决问题。

学习编程中的Ben 发表于 2023-8-4 16:05:09

给个最佳答案呗!求你了!!!{:10_254:}

Justheartyoung 发表于 2023-8-4 16:11:25

学习编程中的Ben 发表于 2023-8-4 16:02
输出结果看起来不正确的原因可能有以下几个方面:

影像读取问题:请确认dune.tif文件路径是否正确,并且 ...

这个怎么没办法把结果图片发送给您,如果能让你看结果图就好了

编程追风梦 发表于 2023-8-4 16:48:07

Justheartyoung 发表于 2023-8-4 16:00
可以运行,但是结果图明显不对,这是为什么大佬

可以在发帖子的时候点击图片-->上传图片,记得用电脑哦~
页: [1]
查看完整版本: 请各位大佬帮弟弟看看问题在哪 怎么改正 谢啦