Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_opencv
提交
f319cb1c
S
skill_tree_opencv
项目概览
CSDN 技术社区
/
skill_tree_opencv
通知
24
Star
9
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f319cb1c
编写于
12月 08, 2021
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add doodle
上级
9dfe922e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
171 addition
and
17 deletion
+171
-17
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.json
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.json
+7
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.md
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.md
+128
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.png
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.png
+0
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.py
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.py
+36
-17
未找到文件。
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.json
浏览文件 @
f319cb1c
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"doodle.md"
,
"notebook_enable"
:
true
}
\ No newline at end of file
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.md
浏览文件 @
f319cb1c
# 矩形涂鸦画板
![](
./doodle.png
)
编写一个矩形涂鸦画板,实现功能:
*
鼠标左键按下拖动绘制矩形,鼠标左键弹起时完成绘制
*
按
`'c'`
键清空画板
*
按
`'ESC'`
键退出
基本框架代码如下:
```
python
import
numpy
as
np
import
cv2
as
cv
from
random
import
randint
class
Painter
:
def
__init__
(
self
)
->
None
:
self
.
mouse_is_pressed
=
False
self
.
last_pos
=
(
-
1
,
-
1
)
self
.
width
=
300
self
.
height
=
512
self
.
img
=
np
.
zeros
((
self
.
width
,
self
.
height
,
3
),
np
.
uint8
)
self
.
window_name
=
'painter'
self
.
color
=
None
def
run
(
self
):
print
(
'画板,拖动鼠标绘制矩形框,按ESC退出,按c键清空画板'
)
cv
.
namedWindow
(
self
.
window_name
)
cv
.
setMouseCallback
(
self
.
window_name
,
lambda
event
,
x
,
y
,
flags
,
param
:
self
.
on_draw
(
event
,
x
,
y
,
flags
,
param
)
)
while
True
:
cv
.
imshow
(
self
.
window_name
,
self
.
img
)
k
=
cv
.
waitKey
(
1
)
&
0xFF
if
k
==
ord
(
'c'
):
self
.
clean
()
elif
k
==
27
:
break
cv
.
destroyAllWindows
()
def
on_draw
(
self
,
event
,
x
,
y
,
flags
,
param
):
# TODO(You): 请正确实现画板事件响应,完成功能
def
clean
(
self
):
cv
.
rectangle
(
self
.
img
,
(
0
,
0
),
(
self
.
height
,
self
.
width
),
(
0
,
0
,
0
),
-
1
)
def
begin_draw_rectangle
(
self
,
pos1
,
pos2
):
if
self
.
color
is
None
:
self
.
color
=
(
randint
(
0
,
256
),
randint
(
0
,
256
),
randint
(
0
,
256
))
cv
.
rectangle
(
self
.
img
,
pos1
,
pos2
,
self
.
color
,
-
1
)
def
end_draw_rectangle
(
self
,
pos1
,
pos2
):
self
.
color
=
None
if
__name__
==
'__main__'
:
p
=
Painter
()
p
.
run
()
```
以下正确实现鼠标事件,完成画板绘制逻辑的代码是?
## 答案
```
python
pos
=
(
x
,
y
)
if
event
==
cv
.
EVENT_LBUTTONDOWN
:
self
.
mouse_is_pressed
=
True
self
.
last_pos
=
pos
elif
event
==
cv
.
EVENT_MOUSEMOVE
:
if
self
.
mouse_is_pressed
==
True
:
self
.
begin_draw_rectangle
(
self
.
last_pos
,
pos
)
elif
event
==
cv
.
EVENT_LBUTTONUP
:
self
.
end_draw_rectangle
(
self
.
last_pos
,
pos
)
self
.
mouse_is_pressed
=
False
```
## 选项
### 鼠标移动事件里缺乏判断
```
python
pos
=
(
x
,
y
)
if
event
==
cv
.
EVENT_LBUTTONDOWN
:
self
.
mouse_is_pressed
=
True
self
.
last_pos
=
pos
elif
event
==
cv
.
EVENT_MOUSEMOVE
:
self
.
begin_draw_rectangle
(
self
.
last_pos
,
pos
)
elif
event
==
cv
.
EVENT_LBUTTONUP
:
self
.
end_draw_rectangle
(
self
.
last_pos
,
pos
)
self
.
mouse_is_pressed
=
False
```
### 鼠标按下事件里缺乏标记
```
python
pos
=
(
x
,
y
)
if
event
==
cv
.
EVENT_LBUTTONDOWN
:
self
.
last_pos
=
pos
elif
event
==
cv
.
EVENT_MOUSEMOVE
:
if
self
.
mouse_is_pressed
==
True
:
self
.
begin_draw_rectangle
(
self
.
last_pos
,
pos
)
elif
event
==
cv
.
EVENT_LBUTTONUP
:
self
.
end_draw_rectangle
(
self
.
last_pos
,
pos
)
self
.
mouse_is_pressed
=
False
```
### 鼠标按起时忘记清空标记
```
python
pos
=
(
x
,
y
)
if
event
==
cv
.
EVENT_LBUTTONDOWN
:
self
.
mouse_is_pressed
=
True
self
.
last_pos
=
pos
elif
event
==
cv
.
EVENT_MOUSEMOVE
:
if
self
.
mouse_is_pressed
==
True
:
self
.
begin_draw_rectangle
(
self
.
last_pos
,
pos
)
elif
event
==
cv
.
EVENT_LBUTTONUP
:
self
.
end_draw_rectangle
(
self
.
last_pos
,
pos
)
```
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.png
0 → 100644
浏览文件 @
f319cb1c
25.0 KB
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/doodle.py
浏览文件 @
f319cb1c
import
numpy
as
np
import
cv2
as
cv
from
random
import
randint
class
Painter
:
def
__init__
(
self
,
background
,
color
)
->
None
:
def
__init__
(
self
)
->
None
:
self
.
mouse_is_pressed
=
False
self
.
last_pos
=
(
-
1
,
-
1
)
self
.
img
=
np
.
zeros
((
300
,
512
,
3
),
np
.
uint8
)
self
.
background
=
background
self
.
color
=
color
self
.
title
=
'画板,拖动鼠标绘制矩形框,按ESC退出'
self
.
width
=
300
self
.
height
=
512
self
.
img
=
np
.
zeros
((
self
.
width
,
self
.
height
,
3
),
np
.
uint8
)
self
.
window_name
=
'painter'
self
.
color
=
None
def
run
(
self
):
cv
.
namedWindow
(
self
.
window_name
,
title
=
self
.
title
)
cv
.
setMouseCallback
(
self
.
window_name
,
lambda
event
,
x
,
y
,
flags
,
param
:
self
.
draw_circle
(
event
,
x
,
y
,
flags
,
param
))
while
(
1
):
print
(
'画板,拖动鼠标绘制矩形框,按ESC退出,按c键清空画板'
)
cv
.
namedWindow
(
self
.
window_name
)
cv
.
setMouseCallback
(
self
.
window_name
,
lambda
event
,
x
,
y
,
flags
,
param
:
self
.
on_draw
(
event
,
x
,
y
,
flags
,
param
)
)
while
True
:
cv
.
imshow
(
self
.
window_name
,
self
.
img
)
k
=
cv
.
waitKey
(
1
)
&
0xFF
if
k
==
27
:
if
k
==
ord
(
'c'
):
self
.
clean
()
elif
k
==
27
:
break
cv
.
destroyAllWindows
()
def
draw_circle
(
self
,
event
,
x
,
y
,
flags
,
param
):
current_
pos
=
(
x
,
y
)
def
on_draw
(
self
,
event
,
x
,
y
,
flags
,
param
):
pos
=
(
x
,
y
)
if
event
==
cv
.
EVENT_LBUTTONDOWN
:
self
.
mouse_is_pressed
=
True
self
.
last_pos
=
current_
pos
self
.
last_pos
=
pos
elif
event
==
cv
.
EVENT_MOUSEMOVE
:
if
self
.
mouse_is_pressed
==
True
:
cv
.
rectangle
(
self
.
img
,
self
.
last_pos
,
current_pos
,
(
0
,
255
,
0
),
-
1
)
self
.
begin_draw_rectangle
(
self
.
last_pos
,
pos
)
elif
event
==
cv
.
EVENT_LBUTTONUP
:
self
.
end_draw_rectangle
(
self
.
last_pos
,
pos
)
self
.
mouse_is_pressed
=
False
cv
.
rectangle
(
self
.
img
,
self
.
last_pos
,
current_pos
,
(
0
,
255
,
0
),
-
1
)
def
clean
(
self
):
cv
.
rectangle
(
self
.
img
,
(
0
,
0
),
(
self
.
height
,
self
.
width
),
(
0
,
0
,
0
),
-
1
)
def
begin_draw_rectangle
(
self
,
pos1
,
pos2
):
if
self
.
color
is
None
:
self
.
color
=
(
randint
(
0
,
256
),
randint
(
0
,
256
),
randint
(
0
,
256
))
cv
.
rectangle
(
self
.
img
,
pos1
,
pos2
,
self
.
color
,
-
1
)
def
end_draw_rectangle
(
self
,
pos1
,
pos2
):
self
.
color
=
None
if
__name__
==
'__main__'
:
p
=
Painter
(
background
=
(
100
,
100
,
100
),
color
=
(
200
,
150
,
50
)
)
p
=
Painter
()
p
.
run
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录