2

This is the actual Image enter image description here

I want to find the distance between 2 irregular edges in a binary image. That is I have marked with red on the below image. My idea was to draw a colored line (say red) on both the edges and then calculate the distance between them at 10 equal intervals (yellow-colored marking)

enter image description here

I cropped the image into two. Say the top half is

enter image description here

Bottom half is

enter image description here

I want to draw the two red lines or just find the distance between those two somehow. I have been using OpenCV and PILLOW for a lot of steps.

there are also image instances where at few columns there are no black pixels in the top image. How do i calculate the distance of the black pixels only from the top? Just the top image.

2
  • 1
    This question has been asked before with the same images by you or someone else. Did you search this forum? One potential approach --- for the top image, measure the distance from the top in the white to the black and subtract from the height for D1. For the bottom half, measure the distance from the bottom to the white in the black and subtract from the height for D2 (you can flip the image over vertically if need be). Then just add D1+D2 to get the required distance.
    – fmw42
    Mar 22, 2022 at 15:40
  • That was me. That previous one seemed a confusion. So uploaded new ones. Thanks a lot for your answer! i tried "Bottom_edge=[] for col in range(img_bottom.shape[1]): edge=max(np.where(img_bottom>0)[0]) Bottom_edge.append(edge) but this seems a slower. How can i find the distance ? any other approaches? Mar 22, 2022 at 15:58

1 Answer 1

3

Here is one possible approach. I will leave the details to you. But the idea is to use Numpy argmax (or argmin as appropriate) to get the index of the first white after all black in each column. First I flip the image vertically so that the black is at the top. Numpy argmax, seems to find the first white value along the column.

Input (bottom image):

enter image description here

import numpy as np
import cv2

# read image
img = cv2.imread("img.png")

# Note that there is a 2 pixel tall white line at the bottom
# Note that there is columns of black at the left and right sides

# convert to grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold
img = cv2.threshold(img,0,255,cv2.THRESH_BINARY)[1]

# flip vertically
img = cv2.flip(img, 0)

# remove 2 pixels at the top
img = img[2:, :]

# add one pixel row of white at bottom
img = cv2.copyMakeBorder(img, 0, 2, 0, 0, cv2.BORDER_CONSTANT, value=255)

# find max along each column
max = np.argmax(img, axis=0)
print(max.shape)

print(max)

cv2.imshow('flipped',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is the output:

(709,)
[112 112 112 112  32  32  32  32  32  32  32  32  32  32  32  32  32  32
  31  31  31  31  31  31  31  31  31  31  30  30  30  30  30  30  30  30
  30  30  30  30  30  30  30  30  30  30  30  31  31  31  31  31  31  31
  31  31  31  31  31  31  31  31  31  30  30  30  30  30  30  30  30  30
  30  30  30  31  31  32  32  33  33  34  35  35  36  36  36  36  36  36
  36  36  36  36  36  36  36  36  37  37  38  38  39  39  39  40  40  40
  41  41  43  43  44  45  45  46  46  46  46  46  46  46  46  46  46  46
  46  46  46  46  46  46  46  46  46  46  46  46  46  45  45  45  45  44
  44  44  44  44  44  44  44  44  44  44  44  44  43  43  43  43  43  43
  43  43  43  43  43  43  43  44  44  44  44  44  44  44  44  43  43  43
  42  42  42  42  42  42  42  42  42  42  41  41  40  40  40  40  40  40
  40  40  40  40  40  40  40  40  40  40  40  40  39  39  39  39  39  39
  39  39  39  39  39  39  39  39  39  39  39  39  38  38  37  37  37  37
  37  37  37  36  36  36  36  36  35  35  35  34  34  34  34  34  34  34
  34  34  33  33  32  32  32  32  32  32  32  32  32  32  32  32  32  32
  33  33  34  34  35  35  36  37  37  37  37  38  38  38  38  38  38  38
  38  38  38  38  38  38  38  39  39  40  40  40  40  40  39  39  39  39
  39  39  38  38  38  37  37  37  37  37  37  37  37  37  37  37  37  37
  37  38  38  38  39  40  40  41  41  41  39  39  38  37  37  37  37  37
  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  38  38
  38  39  39  39  39  39  39  39  39  41  41  41  41  41  41  41  41  41
  41  41  41  41  41  41  41  41  41  41  41  41  41  41  41  41  41  41
  40  40  40  40  40  40  40  40  40  40  39  39  38  38  37  37  37  37
  36  36  36  36  36  36  35  35  34  34  34  33  33  33  32  31  31  31
  31  31  31  31  31  31  31  31  31  31  31  31  31  31  31  31  31  31
  31  31  31  31  31  31  31  31  30  30  29  29  29  28  28  28  28  27
  27  26  26  26  26  26  26  26  26  26  26  26  26  26  26  27  27  27
  27  27  27  27  27  27  28  28  28  28  28  28  28  28  28  28  28  28
  28  28  28  28  28  28  28  28  28  28  28  28  28  28  29  29  29  29
  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29
  29  28  28  27  27  27  27  27  27  27  27  27  27  27  27  27  28  28
  28  28  29  29  30  30  30  31  31  32  32  33  33  34  34  35  35  36
  36  36  37  37  37  38  38  39  39  39  39  39  39  39  39  39  39  39
  39  38  38  37  37  37  37  37  37  36  36  35  35  35  35  35  35  35
  35  35  35  35  35  35  35  35  35  35  35  36  36  36  36  36  36  36
  36  36  36  36  36  36  37  37  37  37  37  37  37  37  37  37  37  37
  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37
  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37  37
  37  37  37  37  37  37  37  37  37  37  37  37  37  38  38  38  39  39
  39  39  39  40 112 112 112]

For the top use argmin or invert the polarity using img=255-img and then use argmax.

6
  • I have few doubts here. why do we add one pixel row of white to the bottom? and how do we know if it is looking for the white pixel? Mar 22, 2022 at 17:39
  • Same can you help for the top image. it is a bit complicated because, there are places without black pixels where it is calculating the entire height diatnce. How can i eleiminate places without the black pixels in the top image Mar 22, 2022 at 17:41
  • 1
    For the top use argmin or invert the polarity using img=255-img and then use argmax.
    – fmw42
    Mar 22, 2022 at 17:59
  • Thanks a lot! But there are also instances where the top image doesn't have any black pixels in few columns. What can i do in such cases? any ideas? Mar 22, 2022 at 20:33
  • 1
    Put a row of black at the bottom of the top image, similar to what I did for the bottom image using cv2.copyMakeBorder().
    – fmw42
    Mar 22, 2022 at 23:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.