Image to features#

Links: notebook, html, PDF, python, slides, GitHub

Read an image with opencv.

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from jyquickhelper import add_notebook_menu
add_notebook_menu()

OpenCV#

import numpy as np
import argparse
import cv2
from numpy import matrix
import os
from functools import reduce

# create NumPy arrays from the boundaries
lower = np.array([0, 0, 0], dtype = "uint8")
upper = np.array([100, 115, 240], dtype = "uint8")

i=0
List= []
folder = "tomates"

if not os.path.exists(folder):
    raise FileNotFoundError(os.path.abspath(folder))

for element in os.listdir(folder):
    i+=1
    # load the image
    image = cv2.imread(os.path.join(folder, element))
    res = cv2.resize(image,(100, 100), interpolation = cv2.INTER_CUBIC)

    # find the colors within the specified boundaries and apply the mask
    mask = cv2.inRange(res, lower, upper)
    output = cv2.bitwise_and(res, res, mask = mask)
    shape = output.shape
    nb = reduce(lambda a,b: a*b, shape)
    mat = output.reshape((1, nb))
    List.append(mat)
    print(element, mat.shape)

bigmat = np.vstack(List)
bigmat.shape
imgt_61.jpg (1, 30000)
imgt_66.jpg (1, 30000)
(2, 30000)
from pyquickhelper.helpgen import NbImage
NbImage("tomates/imgt_61.jpg")
../_images/image_features_5_0.jpg