FacundoGFlores
FacundoGFlores

Reputation: 8118

Using img_to_graph from Sklearn

I have the following code:

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

So I'm trying to use the "spectral clustering" function but I get this error:

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

How can I do for converting my "intensity" numpy array into a correct img_to_graph attribute?

Upvotes: 2

Views: 1843

Answers (1)

eickenberg
eickenberg

Reputation: 14377

You are overwriting your imported image = sklearn.feature_extraction.image with image = cv2.imread("/home/facu/holo.tif",0), so the function img_to_graph will not be accessible anymore.

The solution is to rename one of them, e.g. with

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

and adjusting the rest accordingly.

Upvotes: 2

Related Questions