我正在尝试使用opencv在python中用pycharm编写程序。使用鼠标功能删除图像时出现问题。
我尝试使用鼠标移动功能仅在单击鼠标左键时释放图像,而在释放左键时,橡皮擦停止。但是在输出屏幕上没有任何动作
import cv2
screen="Drawing"
img=cv2.imread("12.jpg")
cv2.namedWindow(screen)
eraser = False x_start,y_start,x_end,y_end = 0、0、0、0
def draw_circle(event,x,y,flags,param):
if (event==cv2.EVENT_LBUTTONDOWN):
x_start, y_start, x_end, y_end = x, y, x, y
eraser=True
elif (event==cv2.EVENT_MOUSEHWHEEL):
if eraser==True:
x_end, y_end = x, y
elif event == cv2.EVENT_LBUTTONUP:
x_end, y_end = x, y
eraser = False
cv2.setMouseCallback(screen,draw_circle)
while True:
i = img.copy()
if not eraser:
cv2.imshow("image", img)
elif eraser:
cv2.circle(img, (x, y), 20, (255, 255, 255), -1)
cv2.imshow(screen,img)
if cv2.waitKey(1)==13:
break
cv2.destroyAllWindows()
程序显示图像,但是我无法通过单击鼠标按钮来删除图像
答案 0 :(得分:0)
如@ api55所述,eraser
需要声明为全局变量。但是您要“擦除”的圆的x和y坐标也是如此。您当前的代码为此使用了错误的变量,并且也永远不会更新它们。这就是为什么橡皮擦不起作用的原因。
通过更改代码,您还可以使用更少的变量并且没有while循环来进行操作,从而提高效率。我可以自由地重构代码并实现橡皮擦大小。
import cv2
screen="Drawing"
img=cv2.imread("12.jpg")
cv2.namedWindow(screen)
eraser=False
radius = 20
def draw_circle(x,y):
# 'erase' circle
cv2.circle(img, ( x, y), radius, (255, 255, 255), -1)
cv2.imshow(screen,img)
def handleMouseEvent(event,x,y,flags,param):
global eraser , radius
if (event==cv2.EVENT_MOUSEMOVE):
# update eraser position
if eraser==True:
draw_circle(x,y)
elif (event==cv2.EVENT_MOUSEWHEEL):
# change eraser radius
if flags > 0:
radius += 5
else:
# prevent issues with < 0
if radius > 10:
radius -= 5
elif event == cv2.EVENT_LBUTTONUP:
# stop erasing
eraser = False
elif (event==cv2.EVENT_LBUTTONDOWN):
# start erasing
eraser=True
draw_circle(x,y)
cv2.setMouseCallback(screen,handleMouseEvent)
# show initial image
cv2.imshow(screen,img)
cv2.waitKey(0)
cv2.destroyAllWindows()