'Image Processing - Skimage or other

I am new to image processing. I am trying out a few experiments.

  1. I have binarized my image with otsu
  2. Found connected pixels with skimage
from PIL import Image
import numpy as np
import skimage

im = Image.open("DMSO_Resized.png")
imgr = im.convert("L")
im2arr = np.array(imgr)
arr2im = Image.fromarray(im2arr)

thresh = skimage.filters.threshold_otsu(im2arr)
binary = im2arr > thresh
connected = skimage.morphology.label(binary)

I'd now like to count the number of background pixels that are either "completely" covered by other background pixels or "partially" covered.

For example, pixel[1][1] is partially covered 
1 0 2
0 0 0
3 0 8
AND
For example, pixel[1][1] is completely covered 
0 0 0
0 0 0
0 0 0

Is there a skimage or other package that has a method to do these ? Or would I have to implement them as an array processing loop ?

Sample image

Binarized image

import numpy as np
from skimage import morphology

bad_connection = np.array([[1, 0, 0, 0, 1],
                           [1, 0, 0, 0, 1],
                           [1, 0, 0, 0, 1],
                           [1, 0, 1, 0, 1],
                           [1, 0, 0, 0, 1]], dtype=np.uint8)

expected_good =  np.array([[0, 0, 1, 0, 0],
                           [0, 0, 1, 0, 0],
                           [0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0]], dtype=np.uint8)


another_bad   = np.array([[1, 0, 0, 0, 1],
                          [1, 1, 0, 1, 1],
                          [1, 1, 1, 1, 1],
                          [1, 1, 0, 1, 1],
                          [1, 0, 0, 0, 1]], dtype=np.uint8)

another_good  = np.array([[0, 0, 0, 0, 0],
                          [0, 0, 0, 0, 0],
                          [0, 0, 0, 0, 0],
                          [0, 0, 0, 0, 0],
                          [0, 0, 0, 0, 0]], dtype=np.uint8)

footprint = np.array([[1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1]], dtype=np.uint8)

Outputs (incorrect or not as expected): Outputs



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source