OpenCV python 图像梯度--sobel与scharr对比

OpenCV python 图像梯度–sobel与scharr对比

OpenCV python 图像梯度--sobel与scharr对比_第1张图片

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]
OpenCV python 图像梯度--sobel与scharr对比_第2张图片
处理结果[img_scharr_xy.jpg]
OpenCV python 图像梯度--sobel与scharr对比_第3张图片

你可能感兴趣的:(Opencv-python)