21
$\begingroup$

In my class I have to create an application using two classifiers to decide whether an object in an image is an example of phylum porifera (seasponge) or some other object.

However, I am completely lost when it comes to feature extraction techniques in python. My advisor convinced me to use images which haven't been covered in class.

Can anyone direct me towards meaningful documentation or reading or suggest methods to consider?

$\endgroup$
2
  • $\begingroup$ You mentioned advisor, so I'd assume this is part of a Graduate School assignment? Do you have access to any commercial software, or are you expected to do this with only Python and open-source packages? What are you learning about in class at the moment and what is the name of the class? Also, is there a performance requirement in terms of time it should take to give an answer? $\endgroup$
    – MLowry
    Nov 15, 2015 at 3:22
  • $\begingroup$ I am expected to only use Python and open source packages. Writing my own source code is discouraged, even. This is a master's level course. The class is an introductory Data Science course. The last thing we covered is feature selection, though almost all of the discussion is about text data. There are no performance requirements outside of an accuracy ~70% $\endgroup$ Nov 15, 2015 at 18:35

3 Answers 3

12
$\begingroup$

In images, some frequently used techniques for feature extraction are binarizing and blurring

Binarizing: converts the image array into 1s and 0s. This is done while converting the image to a 2D image. Even gray-scaling can also be used. It gives you a numerical matrix of the image. Grayscale takes much lesser space when stored on Disc.

This is how you do it in Python:

from PIL import Image

%matplotlib inline  

#Import an image
image = Image.open("xyz.jpg")

image

Example Image:

enter image description here

Now, convert into gray-scale:

im = image.convert('L')

im

will return you this image:

enter image description here

And the matrix can be seen by running this:

array(im)

The array would look something like this:

array([[213, 213, 213, ..., 176, 176, 176],
       [213, 213, 213, ..., 176, 176, 176],
       [213, 213, 213, ..., 175, 175, 175],
       ..., 
       [173, 173, 173, ..., 204, 204, 204],
       [173, 173, 173, ..., 205, 205, 204],
       [173, 173, 173, ..., 205, 205, 205]], dtype=uint8)

Now, use a histogram plot and/or a contour plot to have a look at the image features:

from pylab import *

# create a new figure
figure()
gray()
# show contours with origin upper left corner
contour(im, origin='image')
axis('equal')
axis('off')


figure()


hist(im_array.flatten(), 128)

show()

This would return you a plot, which looks something like this:

enter image description here enter image description here

Blurring: Blurring algorithm takes weighted average of neighbouring pixels to incorporate surroundings color into every pixel. It enhances the contours better and helps in understanding the features and their importance better.

And this is how you do it in Python:

from PIL import *


figure()
p = image.convert("L").filter(ImageFilter.GaussianBlur(radius = 2))
p.show()

And the blurred image is:

enter image description here

So, these are some ways in which you can do feature engineering. And for advanced methods, you have to understand the basics of Computer Vision and neural networks, and also the different types of filters and their significance and the math behind them.

$\endgroup$
2
  • 1
    $\begingroup$ Thank you so much. I posted about this on a few places and yours was by far the most informative answer. I realized that I was misunderstanding how feature extraction of images works conceptually. $\endgroup$ Nov 15, 2015 at 19:38
  • $\begingroup$ Glad that my answer helped you :) $\endgroup$
    – Dawny33
    Nov 16, 2015 at 0:53
8
$\begingroup$

This great tutorial covers the basics of convolutional neuraltworks, which are currently achieving state of the art performance in most vision tasks:

http://deeplearning.net/tutorial/lenet.html

There are a number of options for CNNs in python, including Theano and the libraries built on top of it (I found keras to be easy to use).

If you prefer to avoid deep learning, you might look into OpenCV, which can learn many other types of features, line Haar cascades and SIFT features.

http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_feature2d/py_table_of_contents_feature2d/py_table_of_contents_feature2d.html

$\endgroup$
1
$\begingroup$

As Jeremy Barnes and Jamesmf said, you can use any machine learning algorithms to deal with the problem. They are powerful and could identify the features automatically. You just need to feed the algorithm the correct training data. Since it is needed to work on images, convolution neural networks will be a better option for you .

This is a good tutorial for learning about the convolution neural network. You could download the code also and could change according to your problem definition. But you need to learn python and theano library for the processing and you will get good tutorials for that too

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.