MATLAB: How to count black pixels in a region of an image that can only have 1 white neighbor pixel

pixel count

I have a binary image (black background and white pixels). How do i count the number of blackpixels witch in it's neighbourhood of 5*7 (5 lines and 7 colums) can only have a white pixel.
I can try using bwhitmiss() but i would need 35 matrixs with one white pixel in each "matrix slot".
Can you tell me an easyest way to achieve this?

Best Answer

You could try leveraging conv2.
Here is the idea:
in = [
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 ]
k = [
1 1 1
1 1 1
1 1 1]
r = conv2(in,k,'same')
The result r will have 0 at all locations which DO NOT have 1's in the immediate 3x3 neighborhood.
(Have a look at conv2 and play with the positions of 1's in in to get a better idea)