Remove background of Plant Village dataset

Hello,
I’m trying to remove the background from a dataset of leaves, during my research I found out this paper that describes the steps to extract the plant with respect to the background as below.


However, I cannot understand how to check if the pixel of If(x,y) is at the edge of Id.
So far, I have implement the previous steps:

import numpy as np
import cv2
import skimage.morphology as morph

image = cv2.imread(filename)
height, width, _ = image.shape
Ihsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
Is = Ihsv[:,:,2]

#Apply Spacial Gaussian Filter
trunc_val = 2
sigma_val = 2
k_size = int(sigma_val * trunc_val)
Ifg = cv2.GaussianBlur(Is, (k_size,k_size), sigma_val)
A = np.zeros((height, width), dtype=np.uint8)
    for h in range(0, height):
        for w in range(0, width):
            if Ifg[h, w] > 0.23:
                A[h, w] = 1

#Apply Morphological Operations
kernelD2 = morph.diamond(2)
kernelD3 = morph.diamond(3)
Ie = cv2.erode(A, kernelD2)
Id = cv2.dilate(Ie, kernelD3)

It would be so nice, if someone can help me.

Best Regards.
Tarcísio

edge pixels:

  1. have a mask/binary image, call it A
  2. erode A, call it B
  3. calculate C = A & ~B (subtraction, with opencv functions, would also work, because they saturate)
  4. you now have the inside edge pixels of any blobs in A

same idea for outside edges, except a little rearranged.

more ersion/dilation if you need the edges to be wider.

Thank you @crackwitz, I will try to implement it!