How to make a new filter and apply it on an image using cv2 in python2.7?

后端 未结 2 1714
臣服心动
臣服心动 2021-01-03 04:24

How to make a new filter and apply it on an image using cv2 in python2.7?

For example:

kernel = np.array([[-1, -1, -1],
                   [-1,  4, -         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-03 05:17

    As far as applying a custom kernel to a given image you may simply use filter2D method to feed in a custom filter. You may also copy the following code to get you going. But The results with current filter seem a bit weird:

    import cv2
    import numpy as np
    
    # Create a dummy input image.
    canvas = np.zeros((100, 100), dtype=np.uint8)
    canvas = cv2.circle(canvas, (50, 50), 20, (255,), -1)
    
    kernel = np.array([[-1, -1, -1],
                       [-1, 4, -1],
                       [-1, -1, -1]])
    
    dst = cv2.filter2D(canvas, -1, kernel)
    cv2.imwrite("./filtered.png", dst)
    

    Input image:

    Output Image:

    EDIT: As per the edits suggested by @Dolphin, using a kernel with center value of 8, would get the good results in case of circular binary disc.

提交回复
热议问题