OpenCV python 圖像梯度–sobel與scharr對比
import cv2
def main():
# 1.導入圖檔
img_src = cv2.imread("source.jpg")
img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
# 2.執行sobel邊緣資訊
img_sobel_x = cv2.Sobel(img_gray, cv2.CV_64F, 1, 0, ksize=3)
img_sobel_y = cv2.Sobel(img_gray, cv2.CV_64F, 0, 1, ksize=3)
img_sobel_x = cv2.convertScaleAbs(img_sobel_x)
img_sobel_y = cv2.convertScaleAbs(img_sobel_y)
img_sobel_xy = cv2.addWeighted(img_sobel_x, 0.5, img_sobel_y, 0.5, 0)
# 3.執行scharr邊緣資訊
img_scharr_x = cv2.Scharr(img_gray, cv2.CV_64F, 1, 0)
img_scharr_y = cv2.Scharr(img_gray, cv2.CV_64F, 0, 1)
img_scharr_x = cv2.convertScaleAbs(img_scharr_x)
img_scharr_y = cv2.convertScaleAbs(img_scharr_y)
img_scharr_xy = cv2.addWeighted(img_scharr_x, 0.5, img_scharr_y, 0.5, 0)
# 4.顯示結果
cv2.imshow("img_src", img_src)
cv2.imshow("img_sobel_xy", img_sobel_xy)
cv2.imshow("img_scharr_xy", img_scharr_xy)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
處理結果[img_sobel_xy.jpg]
處理結果[img_scharr_xy.jpg]