show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次研究了面向传统终端的图片生成效果
- 80 * 24 字符
- 640 * 480 像素
- 其中
- 字体、颜色都可以进行修改
- 除了终端显示器之外
- 可以让鼠标键盘参与图像生成吗?🤡
import cv2 as cv
import numpy as np
img = np.zeros((200,200))
def draw_circle(event,x,y,flags,param):
if event == cv.EVENT_LBUTTONDOWN:
cv.circle(img,(x,y),20,255,-1)
cv.namedWindow("img")
cv.setMouseCallback("img",draw_circle)
while True:
cv.imshow("img",img)
n = cv.waitKey(5)
if n == ord("q"):
break
elif n == ord("s"):
cv.imwrite("res.jpg",img)
print("pictrure saved!")
cv.destroyAllWindows()
- 鼠标右键落下的时候
- 就会画一个圆
- 鼠标移动可以有事件响应函数吗?
import cv2
import numpy as np
def mouse_callback(event, x, y, flags, param):
if event == cv2.EVENT_MOUSEMOVE:
print("Current position: ({}, {})".format(x, y))
global image
image = cv2.line(image,(200,200),(x,y),(255,0,0),5)
# 创建一个空白画布作为输入图像
image = np.zeros((400, 400, 3), dtype=np.uint8)
# 设置鼠标回调函数
cv2.namedWindow('Image')
cv2.setMouseCallback('Image', mouse_callback)
while True:
# 将图像显示到窗口中
cv2.imshow('Image', image)
# 按下ESC键时退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 关闭所有打开的窗口
cv2.destroyAllWindows()
- 效果
- 可以做出点互动艺术吗?
import cv2
import numpy as np
def mouse_callback(event, x, y, flags, param):
if event == cv2.EVENT_MOUSEMOVE:
print("Current position: ({}, {})".format(x, y))
global image
image = np.zeros((400, 400, 3), dtype=np.uint8)
ix = 400 - x
iy = 400 - y
image = cv2.rectangle(image,(x,200,y,y),(0,0,255),5)
image = cv2.circle(image,(ix,200),y,(0,255,0),5)
# 创建一个空白画布作为输入图像
image = np.zeros((400, 400, 3), dtype=np.uint8)
# 设置鼠标回调函数
cv2.namedWindow('Image')
cv2.setMouseCallback('Image', mouse_callback)
while True:
# 将图像显示到窗口中
cv2.imshow('Image', image)
# 按下ESC键时退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 关闭所有打开的窗口
cv2.destroyAllWindows()
- 效果
-
可以设置键盘与生成图像的效果吗?
-
可以再设置鼠标抬起时的效果吗?
import numpy as np
import cv2
drawing=False
mode=True
ix,iy=-1,-1
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
ix,iy=x,y
elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
if drawing==True:
if mode==True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),3,(0,0,255),-1)
elif event==cv2.EVENT_LBUTTONUP:
drawing==False
img=np.zeros((512,512,3),np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
k=cv2.waitKey(1)&0xff
if k==ord('m'):
mode=not mode
elif k==27:
break
- 效果
- 可以做一个画笔吗?
import numpy as np
import cv2
b_drawing=False
def draw_circle(event,x,y,flags,param):
global b_drawing
if event==cv2.EVENT_LBUTTONDOWN:
b_drawing=True
elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
if b_drawing==True:
cv2.circle(img,(x,y),10,(0,0,0),-1)
elif event==cv2.EVENT_LBUTTONUP:
b_drawing==False
img=np.ones((512,512,3),np.uint8)*255
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
k=cv2.waitKey(1)&0xff
if k==ord('m'):
mode=not mode
elif k==27:
break
- 可以调节笔刷的大小吗?
import numpy as np
import cv2
b_drawing=False
radius = 10
def draw_circle(event,x,y,flags,param):
global b_drawing,radius
if event==cv2.EVENT_LBUTTONDOWN:
b_drawing=True
elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
if b_drawing==True:
cv2.circle(img,(x,y),radius,(0,0,0),-1)
elif event==cv2.EVENT_LBUTTONUP:
b_drawing==False
def set_radius(value):
global radius
radius = value
print(value)
img=np.ones((512,512,3),np.uint8)*255
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
cv2.createTrackbar('T', 'image', 0, 100, set_radius)
while(1):
cv2.imshow('image',img)
k=cv2.waitKey(1)&0xff
if k==ord('m'):
mode=not mode
elif k==27:
break
import numpy as np
import cv2
import math
b_drawing=False
radius = 10
ix,iy = -1,-1
distance = 0
def draw_circle(event,x,y,flags,param):
global b_drawing,radius,distance,ix,iy
if event==cv2.EVENT_LBUTTONDOWN:
b_drawing=True
elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
if b_drawing==True:
if (((x-ix)**2 + (y-iy)**2)**0.5 / radius) >= distance/100:
cv2.circle(img,(x,y),radius,(128,128,128),-1)
ix = x
iy = y
elif event==cv2.EVENT_LBUTTONUP:
b_drawing==False
def set_distance(value):
global distance
distance = value
print(value)
def set_radius(value):
global radius
radius = value
print(value)
img=np.ones((512,512,3),np.uint8)*255
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
cv2.createTrackbar('radius', 'image', 0, 100, set_radius)
cv2.createTrackbar('distance', 'image', 0, 100, set_distance)
while(True):
cv2.imshow('image',img)
k=cv2.waitKey(1)&0xff
if k==ord('m'):
mode=not mode
elif k==27:
break
- 这次研究了鼠标的各种互动效果
- 键盘可以有什么互动效果吗??🤔
- 下次再说👋