Skip to content

Instantly share code, notes, and snippets.

@svpino
Created February 8, 2021 20:37
Show Gist options
  • Save svpino/be7ba9b873216e18079dab183e86b37c to your computer and use it in GitHub Desktop.
Save svpino/be7ba9b873216e18079dab183e86b37c to your computer and use it in GitHub Desktop.
Convolutions using OpenCV
import cv2
import numpy as np
from matplotlib import pyplot as plt
image = cv2.imread("building.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
horizontal_edges = cv2.filter2D(image, -1, np.array([
[-1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]
]))
vertical_edges = cv2.filter2D(image, -1, np.array([
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]
]))
f, axs = plt.subplots(1, 3, figsize=(20, 20))
plt.subplot(1, 3, 1), plt.imshow(image)
plt.subplot(1, 3, 2), plt.imshow(horizontal_edges)
plt.subplot(1, 3, 3), plt.imshow(vertical_edges)
plt.setp(plt.gcf().get_axes(), xticks=[], yticks=[])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment