使用Sklearn的img_-to-_图

2024-03-29 08:03:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
from sklearn.feature_extraction import image
from sklearn.cluster import spectral_clustering

image = cv2.imread("/home/facu/holo.tif",0)
image = image


spectrum = np.fft.fftshift(np.fft.fft2(image))
intensity = 10*np.log(np.abs(spectrum))


mask = intensity.astype(bool)
img = intensity.astype(float)
graph = image.img_to_graph(img, mask=mask)
graph.data = np.exp(-graph.data/graph.data.std())

labels = spectral_clustering(graph, k=2, mode = 'arpack')
label_img = -np.ones(mask.shape)
label_im[mask] = labels

所以我试图使用“光谱聚类”函数,但我得到了一个错误:

AttributeError: 'numpy.ndarray' object has no attribute 'img_to_graph'

如何将“强度”numpy数组转换为正确的img_to_graph属性?在


Tags: tofromimageimportnumpyimgdataas
1条回答
网友
1楼 · 发布于 2024-03-29 08:03:29

您正在用image = cv2.imread("/home/facu/holo.tif",0)覆盖导入的image = sklearn.feature_extraction.image,因此函数img_to_graph将不再可访问。在

解决方案是将其中一个重命名,例如

raw_img = cv2.imread("/home/facu/holo.tif",0)

并相应地调整其余部分。在

相关问题 更多 >