Skip to content

Instantly share code, notes, and snippets.

@fedden
Created December 19, 2017 22:54
Show Gist options
  • Save fedden/d9c3e001dc664389a93402e93b599442 to your computer and use it in GitHub Desktop.
Save fedden/d9c3e001dc664389a93402e93b599442 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import os
def auto_canny(image, sigma=0.33):
median = np.median(image)
lower = int(max(0, (1.0 - sigma) * median))
upper = int(min(255, (1.0 + sigma) * median))
return cv2.Canny(image, lower, upper)
def preprocess(image_path):
image = cv2.imread(image_path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return cv2.GaussianBlur(gray_image, (3, 3), 0)
images_directory = "snow2/images"
target_directory = "snow2/edges"
if not os.path.exists(target_directory):
os.makedirs(target_directory)
files = os.listdir(images_directory)
amount_images = len(list(images_directory))
counter = 0
for file_path in files:
if file_path.endswith(".png"):
image_path = os.path.join(images_directory, file_path)
image = preprocess(image_path)
edge = 255 - auto_canny(image)
target_path = os.path.join(target_directory, file_path)
cv2.imwrite(target_path, edge)
print(target_path, end="\r")
counter += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment