pythonkernel

How to make a circular kernel?


For the Code below, I am wondering how to make a circular kernel instead of a rectangular one. I am currently looking at something circular, and I want to find the BGR average values for it. By adjusting my kernel, my data will be more accurate.

for center in c_1:
    b = img2[center[0]-4: center[0]+5, center[1]-4: center[1]+5, 0]
    g = img2[center[0]-4: center[0]+5, center[1]-4: center[1]+5, 1]
    r = img2[center[0]-4: center[0]+5, center[1]-4: center[1]+5, 2]

Solution

  • Get the circle region when given the center, you could try the following function:

    def circleAverage(center, r = 4):
        """
        """
        for i in range(center[0]-r, center[0]+r):
            for j in range(center[1]-r, center[1] + r):
                if (center[0] - i) ** 2 + (center[1] - j) ** 2 <= r**2:
                    // do your computation here.
    

    Hope this helps you.