Created
December 19, 2017 22:54
-
-
Save fedden/d9c3e001dc664389a93402e93b599442 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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