08.06.2015 Views

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Computer Vision – Pattern Recognition<br />

The just_filter=True argument is necessary, otherwise thresholding is performed<br />

and you get an estimate of where the edges are. The following screenshot shows the<br />

result of applying the filter (so that lighter areas are edgier) on the left and the result<br />

of thresholding on the right:<br />

Based on this operator, we may want to define a global feature as the overall<br />

edginess of the result:<br />

def edginess_sobel(image):<br />

edges = mh.sobel(image, just_filter=True)<br />

edges = edges.ravel()<br />

return np.sqrt(np.dot(edges, edges))<br />

In the last line, we used a trick to compute the root mean square—using the inner<br />

product function np.dot is equivalent to writing np.sum(edges ** 2), but much<br />

faster (we just need to make sure we unraveled the array first). Naturally, we<br />

could have thought up many different ways to achieve similar results. Using the<br />

thresholding operation and counting the fraction of pixels above threshold would<br />

be another obvious example.<br />

We can add this feature to the previous pipeline very easily:<br />

features = []<br />

for im in images:<br />

image = mh.imread(im)<br />

features.append(np.concatenate(<br />

mh.features.haralick(im).mean(0),<br />

# Build a 1-element list <strong>with</strong> our feature to match<br />

expectations<br />

# of np.concatenate<br />

[edginess_sobel(im)],<br />

))<br />

[ 214 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!