0
$\begingroup$

I'm currently working on a project that involves classifying an image as either that of a dog or that of a cat. The twist is that I want to do this without using Convolutional Neural Networks, mainly because I do not quite understand them yet and I don't just want to copy someone's code off of Github.

I know that the classification algorithms on Sci-Kit Learn require the x input data to be in vectorized form for the classifier to fit the data, but I'm unsure of how to do this. It's easy to do with text data (feature_selection.text.CountVectorizer/TfidfVectorizer), but I have no idea how this works with images. How do I convert .jpg image files to vectors/matrices so that the models on SK-Learn understand it?

Thanks in advance, and I'm sorry if this is a stupid question.

$\endgroup$
1
  • $\begingroup$ You can try to use numpy flatten() $\endgroup$
    – RABI Hamza
    Jun 11, 2020 at 21:36

4 Answers 4

0
$\begingroup$

You can use imread function from imageio library with np.array from numpy:

from imageio import imread
import numpy as np
filename = '/path/to/dog.jpg'
vectorized_picture = np.array(imread(filename))

When entering this data into a ML engine, you'll probably need to reshape it to a column matrix (at least in Prof. Andrew Ng's Coursera Machine Learning courses that I've been doing). If so, you can do that by:

reshaped_vectorized_picture = vectorized_picture.reshape(1, -1).T

You need to install both libraries through apt, brew or pip (preferred one)

pip install numpy imageio
$\endgroup$
1
  • $\begingroup$ His question is something different. Vectorization concepts which are used for text data, not simple vectors $\endgroup$
    – 10xAI
    Jun 12, 2020 at 2:53
0
$\begingroup$

For texts, we need vectorization method because all the tokens we have in our input data needs to be in numerical format to be processed by any of the algorithm we working in , and I agree there are plenty of function to do that.

But it works different in terms of images. As image is nothing but a two dimensional array of its pixel value. If you have a colored image of 512X512, its nothing but array shaped 512X512X3 (3 represents the color channel) with values ranging from 0 to 255.

You can extract this by reading it through libraries like opencv, imageio, PIL etc.

$\endgroup$
0
$\begingroup$

I use the module skimage, you can import it by:

from skimage import data

You can get imported the image to one variable like:

picture_imported = imageio.imread('picture.jpg')

But this variable is imageio.core.util.Array In order to get this as a ndarray just:

picture = np.copy(picture_imported)
$\endgroup$
0
$\begingroup$

An image contains some values in it. Lets suppose in an image a person is standing in front of tree, so we can say that every object is defined by different values, suppose for tree the value assigned is 1, for person it is 2 and for background 0. Well this is only for example, in reality the values are ranging from 0-255 for each pixels for black and white image, and for RGB image there are 3 channels.

These pixel values denote the intensity of the pixels. The smaller numbers closer to zero represent the darker shade while the larger numbers closer to 255 represent the lighter or the white shade. (To be honest i also don't know how this values get assigned but we can assume).

After all of this, we perform normalization on each pixels (scaling data to the range 0-1 is known as normalization). Scaling pixels in the range 0-1 can be done by setting the rescale argument by dividing pixel's max value by pixel's min value: 1/255 = 0.0039.

And as per my knowledge finally after getting our scaled values, this values are used as vectors to perform further operations like doing calculations with convolution layers and so on.. .....correct me if i'm wrong....

New contributor
MORE OMKAR RAJENDRA SHOBHA is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
1
  • $\begingroup$ Welcome to Data Science Stack Exchange. While your answer contains some useful detail about images, is doesn't really address the OP's question - How do I convert .jpg image files to vectors/matrices? Can you please edit your answer to include this information? $\endgroup$
    – Lynn
    Aug 7 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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