How to plot the shortest distance between two lines passing through an arbitrary point using OpenCV in Python?

553 views Asked by At

I have a picture with lines drawn on it.

And, I want to draw a straight line with the shortest distance between two lines passing through an arbitrary point. with Python OpenCV

This is a code that can extract green from the original image and draw dots with mouse from the thinned image.

import numpy as np
import cv2
def on_mouse(event, x,y,flags,param):
   if event == cv2.EVENT_LBUTTONDOWN:
      cv2.line(img,(x,y),(x,y),(255,0,0),2)
      cv2.imshow('thinned',thinned)

def colorPickandThinning(img):
   height, width = img.shape[:2] 

   img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 
 
   lower_green = (40,60,0) 
   upper_green = (80, 255, 255) 
   img_mask = cv2.inRange(img_hsv, lower_green, upper_green)

   img[:, :, 0] = 0
   img[:, :, 2] = 0
   img[:, :, 1] = img_mask
   img_temp = img

   # filtering image nosiecancel
   noisecancel = cv2.fastNlMeansDenoisingColored(img_temp,None,49,57,7,21)
   cv2.imshow('noise',noisecancel)

   #thinning
   thinned = cv2.ximgproc.thinning(cv2.cvtColor(noisecancel,cv2.COLOR_RGB2GRAY))

   return thinned

img = cv2.imread('test_img.jpg')
thinned = colorPickandThinning(img)
cv2.imshow('thinned',thinned)
cv2.setMouseCallback('thinned',on_mouse,thinned)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is a result picture and I drew a dot with the mouse.

enter image description here

I want to draw like this picture.

enter image description here

0

There are 0 answers