6

I am looking for an easy way to count the number of the green pixels in the image below, where the original image is the same but the green pixels are black.

I tried it with numpy.diff(), but then I am counting some pixels twice. I thought about numpy.gradient() – but here I am not sure if it is the right tool.

I know there have to be many solutions to this problem, but I don't know how to google for it. I am looking for a solution in python.

Image with a T

To make it clearer, I have only one image (only black and white pixels). The image with the green pixel is just for illustration.

7
  • So you have two images? And you want to count the pixels that are green in one but black in the other image? Commented Nov 14, 2021 at 12:10
  • 2
    If I understood correctly, you would like to have a function that takes a binary image as an input and returns a list of all edge pixels. I think the answer on this question is what you're looking for. Please note it is not that efficient but it definitely is simple. stackoverflow.com/questions/60095053/… Commented Nov 14, 2021 at 12:16
  • I only want to count the pixels, this answer is to inefficient for me Commented Nov 14, 2021 at 12:28
  • 1
    Your image is incoherent : the tops of the T have the diagonals cells counted, while the inner short line of 3 do not have the diagonal cells included. Please refine your definition of neighbor pixels and edit the image accordingly. Commented Nov 14, 2021 at 12:37
  • 1
    @KarlKnechtel yes Commented Nov 14, 2021 at 13:23

1 Answer 1

6

You can use the edge detection kernel for this problem.

import numpy as np
from scipy.ndimage import convolve

a = np.array([[0, 0, 0, 0],
              [0, 1, 1, 1],
              [0, 1, 1, 1]])

kernel = np.array([[-1, -1, -1],
                   [-1,  8, -1],
                   [-1, -1, -1]])

Then, we will convolve the original array with the kernel. Notice that the edges are all negatives.

>>> convolve(a, kernel)
[[-1 -2 -3 -3]
 [-2  5  3  3]
 [-3  3  0  0]]

We will count the number of negative values and get the result.

>>> np.where(convolve(a, kernel) < 0, 1, 0)
[[1 1 1 1]
 [1 0 0 0]
 [1 0 0 0]]

>>> np.sum(np.where(convolve(a, kernel) < 0, 1, 0))
6

Edges-only kernel

There are a lot of things you can do with the kernel. For example, you can modify the kernel if you don't want to include diagonal neighbors.

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

This gives the following output.

>>> np.where(convolve(a, kernel) < 0, 1, 0)
[[0 1 1 1]
 [1 0 0 0]
 [1 0 0 0]]

>>> np.sum(np.where(convolve(a, kernel) < 0, 1, 0))
5
Sign up to request clarification or add additional context in comments.

3 Comments

would switching to excluded diagonal neighbors be as simple as setting them to 0 in the kernel ?
@sybog64 Yes, you can use the np.array([[ 0, -1, 0], [-1, 4, -1], [0, -1, 0]]) kernel if you don't want to include diagonal neighbors.
This answer is perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.