Last active
June 7, 2017 03:01
-
-
Save dboyliao/bb7449e42afb2297bff9bcbbd48f591e 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
| #!/usr/bin/env python3 | |
| # -*- coding: utf8 -*- | |
| import sys, argparse | |
| try: | |
| import cv2 | |
| import numpy as np | |
| from scipy.ndimage import convolve | |
| except ImportError: | |
| print("install opencv and scipy please", file=sys.stderr) | |
| sys.exit(1) | |
| def main(img_path, y_direct=False): | |
| img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE).astype(np.float) | |
| kernel = np.array([[-1, 0, 1], | |
| [-2, 0, 2], | |
| [-1, 0, 1]])/8 | |
| if y_direct: | |
| kernel = kernel.T | |
| deriv = convolve(img, kernel) | |
| return deriv | |
| if __name__ == "__main__": | |
| import matplotlib.pyplot as plt | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("img_path", metavar="IMAGE_PATH", help="input image path") | |
| parser.add_argument("-y", "--y-direct", | |
| dest="y_direct", action="store_true", | |
| help="compute derivative along y axis") | |
| args = vars(parser.parse_args()) | |
| deriv = main(**args) | |
| plt.imshow(np.abs(deriv), cmap='gray') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment