import cv2

# Read the original image
img = cv2.imread(r'C:\Users\harrizazham98\Desktop\OpenCVForPython\resources\Day 3\geometric-shape-names-types-definitions.png')
# Blur the image for better edge detection

gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Gradient x is calculated:
# the depth of the output is set to CV_16S to avoid overflow
# CV_16S = one channel of 2-byte signed integers (16-bit signed integers)
gradient_x = cv2.Sobel(gray_image, cv2.CV_16S, 1, 0, 3)
gradient_y = cv2.Sobel(gray_image, cv2.CV_16S, 0, 1, 3)

# Conversion to an unsigned 8-bit type:
abs_gradient_x = cv2.convertScaleAbs(gradient_x)
abs_gradient_y = cv2.convertScaleAbs(gradient_y)
# Combine the two images using the same weight:
sobel_image = cv2.addWeighted(abs_gradient_x, 0.5, abs_gradient_y, 0.5, 0)



# Display Sobel Edge Detection Images
cv2.imshow('Sobel X', abs_gradient_x)


cv2.imshow('Sobel Y', abs_gradient_y)


cv2.imshow('Sobel X Y using Sobel() function', sobel_image)
cv2.waitKey(0)
import cv2

# Read the original image
img = cv2.imread(r'C:\Users\harrizazham98\Desktop\OpenCVForPython\resources\Day 3\geometric-shape-names-types-definitions.png')
# Blur the image for better edge detection

gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Gradient x is calculated:
# the depth of the output is set to CV_16S to avoid overflow
# CV_16S = one channel of 2-byte signed integers (16-bit signed integers)
gradient_x = cv2.Sobel(gray_image, cv2.CV_16S, 1, 0, 3)
gradient_y = cv2.Sobel(gray_image, cv2.CV_16S, 0, 1, 3)

# Conversion to an unsigned 8-bit type:
abs_gradient_x = cv2.convertScaleAbs(gradient_x)
abs_gradient_y = cv2.convertScaleAbs(gradient_y)
# Combine the two images using the same weight:
sobel_image = cv2.addWeighted(abs_gradient_x, 0.5, abs_gradient_y, 0.5, 0)



# Display Sobel Edge Detection Images
cv2.imshow('Sobel X', abs_gradient_x)


cv2.imshow('Sobel Y', abs_gradient_y)


cv2.imshow('Sobel X Y using Sobel() function', sobel_image)
cv2.waitKey(0)
# Read the original image # Blur the image for better edge detection # Gradient x is calculated: # the depth of the output is set to CV_16S to avoid overflow # CV_16S = one channel of 2-byte signed integers (16-bit signed integers) # Conversion to an unsigned 8-bit type: # Combine the two images using the same weight: # Display Sobel Edge Detection Images