gpt4 book ai didi

python - 如何使矩形透明?

转载 作者:行者123 更新时间:2023-12-02 17:17:59 25 4
gpt4 key购买 nike

所以我正在使用opencv,我想做一种选择工具,但是问题是不能使矩形透明。这是代码:

import numpy as np
import cv2 as cv
drawing = False

def draw_rec(event,x,y,flags,param):
global ix,iy,drawing
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv.EVENT_LBUTTONUP:
drawing = False
cv.rectangle(img,(ix,iy),(x,y),(0,0,0),-1)
elif event == cv.EVENT_MOUSEMOVE:
if drawing == True:
cv.rectangle(img, (ix, iy), (x, y), (0, 255, 0), 5)
img = cv.imread('baboon.jpg', -1)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_rec)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
cv.destroyAllWindows()

最佳答案

代码中的第一个错误是:

elif event == cv.EVENT_LBUTTONUP:
drawing = False
cv.rectangle(img,(ix,iy),(x,y),(0,0,0),-1)
-1参数表示填充矩形。 source如果将-1更改为1:
enter image description here
从我的 Angular 来看,结果并不令人满意。多矩形显示是由 mouse_movement引起的。
elif event == cv.EVENT_MOUSEMOVE:
if drawing == True:
cv.rectangle(img, (ix, iy), (x, y), (0, 255, 0), 5)
每次鼠标移动时,都会绘制矩形。我认为最好在鼠标移动结束后进行绘制:
enter image description here
码:

import numpy as np
import cv2 as cv
drawing = False

def draw_rec(event,x,y,flags,param):
global ix,iy,drawing
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv.EVENT_LBUTTONUP:
drawing = False
cv.rectangle(img,(ix,iy),(x,y),(0,255,0),5)


img = cv.imread('27BR1.jpg', -1)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_rec)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
cv.destroyAllWindows()

关于python - 如何使矩形透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63752193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com