Extract feature vector from 2d image in numpy

时光怂恿深爱的人放手 提交于 2019-12-10 11:55:15

问题


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:

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.


回答1:


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])



回答2:


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.



来源:https://stackoverflow.com/questions/49180941/extract-feature-vector-from-2d-image-in-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!