[[SOLVED]] Count pixels with distinct neighborhood

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
echinotrix
Posts: 5
Joined: 2013-08-18T11:28:30-07:00
Authentication code: 6789

[[SOLVED]] Count pixels with distinct neighborhood

Post by echinotrix »

Dear Forum Members,

after searching for many hours both here and everywhere, I decided to ask for help in the forum.

In a binary black-and-white image, I want to count the number of black pixels that have one or more white pixels in its direct neighborhood (8 surrounding pixels).

This shall serve as an approximation for the perimeter of the black area.

Can anybody help me? I do not have any idea how to do that, not even how to start.


Kind regards,

echinotrix
Last edited by echinotrix on 2013-08-20T23:02:20-07:00, edited 1 time in total.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Count pixels with distinct neighborhood

Post by GreenKoopa »

Welcome. Morphology is a neighborhood operator.
http://www.imagemagick.org/Usage/morphology/

Can you provide an example image and give any constraints that may help? "binary black-and-white image" helps clarify. Does perimeter mean that you always have a single, solid, black shape on a white background?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Count pixels with distinct neighborhood

Post by snibgo »

I would start with morphology. See http://www.imagemagick.org/Usage/morphology/

Create a test black/white image. Windows script:

Code: Select all

convert -size 200x200 xc:black ^
  -fill white ^
  -draw "rectangle 50,50 150,150" ^
  -draw "rectangle 175,175 176,176" ^
  e.png
There are various ways of doing this. You say you just want to count the adjacent pixels. We can dilate (ie enlarge) the white pixels, then count how may have changed.

Code: Select all

convert e.png -morphology Dilate Square ed.png
compare -metric AE e.png ed.png NULL:
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count pixels with distinct neighborhood

Post by fmw42 »

try -morphology edgout square:1 (or perhaps diamond:1)

see http://www.imagemagick.org/Usage/morphology/#edgeout
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Count pixels with distinct neighborhood

Post by anthony »

For counting, a better solution is a convolution, with a special 'counting kernel'.
The exact kernel however depend on just what you call a 'neighbourhood'.

For examples see
IM Examples, Convolution , Counting Neighbours
http://www.imagemagick.org/Usage/convolve/#counting

For a practical example see the very next section... Game of Life This uses neighbour counting to lookup what a pixel value in the 'next generation should be (regadless of if it is (black or white).
http://www.imagemagick.org/Usage/convolve/#life
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
echinotrix
Posts: 5
Joined: 2013-08-18T11:28:30-07:00
Authentication code: 6789

Re: Count pixels with distinct neighborhood

Post by echinotrix »

Thanks for your help so far.

-morphology gives me just what I asked for: an approximation for the perimeter of a black area on a white background. Here is a sample image: Image

However, counting black pixels with >=1 white neighbor it is just an approximation. Maybe there is an exact way to determine the perimeter of the black area, e.g. counting the number of pixel sides (!) that seperate black from white pixels.

I hope anyone can understand what I mean.

Thanks again.

P.S.: Here a little illustration of what I mean. When counting the black pixels with white neighbors, the resulting value is 29 (first image). When counting the pixel sides that "touch" a black and a white pixel, the value is 28 (second image). The second is what I want.

Image
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Count pixels with distinct neighborhood

Post by snibgo »

You've confused me. What are the yellow pixels? Are they black ones you have counted?

I can count pixels. I don't know how to count edges.

Maybe you could count the black pixels that touch white ones. Then count white pixels that touch black ones. Then take the average of the two. Would that give the answer you want?

Or you could count black pixels that touch white ones horizontally, then count black pixels that touch white vertically, and add the two. Is that the right answer?
snibgo's IM pages: im.snibgo.com
echinotrix
Posts: 5
Joined: 2013-08-18T11:28:30-07:00
Authentication code: 6789

Re: Count pixels with distinct neighborhood

Post by echinotrix »

Sorry for the confusion, actually I wanted to clarify what I mean ;)

The yellow pixels in the first image are the (black) pixels that are counted. The yellow line in the second image represents the edges to count.

However, I think your second solution is nearly what I want: counting black pixels that touch white ones horizontally, then count black pixels that touch white vertically, and add the two. Only one thing: black pixels that touch white pixels at two horizontal sides have to count twice, as there are two edges that contribute to the perimeter. The same applies to black pixels that touch white ones vertically.

Can you give me a example how to restrict counting to horizontal/vertical touches?

P.S.: A possible solution would be: Count black pixels that touch white ones at the top, then at the left side, at the buttom, and at the right side, respectively. Then add the four values. This would be the exact perimeter. Is this possible with IM?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Count pixels with distinct neighborhood

Post by snibgo »

Anthony will give you the best answer, 'cos he wrote morphology, but here is my go:

Code: Select all

%IM%convert e.png ^
  -verbose ^
  -morphology Thicken "2x1+1+0:1,0" ed.png

%IM%compare -metric AE e.png ed.png NULL:

%IM%convert e.png ^
  -verbose ^
  -morphology Thicken "2x1+0+0:0,1" ed.png

%IM%compare -metric AE e.png ed.png NULL:

%IM%convert e.png ^
  -verbose ^
  -morphology Thicken "1x2+0+1:1,0" ed.png

%IM%compare -metric AE e.png ed.png NULL:

%IM%convert e.png ^
  -verbose ^
  -morphology Thicken "1x2+0+0:0,1" ed.png

%IM%compare -metric AE e.png ed.png NULL:
The verbose will tell you how many pixels have changed. Compare should give the same number.

The first looks for black to the right of white.

The second looks for black to the left of white.

The third looks for black below white.

The fourth looks for black above white.

Thus a black pixel surrounded by 8 white pixels will be counted 4 times.
snibgo's IM pages: im.snibgo.com
echinotrix
Posts: 5
Joined: 2013-08-18T11:28:30-07:00
Authentication code: 6789

Re: Count pixels with distinct neighborhood

Post by echinotrix »

Thanks everybody, perfect.

I'll give it a try and report.

Great forum!
echinotrix
Posts: 5
Joined: 2013-08-18T11:28:30-07:00
Authentication code: 6789

Re: Count pixels with distinct neighborhood

Post by echinotrix »

Works like a charm.

Thanks again.

P.S.: How can I mark the thread as solved?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Count pixels with distinct neighborhood

Post by snibgo »

Good stuff. You can edit your first entry in the thread, inserting "SOLVED " at the start of the subject.
snibgo's IM pages: im.snibgo.com
Post Reply