.. _imagefeaturesrst: ================= Image to features ================= .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`PDF `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/cheat_sheets/image_features.ipynb|*` Read an image with opencv. .. code:: ipython3 %matplotlib inline import matplotlib.pyplot as plt plt.style.use('ggplot') .. code:: ipython3 from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: OpenCV ------ .. code:: ipython3 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 .. parsed-literal:: imgt_61.jpg (1, 30000) imgt_66.jpg (1, 30000) .. parsed-literal:: (2, 30000) .. code:: ipython3 from pyquickhelper.helpgen import NbImage NbImage("tomates/imgt_61.jpg") .. image:: image_features_5_0.jpg