如何删除Python中图像上绘制的线条?

dan*_*chy 5 python opencv

我在现有图像的顶部使用鼠标单击(每次鼠标单击都是一个连接的点)在 python(带有 opencv 包)中绘制了几条线,您可以将其视为允许用户在图像上选择某些内容。

我如何允许用户通过单击鼠标右键来删除图像上的最后一个点?这是我当前的代码:

import numpy
import cv2

points = []

def draw_point(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(image, (x,y), 1, (255,0,0),-1)
        points.append((x,y))
        pts = numpy.array(points, numpy.int32)
        cv2.polylines(image,[pts],False,(255,0,0))
    elif event == cv2.EVENT_RBUTTONDOWN:
        # HOW TO DELETE?
        del points[-1]
        pts = numpy.array(points, numpy.int32)
        cv2.polylines(image,[pts],True,(255,0,0))



image = cv2.imread('simple_tattoo.jpg', cv2.IMREAD_UNCHANGED)

cv2.namedWindow('example', cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback('example', draw_point)


while(1):
   cv2.imshow('example',image)

   if cv2.waitKey(20) & 0xFF == 27:
        break

cv2.destroyAllWindows()

print (points)
Run Code Online (Sandbox Code Playgroud)

是否有更简单的方法在图像上绘制线条(任何东西)?

Fra*_*man -1

因此,如果其他人遇到此问题,保持 while 循环来刷新 img 也很重要 - 没有它就无法工作

def click_event(event, x, y, flags, param):
    global img 
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x,y)
        cv2.circle(img, (x, y), 10, (0, 0, 255), -1)
        cv2.imshow('image', img)
    if event == cv2.EVENT_RBUTTONDBLCLK:
        img = cv2.imread(img_path)
        print("cleaned")
        cv2.imshow('image', img)

img_path = "my_img.jpg"        
global img        
img = cv2.imread(img_path)        

while(1):
    cv2.setMouseCallback('image', click_event)
    cv2.imshow('image', img)
    k=cv2.waitKey(1) & 0xFF
    if k==27: #Escape KEY
        break

    cv2.imshow('image', img)

cv2.destroyAllWindows()    
Run Code Online (Sandbox Code Playgroud)