Skip to content

Instantly share code, notes, and snippets.

@jimlinntu
Created November 15, 2020 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimlinntu/3ddbf392aca06555e24e9e67b0534704 to your computer and use it in GitHub Desktop.
Save jimlinntu/3ddbf392aca06555e24e9e67b0534704 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
def main():
img = cv2.imread("./demo.jpeg", cv2.IMREAD_COLOR)
sobel_kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.int32)
# NOTE: filter2D is actually computing correlation so we flip the kernel here
# See: https://docs.opencv.org/4.5.0/d4/d86/group__imgproc__filter.html#ga27c049795ce870216ddfb366086b5a04
# for more information
img_grad_x = cv2.filter2D(img, -1, sobel_kernel)
sobel_kernel = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=np.int32)
img_grad_y = cv2.filter2D(img, -1, sobel_kernel)
blur = cv2.blur(img, (5, 5))
cv2.imwrite("blur.jpeg", blur)
cv2.imwrite("img_grad_x.jpeg", img_grad_x)
cv2.imwrite("img_grad_y.jpeg", img_grad_y)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment