How to apply a ScikitLearn classifier to tiles/windows in a large image

社会主义新天地 提交于 2021-02-08 20:53:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!