-1

enter image description hereI'm trying to find the distance between two irregular edges in a Binary image at various intervals. I want to find the distance/ length of the red line (the distance between the black edge at the bottom to the black peak edge at the top) Not sure which method to use for this. I also did edge detection. But not sure how to find the distance between the edges (the green lines) It would be great if I can also trace the green lines and draw a line (not straight) on top of it. I'm trying to do this all with OpenCV and Scipy packages. Please let me know if I should approach this problem in any other way. ANd In my case, I can't manually detect coordinates because I have a huge data set to work on a daily basis.

topLeft = count(mask[0])
bottomLeft = count(mask[h])
# to shadow and hide the old left line
mask = line(mask, (topLeft, 0), (bottomLeft, h), (0, 0, 0), 80)

topRight = count(mask[0])
bottomRight = count(mask[h])
# to shadow and hide the old right line
mask = line(mask, (topRight, 0), (bottomRight, h), (0, 0, 0), 80)

mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

# to draw new clean left line
mask = line(mask, (topLeft, 0), (bottomLeft, h), (128, 0, 255), 25)
# to draw new clean right line
mask = line(mask, (topRight, 0), (bottomRight, h), (128, 0, 255), 25)

a = center(topLeft, 0, bottomLeft, h)
b = center(topRight, 0, bottomRight, h)
mask = line(mask, a, b, (128, 0, 255), 25)

cv2.imwrite("out2.jpg", mask)

Blockquote

The ones I want to find the distance enter image description here

The Actual image enter image description here

The Edge detected image with a red line which I drew enter image description here

  • 1
    You could flood fill the bottom with black and the middle with white. Then just use numpy count_nonzero() to count the number of white pixels in each column. See https://numpy.org/doc/stable/reference/generated/numpy.count_nonzero.html – fmw42 Mar 04 '22 at 18:44
  • Does your image actually include 17 acres of white-space all around it? – Mark Setchell Mar 04 '22 at 18:59
  • No, My image doesn't have any white space around. Just the rectangle there is my sample image. – Starcode1619 Mar 04 '22 at 21:23
  • @fmw42 But I missed mentioning this thing, there are some white dots in between the black space like some extra edges and noise/ – Starcode1619 Mar 04 '22 at 21:32
  • @fmw42 No I missed mentioning this thing. There are some white noise edges in between in the black space. – Starcode1619 Mar 04 '22 at 21:34
  • 1
    Use morphology to clean them up. – fmw42 Mar 04 '22 at 22:01
  • 1
    Perhaps you could post an original mask image without the red lines at full resolution. – fmw42 Mar 04 '22 at 22:02
  • I'm posting the image now. clearly. I want to find the orange color distance in between the bottom most to the top edge @fmw42 – Starcode1619 Mar 10 '22 at 16:18
  • @fmw42 I just posted the images clearly. Can you check and tell me whta methods I can use for this distance finding. Like if i should go for ML or DL stuff using all the imgaes? – Starcode1619 Mar 10 '22 at 16:51
  • You could use numpy to find the max and min y coordinates of the black edge pixels along every x value. – fmw42 Mar 10 '22 at 17:01

1 Answers1

1

What you call the distance seems to be the vertical distance.

For the vertical distance, it suffices to follow a vertical from the top and detect the two transitions from black to white.

If you are looking for an oblique distance (or is your drawing jst inaccurate), you have to draw Bresenham lines instead of pure verticals.

  • Thanks a lot @Yves Daoust .Now, i have a edge detected image which is predominantly black and the edges are in white. I want to find the distance between the lower most white pixel to the upper most white pixel vertically. Straight line distance calculation will do. Can't calculate co-ordinates for a single image, because I'm looking to implement this for multiple images. Please let me know if there is any other way. – Starcode1619 Mar 09 '22 at 15:33
  • @Starcode1619: "Can't calculate co-ordinates for a single image, because I'm looking to implement this for multiple images": sorry, I can't make any sense of this. –  Mar 09 '22 at 15:43
  • The thing is I cannot do this by manually calculating co-ordinates of the edges, because I have to implement this on huge number of images. Does it make it clear? – Starcode1619 Mar 09 '22 at 16:53
  • That was clear from the beginning, I never imagined that you would do that by hand. What do you miss in my answer ? –  Mar 09 '22 at 17:27
  • Now I have updated my question with the new actual images. Please let me know what else can be done. – Starcode1619 Mar 10 '22 at 16:34
  • @Starcode1619: my answer is still valid, though with reverse polarity. It seems that you are only concerned with local maxima, but the question does not say it. And the image is more complex. Please formulate a complete question with representative images, otherwise you make us waste our time. –  Mar 10 '22 at 16:56