Skip to main content
 首页 » 编程设计

python之使用 Sklearn 中的 img_to_graph

2024年10月25日64Free-Thinker

我有以下代码:

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 属性?

请您参考如下方法:

您正在用 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) 

并相应地调整其余部分。