Skip to content

Instantly share code, notes, and snippets.

@init27
Last active August 31, 2018 16:57
Show Gist options
  • Save init27/a7ca88ee7754596bb61aa9c0571e722b to your computer and use it in GitHub Desktop.
Save init27/a7ca88ee7754596bb61aa9c0571e722b to your computer and use it in GitHub Desktop.
import argparse
import cv2
import numpy as np
image = cv2.imread("data/blur.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
ap = argparse.ArgumentParser()
ap.add_argument("-l", "--lower-angle", type=float, default=175.0,
help="Lower orientation angle")
ap.add_argument("-u", "--upper-angle", type=float, default=180.0,
help="Upper orientation angle")
args = vars(ap.parse_args())
gX = cv2.Sobel(gray, ddepth=cv2.CV_64F, dx=1, dy=0)
gY = cv2.Sobel(gray, ddepth=cv2.CV_64F, dx=0, dy=1)
mag = np.sqrt((gX ** 2) + (gY ** 2))
orientation = np.arctan2(gY, gX) * (180 / np.pi) % 180
idxs = np.where(orientation >= args["lower_angle"], orientation, -1)
idxs = np.where(orientation <= args["upper_angle"], idxs, -1)
mask = np.zeros(gray.shape, dtype="uint8")
mask[idxs > -1] = 255
cv2.imshow("Mask", mask)
cv2.imwrite("data/angle.jpg",mask)
cv2.waitKey(0)
gX = cv2.convertScaleAbs(gX)
gY = cv2.convertScaleAbs(gY)
sobelCombined = cv2.addWeighted(gX, 10.5, gY, 10.5, 0)
cv2.imshow("Sobel X", gX)
cv2.imshow("Sobel Y", gY)
cv2.imshow("Sobel Combined", sobelCombined)
cv2.imwrite("data/Sobel_Combined.jpg", sobelCombined)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment