问题
Given is a trained classifer in scikit learn, e.g. a RandomForestClassifier
. The classifier has been trained on samples of size e.g. 25x25.
How can I easily apply this to all tiles/windows in a large image (e.g. 640x480)?
What I could do is (slow code ahead!)
x_train = np.arange(25*25*1000).reshape(25,25,1000) # just some pseudo training data
y_train = np.arange(1000) # just some pseudo training labels
clf = RandomForestClassifier()
clf.train( ... ) #train the classifier
img = np.arange(640*480).reshape(640,480) #just some pseudo image data
clf.magicallyApplyToAllSubwindoes( img )
How can I apply clf
to all 25x25 windows in img
?
回答1:
Perhaps you are looking for something like skimage.util.view_as_windows. Please, be sure to read the caveat about memory usage at the end of the documentation.
If using view_as_windows
is an affordable approach for you, you could magically generate test data from all the windows in the image by reshaping the returned array like this:
import numpy as np
from skimage import io
from skimage.util import view_as_windows
img = io.imread('image_name.png')
window_shape = (25, 25)
windows = view_as_windows(img, window_shape)
n_windows = np.prod(windows.shape[:2])
n_pixels = np.prod(windows.shape[2:])
x_test = windows.reshape(n_windows, n_pixels)
clf.apply(x_test)
来源:https://stackoverflow.com/questions/42790356/how-to-apply-a-scikitlearn-classifier-to-tiles-windows-in-a-large-image