Skip to content

Instantly share code, notes, and snippets.

@YHaruoka
Last active May 11, 2022 02:52
Show Gist options
  • Save YHaruoka/c3c1cc66f7c54491698c22380a3ac950 to your computer and use it in GitHub Desktop.
Save YHaruoka/c3c1cc66f7c54491698c22380a3ac950 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import sys
def main():
print("OpenCV Version: " + str(cv2.__version__))
# Loading image data (GRAYSCALE)
filename = "image.png"
image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
if image is None:
print("Cannot find image data : " + filename)
sys.exit()
# Prewitt filter (horizontal)
image_prewitt_h = prewitt_horizontal(image)
# Prewitt filter (vertical)
image_prewitt_v = prewitt_vertical(image)
# Prewitt filter (horizontal & vertical)
image_prewitt = prewitt(image)
# Saving image
cv2.imwrite('image_prewitt_h.png',image_prewitt_h)
cv2.imwrite('image_prewitt_v.png',image_prewitt_v)
cv2.imwrite('image_prewitt.png',image_prewitt)
def prewitt_horizontal(image):
kernel_prewitt_h = np.array([[-1, -1, -1],
[0, 0, 0],
[1, 1, 1]])
image_output = cv2.filter2D(image, cv2.CV_64F, kernel_prewitt_h)
return image_output
def prewitt_vertical(image):
kernel_prewitt_v = np.array([[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1]])
image_output = cv2.filter2D(image, cv2.CV_64F, kernel_prewitt_v)
return image_output
def prewitt(image):
image_h = prewitt_horizontal(image)
image_v = prewitt_vertical(image)
image_output = np.sqrt(image_h ** 2 + image_v ** 2)
return image_output
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment