How to apply the ScikitLearn classifier to fragments / windows in a large image

Given that the trained class in learning scikit, for example. a RandomForestClassifier. The classifier was trained on samples of size, for example. 25x25.

How can I easily apply this to all fragments / windows of a large image (e.g. 640x480)?

What can I do (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 clfto all windows 25x25 in img?

+4
source share
1 answer

Perhaps you are looking for something like skimage.util.view_as_windows. Please remember to read the memory usage clause at the end of the documentation.

view_as_windows , , :

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)
+4

Source: https://habr.com/ru/post/1672270/


All Articles