0

我有一张画有线条的图片。

而且,我想画一条通过任意点的两条线之间距离最短的直线。使用Python OpenCV

这是一个可以从原始图像中提取绿色并用鼠标从细化图像中绘制点的代码。

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()

这是一张结果图,我用鼠标画了一个点。

在此处输入图像描述

我想画这样的图。

在此处输入图像描述

4

0 回答 0