|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import matplotlib.pyplot as plt
# Load the image
image = img_as_float(io.imread('C:\\Users\\WINDOWS\\Desktop\\taiyuan\\python\\seg.png'))
# Apply SLIC and extract (approximately) the supplied number of segments
segments = slic(image, n_segments = 100, sigma = 5)
# Show the output of SLIC
fig = plt.figure("Superpixels")
ax = fig.add_subplot(1, 1, 1)
ax.imshow(mark_boundaries(image, segments))
plt.axis("off")
报错:
Traceback (most recent call last):
File "C:\Users\WINDOWS\Desktop\taiyuan\python\segement.py", line 13, in <module>
ax.imshow(mark_boundaries(image, segments))
File "F:\anaconda\setup\lib\site-packages\skimage\segmentation\boundaries.py", line 239, in mark_boundaries
marked[boundaries] = color
ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (18932,4)
Process finished with exit code 1
问题分析:
报错信息显示在执行 ax.imshow(mark_boundaries(image, segments))时出现了错误。具体错误提示为: ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (18932,4)。
错误原因:
该错误通常出现在调用 mark_boundaries函数时,给定的颜色数组与边界数组的形状不匹配。在这种情况下,颜色数组有一个长度为3的维度,而边界数组带有两个维度(一个表示像素位置,另一个表示标签)。
解决方法:
修改代码中的颜色数组,确保其形状与边界数组匹配。将颜色数组由长度为3的一维数组修改为具有四个元素的二维数组。每个元素表示RGBA颜色值,其中A表示透明度。
以下是修改后的代码:
- from skimage.segmentation import slic
- from skimage.segmentation import mark_boundaries
- from skimage.util import img_as_float
- from skimage import io
- import matplotlib.pyplot as plt
- # Load the image
- image = img_as_float(io.imread('C:\\Users\\WINDOWS\\Desktop\\taiyuan\\python\\seg.png'))
- # Apply SLIC and extract (approximately) the supplied number of segments
- segments = slic(image, n_segments=100, sigma=5)
- # Show the output of SLIC
- fig = plt.figure("Superpixels")
- ax = fig.add_subplot(1, 1, 1)
- # Modify the color array to match the shape of the boundaries array
- color = [[1, 0, 0, 1]] # Red color with opacity
- ax.imshow(mark_boundaries(image, segments, color))
- plt.axis("off")
- plt.show()
复制代码
在修改后的代码中,我们将颜色数组修改为 [[1, 0, 0, 1]],代表红色和不透明度为1。这样,颜色数组的形状就与边界数组匹配了。
请尝试运行修改后的代码,查看是否可以成功显示带有颜色边界的图像。如有其他问题,请随时提问。
如果回答对你有帮助,请给我一个最佳答案! 
|
|