DavidS1992
DavidS1992

Reputation: 915

Efficient way to get the average value in a neighborhood of pixel, on mesh grid

My problem is quite complex but I'm not sure how can I split it.

I have the image and I'd like to put a grid (mesh grid?) on it, and then get the average color of the pixel on line crossings and its neighborhood.

The image below illustrates my task:

enter image description here

So eventually I want to know whether the stone is black, white, or the position is empty.

My question is how to efficiently get the pixel in the red area?

The vanilla solution is

x = np.linspace(0, image.shape[0], 19)
y = np.linspace(0, image.shape[1], 19)

for i in x:
    for j in y:
        color = image[i, j, :]

But it's not efficient and doesn't average within the neighborhood. There will also be some problems with

Upvotes: 0

Views: 566

Answers (1)

Prune
Prune

Reputation: 77837

The solution doesn't seem to support the task all that well. Yes, averaging the neighborhood color is one metric of the image there -- but as you say, it's inefficient.

You don't need the neighborhood: you need the stone classification. You could do this with a single pixel, or a handful, in areas that do not get overlaid with annotation information such as the move number.

From the intersection point, simply grab a pixel at, say, the 1:00 direction, 90* of the radius from the center. Adjust as needed for the line width. For instance, given a position (row, col) and stone radius r

x = col + 0.85 * r
y = row + 0.10 * r
color = image[x, y, :]
brightness = sum(color)
if brightness < 100:
    stone = "black"
elif brightness > 600:
    stone = "white"
else:
    stone = "vacant"

Upvotes: 1

Related Questions