Skip to content

Instantly share code, notes, and snippets.

@Jim-Holmstroem
Created May 13, 2017 10:07
Show Gist options
  • Save Jim-Holmstroem/bd9f24d4bb0a5c30cf8609fe30bc4db2 to your computer and use it in GitHub Desktop.
Save Jim-Holmstroem/bd9f24d4bb0a5c30cf8609fe30bc4db2 to your computer and use it in GitHub Desktop.
Apply image kernel on file and create another file
from __future__ import division, print_function
import numpy as np
import cv2
COLOR = 1
GRAYSCALE = 0
UNCHANGED = -1
def kernel_transform(input_filename, output_filename):
img = cv2.imread(input_filename, UNCHANGED)
N = 5
kernel_blur = np.ones((N, N), np.float32) / N ** 2
kernel_deriv = np.array([
[ 0, 1, 0],
[ 1,-4, 1],
[ 0, 1, 0],
])
img = cv2.filter2D(img, -1, kernel_blur)
img = cv2.filter2D(img, -1, kernel_deriv)
cv2.imwrite(output_filename, img)
if __name__ == "__main__":
import sys
kernel_transform(input_filename=sys.argv[1], output_filename=sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment