如何将ScikitLearn分类器应用于大图像中的图块/窗口 [英] How to apply a ScikitLearn classifier to tiles/windows in a large image

查看:59
本文介绍了如何将ScikitLearn分类器应用于大图像中的图块/窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Given是scikit学习中训练有素的分类器,例如 RandomForestClassifier .分类器已经过训练,样本大小为,例如.25x25.

Given is a trained classifer in scikit learn, e.g. a RandomForestClassifier. The classifier has been trained on samples of size e.g. 25x25.

如何轻松将其应用于大图像(例如640x480)中的所有图块/窗口?

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 )

如何将 clf 应用于 img 中的所有25x25窗口?

How can I apply clf to all 25x25 windows in img?

推荐答案

也许您正在寻找类似

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.

如果使用 view_as_windows 对您来说是一种负担得起的方法,则可以通过如下所示重塑返回的数组,从图像中的所有窗口神奇地生成测试数据:

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)

这篇关于如何将ScikitLearn分类器应用于大图像中的图块/窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆