使用Sklearn的img_to_graph

时间:2014-06-19 20:33:57

标签: python image-processing scikit-learn feature-detection

我有以下代码:

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'对象没有属性' img_to_graph'

如何转换我的强度"将numpy数组转换为正确的img_to_graph属性?

1 个答案:

答案 0 :(得分:2)

您正在使用image = sklearn.feature_extraction.image覆盖已导入的image = cv2.imread("/home/facu/holo.tif",0),因此无法再访问该函数img_to_graph

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

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

并相应地调整其余部分。