0
votes

I have a series of 2d images of two types, either a star or a pentagon. My aim is to classify all of these images respectively. I have 30 star images and 30 pentagon images. An example of each image is shown side by side here:

Sample Images

Before I apply the KNN classification algorithm, I need to extract a feature vector from all the images. The feature vectors must all be of the same size however the 2d images all vary in size. I have extracted read in my image and I get back a 2d array with zeros and ones.

image = pl.imread('imagepath.png')

My question is how do I process image in order produce a meaningful feature vector that contains enough information to allow me to do the classification. It has to be a single vector per image which I will use for training and testing.

2
Just convert the 2-d pixel data to 1-dVivek Kumar
What would be a good way to standardize the size during the conversion so that all 1-d arrays are the same size?Visionary Zen
Resize the images to be of same size and then take the features.Vivek Kumar

2 Answers

3
votes

If you want to use opencv then:

Resize images to a standard size:

import cv2
import numpy as np

src = cv2.imread("/path.jpg")
target_size = (64,64)

dst = cv2.resize(src, target_size)

Convert to a 1D vector:

dst = dst.reshape(target_size.shape[0] * target_size.shape[1])
1
votes

Before you start coding, you have to decide whuch features are useful for this task:

The easiest way out is trying the approach in @Jordan's answer and converting the entire image to a feature. This could work because the classes are simple patterns, and is interesting if you are using KNN. If this does not work well, the following steps show how you should approach the problem.

  1. The number of black pixels might not help, because the size of the star and pentagon can vary.
  2. The number of sharp corners is very likely to be useful.
  3. The number of straight line segments might be useful, but this could be unreliable because the shapes are hand-drawn.

Supposing you want to have a go at using the number of corners as a feature, you can refer to this page to learn how to extract corners.