Loading project/minimal_examples/extract_features.py +32 −15 Original line number Diff line number Diff line Loading @@ -2,34 +2,51 @@ Minimal example of - extracting features from an image and saving them in a file - flattening the features - detecting edges in an image and saving the edge image - resizing the image and saving its features in a file """ import numpy as np from skimage.io import imread # requirement: pip install scikit-image from PIL import Image import cv2 # requirement: pip install opencv-python import cv2 from skimage.io import imread # Read image from file image = imread("../figures/banana.jpg", as_gray=False) print("Shape of image: ", image.shape) image = imread("../figures/examples_from_dataset/banana.jpg", as_gray=False) print("Shape of original image: ", image.shape) # Save features in file with open('features-banana.txt', 'w') as f: f.write(str(image)) # Flatten feature array features = np.reshape(image, (image.shape[0] * image.shape[1] * image.shape[2])) features = image.flatten() #features = np.reshape(image, (image.shape[0] * image.shape[1] * image.shape[2])) # Alternative to flatten print("Shape of features after flattening: ", features.shape) with open('features-banana-flattened.txt', 'w') as f: f.write(str(features)) print("Original features saved in file 'features-banana.txt'") print("Flattened features saved in file 'features-banana-flattened.txt'\n") # Resizing image to 50x50 image_resized = cv2.resize(image, (50, 50)) # Save features in file with open('features-banana-resized.txt', 'w') as f: f.write(str(image_resized)) print("Shape of resized image: ", image_resized.shape) # Flatten feature array of resized image features_resized = image_resized.flatten() with open('features-banana-resized-flattened.txt', 'w') as f: f.write(str(features_resized)) print("Shape of resized image after flattening: ", features_resized.shape) print("Flattened features of resized image saved in file 'features-banana-resized-flattened.txt'") print("Features saved in file 'features-banana-resized.txt'") # Detect edges img = Image.open("../figures/banana.jpg") img = np.array(img) edge_img = cv2.Canny(img, threshold1=50, threshold2=80) #cv2.imshow("Edge Image", edge_img) #cv2.waitKey(0) # Save image cv2.imwrite('../figures/banana-edges.jpg', edge_img) Loading
project/minimal_examples/extract_features.py +32 −15 Original line number Diff line number Diff line Loading @@ -2,34 +2,51 @@ Minimal example of - extracting features from an image and saving them in a file - flattening the features - detecting edges in an image and saving the edge image - resizing the image and saving its features in a file """ import numpy as np from skimage.io import imread # requirement: pip install scikit-image from PIL import Image import cv2 # requirement: pip install opencv-python import cv2 from skimage.io import imread # Read image from file image = imread("../figures/banana.jpg", as_gray=False) print("Shape of image: ", image.shape) image = imread("../figures/examples_from_dataset/banana.jpg", as_gray=False) print("Shape of original image: ", image.shape) # Save features in file with open('features-banana.txt', 'w') as f: f.write(str(image)) # Flatten feature array features = np.reshape(image, (image.shape[0] * image.shape[1] * image.shape[2])) features = image.flatten() #features = np.reshape(image, (image.shape[0] * image.shape[1] * image.shape[2])) # Alternative to flatten print("Shape of features after flattening: ", features.shape) with open('features-banana-flattened.txt', 'w') as f: f.write(str(features)) print("Original features saved in file 'features-banana.txt'") print("Flattened features saved in file 'features-banana-flattened.txt'\n") # Resizing image to 50x50 image_resized = cv2.resize(image, (50, 50)) # Save features in file with open('features-banana-resized.txt', 'w') as f: f.write(str(image_resized)) print("Shape of resized image: ", image_resized.shape) # Flatten feature array of resized image features_resized = image_resized.flatten() with open('features-banana-resized-flattened.txt', 'w') as f: f.write(str(features_resized)) print("Shape of resized image after flattening: ", features_resized.shape) print("Flattened features of resized image saved in file 'features-banana-resized-flattened.txt'") print("Features saved in file 'features-banana-resized.txt'") # Detect edges img = Image.open("../figures/banana.jpg") img = np.array(img) edge_img = cv2.Canny(img, threshold1=50, threshold2=80) #cv2.imshow("Edge Image", edge_img) #cv2.waitKey(0) # Save image cv2.imwrite('../figures/banana-edges.jpg', edge_img)