Loading project/minimal_examples/apply_filters.py 0 → 100644 +54 −0 Original line number Diff line number Diff line """ Minimal example of - applying filters to an image and saving the filtered images in a file """ import cv2 from skimage.io import imread from skimage.filters import sobel # Read image from file image = imread("../figures/examples_from_dataset/banana.jpg", as_gray=False) # Resizing image to 50x50 image_resized = cv2.resize(image, (50, 50)) cv2.imwrite('../figures/examples_from_dataset/banana-resized.jpg', image_resized) # Use Canny edge detection filter and save to file edge_img = cv2.Canny(image, threshold1=50, threshold2=80) cv2.imwrite('../figures/examples_from_dataset/banana-edges.jpg', edge_img) # Use Canny edge detection filter for resized image and save to file edge_img_resized = cv2.Canny(image_resized, threshold1=50, threshold2=80) cv2.imwrite('../figures/examples_from_dataset/banana-edges-resized.jpg', edge_img_resized) # Use sobel filter and save to file sobel_img = sobel(image) cv2.imwrite('../figures/examples_from_dataset/banana-sobel.jpg', sobel_img * 255) # Use sobel filter for resized image and save to file sobel_img_resized = sobel(image_resized) cv2.imwrite('../figures/examples_from_dataset/banana-sobel-resized.jpg', sobel_img_resized * 255) # Use grayscale filter and save to file image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imwrite('../figures/examples_from_dataset/banana-gray.jpg', image_gray) # Use grayscale filter for resized image and save to file image_gray_resized = cv2.cvtColor(image_resized, cv2.COLOR_BGR2GRAY) cv2.imwrite('../figures/examples_from_dataset/banana-gray-resized.jpg', image_gray_resized) # Use HSV filter and save to file image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) cv2.imwrite('../figures/examples_from_dataset/banana-hsv.jpg', image_hsv) # Use HSV filter for resized image and save to file image_hsv_resized = cv2.cvtColor(image_resized, cv2.COLOR_BGR2HSV) cv2.imwrite('../figures/examples_from_dataset/banana-hsv-resized.jpg', image_hsv_resized) print("Applied the following filters to the image 'banana.jpg':") print("- Canny edge detection") print("- Sobel filter") print("- Grayscale") print("- HSV") print("Saved the images with applied filters in folder 'figures/examples_from_dataset'") Loading
project/minimal_examples/apply_filters.py 0 → 100644 +54 −0 Original line number Diff line number Diff line """ Minimal example of - applying filters to an image and saving the filtered images in a file """ import cv2 from skimage.io import imread from skimage.filters import sobel # Read image from file image = imread("../figures/examples_from_dataset/banana.jpg", as_gray=False) # Resizing image to 50x50 image_resized = cv2.resize(image, (50, 50)) cv2.imwrite('../figures/examples_from_dataset/banana-resized.jpg', image_resized) # Use Canny edge detection filter and save to file edge_img = cv2.Canny(image, threshold1=50, threshold2=80) cv2.imwrite('../figures/examples_from_dataset/banana-edges.jpg', edge_img) # Use Canny edge detection filter for resized image and save to file edge_img_resized = cv2.Canny(image_resized, threshold1=50, threshold2=80) cv2.imwrite('../figures/examples_from_dataset/banana-edges-resized.jpg', edge_img_resized) # Use sobel filter and save to file sobel_img = sobel(image) cv2.imwrite('../figures/examples_from_dataset/banana-sobel.jpg', sobel_img * 255) # Use sobel filter for resized image and save to file sobel_img_resized = sobel(image_resized) cv2.imwrite('../figures/examples_from_dataset/banana-sobel-resized.jpg', sobel_img_resized * 255) # Use grayscale filter and save to file image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imwrite('../figures/examples_from_dataset/banana-gray.jpg', image_gray) # Use grayscale filter for resized image and save to file image_gray_resized = cv2.cvtColor(image_resized, cv2.COLOR_BGR2GRAY) cv2.imwrite('../figures/examples_from_dataset/banana-gray-resized.jpg', image_gray_resized) # Use HSV filter and save to file image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) cv2.imwrite('../figures/examples_from_dataset/banana-hsv.jpg', image_hsv) # Use HSV filter for resized image and save to file image_hsv_resized = cv2.cvtColor(image_resized, cv2.COLOR_BGR2HSV) cv2.imwrite('../figures/examples_from_dataset/banana-hsv-resized.jpg', image_hsv_resized) print("Applied the following filters to the image 'banana.jpg':") print("- Canny edge detection") print("- Sobel filter") print("- Grayscale") print("- HSV") print("Saved the images with applied filters in folder 'figures/examples_from_dataset'")