Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_opencv
提交
19459fba
S
skill_tree_opencv
项目概览
CSDN 技术社区
/
skill_tree_opencv
通知
57
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看板
提交
19459fba
编写于
12月 08, 2021
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add io and gui
上级
87003ee3
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
398 addition
and
1 deletion
+398
-1
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds.mp4
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds.mp4
+0
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds_concat.jpg
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds_concat.jpg
+0
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds_concat.mp4
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds_concat.mp4
+0
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/bug.jpg
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/bug.jpg
+0
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/bug_band.jpg
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/bug_band.jpg
+0
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/config.json
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/config.json
+6
-1
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.json
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.json
+7
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.md
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.md
+83
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.py
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.py
+26
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_wirte.json
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_wirte.json
+7
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_write.md
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_write.md
+102
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_write.py
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_write.py
+18
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.json
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.json
+7
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.md
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.md
+104
-0
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.py
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.py
+38
-0
未找到文件。
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds.mp4
0 → 100644
浏览文件 @
19459fba
文件已添加
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds_concat.jpg
0 → 100644
浏览文件 @
19459fba
123.0 KB
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/birds_concat.mp4
0 → 100644
浏览文件 @
19459fba
文件已添加
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/bug.jpg
0 → 100644
浏览文件 @
19459fba
206.2 KB
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/bug_band.jpg
0 → 100644
浏览文件 @
19459fba
339.1 KB
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/config.json
浏览文件 @
19459fba
...
...
@@ -2,5 +2,9 @@
"node_id"
:
"opencv-04cf975c42894174bd5619ed44ea5e87"
,
"keywords"
:
[],
"children"
:
[],
"export"
:
[]
"export"
:
[
"img_read_write.json"
,
"video_read_write.json"
,
"img_buffer_convert.json"
]
}
\ No newline at end of file
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.json
0 → 100644
浏览文件 @
19459fba
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"img_buffer_convert.md"
,
"notebook_enable"
:
true
}
\ No newline at end of file
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.md
0 → 100644
浏览文件 @
19459fba
# 甲壳虫的Base64之旅
如下的一只甲壳虫,我们希望把它编码成 Base64,再从Base64解码出来。
![](
./bug.jpg
)
代码框架如下:
```
python
import
numpy
as
np
import
cv2
import
base64
def
img_to_base64
(
img
):
# TODO(You):
def
img_from_base64
(
img_base64
):
# TODO(You):
if
__name__
==
'__main__'
:
img
=
cv2
.
imread
(
'bug.jpg'
)
img_base64
=
img_to_base64
(
img
)
img
=
img_from_base64
(
img_base64
)
cv2
.
imshow
(
'img_decode'
,
img
)
cv2
.
waitKey
()
cv2
.
destroyAllWindows
()
```
以下对两个函数实现正确的是?
## 答案
```
python
def
img_to_base64
(
img
):
return
base64
.
b64encode
(
cv2
.
imencode
(
'.jpg'
,
img
)[
1
]).
decode
()
def
img_from_base64
(
img_base64
):
jpg_original
=
base64
.
b64decode
(
img_base64
)
jpg_as_np
=
np
.
frombuffer
(
jpg_original
,
dtype
=
np
.
uint8
)
img
=
cv2
.
imdecode
(
jpg_as_np
,
flags
=
1
)
return
img
```
## 选项
### A
```
python
def
img_to_base64
(
img
):
return
base64
.
b64encode
(
cv2
.
imencode
(
'.jpg'
,
img
)[
1
])
def
img_from_base64
(
img_base64
):
jpg_original
=
base64
.
b64decode
(
img_base64
)
img
=
cv2
.
imdecode
(
jpg_original
,
flags
=
1
)
return
img
```
### B
```
python
def
img_to_base64
(
img
):
return
base64
.
b64encode
(
cv2
.
imencode
(
'.jpg'
,
img
)).
decode
()
def
img_from_base64
(
img_base64
):
jpg_original
=
base64
.
b64decode
(
img_base64
)
jpg_as_np
=
np
.
frombuffer
(
jpg_original
,
dtype
=
np
.
uint8
)
img
=
cv2
.
imdecode
(
jpg_as_np
,
flags
=
1
)
return
img
```
### C
```
python
def
img_to_base64
(
img
):
return
base64
.
b64encode
(
cv2
.
imencode
(
'.jpg'
,
img
)[
1
]).
decode
()
def
img_from_base64
(
img_base64
):
jpg_original
=
base64
.
b64decode
(
img_base64
)
jpg_as_np
=
np
.
frombuffer
(
jpg_original
)
img
=
cv2
.
imdecode
(
jpg_as_np
,
flags
=
1
)
return
img
```
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_buffer_convert.py
0 → 100644
浏览文件 @
19459fba
# -*- coding: utf-8 -*-
import
numpy
as
np
import
cv2
import
base64
def
img_to_base64
(
img
):
return
base64
.
b64encode
(
cv2
.
imencode
(
'.jpg'
,
img
)[
1
]).
decode
()
def
img_from_base64
(
img_base64
):
jpg_original
=
base64
.
b64decode
(
img_base64
)
jpg_as_np
=
np
.
frombuffer
(
jpg_original
,
dtype
=
np
.
uint8
)
img
=
cv2
.
imdecode
(
jpg_as_np
,
flags
=
1
)
return
img
if
__name__
==
'__main__'
:
img
=
cv2
.
imread
(
'bug.jpg'
)
img_base64
=
img_to_base64
(
img
)
img
=
img_from_base64
(
img_base64
)
cv2
.
imshow
(
'img_decode'
,
img
)
cv2
.
waitKey
()
cv2
.
destroyAllWindows
()
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_wirte.json
0 → 100644
浏览文件 @
19459fba
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"img_read_write.md"
,
"notebook_enable"
:
true
}
\ No newline at end of file
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_write.md
0 → 100644
浏览文件 @
19459fba
# 甲壳虫乐队
一只甲壳虫想组个乐队,但是临时找不到队友。请使用 OpenCV 读取下面的彩色甲壳虫图片
`'bug.jpg'`
,帮助他变身灰色甲壳虫,然后完成组队。
![](
./bug.jpg
)
**显示甲壳虫乐队并写入到 `'bug_band.jpg'`**
:
![](
./bug_band.jpg
)
以下实现正确的是?
## 答案
```
python
import
numpy
as
np
import
cv2
if
__name__
==
'__main__'
:
bug_img
=
cv2
.
imread
(
"bug.jpg"
)
bug_img_gray
=
cv2
.
cvtColor
(
bug_img
,
cv2
.
COLOR_BGR2GRAY
)
bug_img_gray_by_BGR_space
=
cv2
.
cvtColor
(
bug_img_gray
,
cv2
.
COLOR_GRAY2BGR
)
bug_img_concat
=
np
.
concatenate
(
(
bug_img
,
bug_img_gray_by_BGR_space
),
axis
=
1
)
cv2
.
imwrite
(
"bug_band.jpg"
,
bug_img_gray
)
cv2
.
imshow
(
'甲壳虫乐队'
,
bug_img_concat
)
cv2
.
waitKey
(
0
)
cv2
.
destroyAllWindows
()
```
## 选项
### 读写api名字错误
```
python
import
numpy
as
np
import
cv2
if
__name__
==
'__main__'
:
bug_img
=
cv2
.
read
(
"bug.jpg"
)
bug_img_gray
=
cv2
.
cvtColor
(
bug_img
,
cv2
.
COLOR_BGR2GRAY
)
bug_img_gray_by_BGR_space
=
cv2
.
cvtColor
(
bug_img_gray
,
cv2
.
COLOR_GRAY2BGR
)
bug_img_concat
=
np
.
concatenate
(
(
bug_img
,
bug_img_gray_by_BGR_space
),
axis
=
1
)
cv2
.
write
(
"bug_band.jpg"
,
bug_img_gray
)
cv2
.
imshow
(
'甲壳虫乐队'
,
bug_img_concat
)
cv2
.
waitKey
(
0
)
cv2
.
destroyAllWindows
()
```
### 写入参数顺序错误
```
python
import
numpy
as
np
import
cv2
if
__name__
==
'__main__'
:
bug_img
=
cv2
.
imread
(
"bug.jpg"
)
bug_img_gray
=
cv2
.
cvtColor
(
bug_img
,
cv2
.
COLOR_BGR2GRAY
)
bug_img_gray_by_BGR_space
=
cv2
.
cvtColor
(
bug_img_gray
,
cv2
.
COLOR_GRAY2BGR
)
bug_img_concat
=
np
.
concatenate
(
(
bug_img
,
bug_img_gray_by_BGR_space
),
axis
=
1
)
cv2
.
imwrite
(
bug_img_gray
,
"bug_band.jpg"
)
cv2
.
imshow
(
'甲壳虫乐队'
,
bug_img_concat
)
cv2
.
waitKey
(
0
)
cv2
.
destroyAllWindows
()
```
### 不能合并彩色和灰色,尺寸不同
```
python
import
numpy
as
np
import
cv2
if
__name__
==
'__main__'
:
bug_img
=
cv2
.
imread
(
"bug.jpg"
)
bug_img_gray
=
cv2
.
cvtColor
(
bug_img
,
cv2
.
COLOR_BGR2GRAY
)
bug_img_concat
=
np
.
concatenate
(
(
bug_img
,
bug_img_gray
),
axis
=
1
)
cv2
.
imwrite
(
"bug_img_concat.jpg"
,
bug_img_concat
)
cv2
.
imshow
(
'甲壳虫乐队'
,
bug_img_gray
)
cv2
.
waitKey
(
0
)
cv2
.
destroyAllWindows
()
```
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/img_read_write.py
0 → 100644
浏览文件 @
19459fba
import
numpy
as
np
import
cv2
if
__name__
==
'__main__'
:
bug_img
=
cv2
.
imread
(
"bug.jpg"
)
bug_img_gray
=
cv2
.
cvtColor
(
bug_img
,
cv2
.
COLOR_BGR2GRAY
)
bug_img_gray_by_BGR_space
=
cv2
.
cvtColor
(
bug_img_gray
,
cv2
.
COLOR_GRAY2BGR
)
bug_img_concat
=
np
.
concatenate
(
(
bug_img
,
bug_img_gray_by_BGR_space
),
axis
=
1
)
cv2
.
imwrite
(
"bug_band.jpg"
,
bug_img_gray
)
cv2
.
imshow
(
'甲壳虫乐队'
,
bug_img_concat
)
cv2
.
waitKey
(
0
)
cv2
.
destroyAllWindows
()
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.json
0 → 100644
浏览文件 @
19459fba
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"video_read_write.md"
,
"notebook_enable"
:
true
}
\ No newline at end of file
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.md
0 → 100644
浏览文件 @
19459fba
# 早起的鸟儿有虫吃
早起的鸟儿不但有虫吃,还可以照镜子。请你帮助鸟儿们跟镜像合影。
这是鸟儿的视频:
<object
width=
"500"
height=
"300"
data=
"./birds.mp4"
></object>
这是合影的效果:
<object
width=
"500"
height=
"300"
data=
"./birds_concat.mp4"
></object>
基本的实现代码如下:
```
python
import
numpy
as
np
import
cv2
if
__name__
==
'__main__'
:
cap
=
cv2
.
VideoCapture
(
'birds.mp4'
)
out
=
...
# TODO(You): 请在此正确创建待保存的目标mp4文件`out`
while
(
cap
.
isOpened
()):
ret
,
bird_frame
=
cap
.
read
()
if
bird_frame
is
None
:
break
bird_flip_frame
=
cv2
.
flip
(
bird_frame
,
0
)
bird_concat_frame
=
np
.
concatenate
(
(
bird_frame
,
bird_flip_frame
),
axis
=
1
)
cv2
.
imshow
(
'bird_concat_frame'
,
bird_concat_frame
)
out
.
write
(
bird_concat_frame
)
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
break
out
.
release
()
cap
.
release
()
cv2
.
destroyAllWindows
()
```
以下正确创建了输出mp4文件
`out`
变量的是?
## 答案
```
python
width
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
))
height
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
))
fps
=
cap
.
get
(
cv2
.
CAP_PROP_FPS
)
out
=
cv2
.
VideoWriter
(
'birds_concat.mp4'
,
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
),
fps
,
(
width
*
2
,
height
)
)
```
## 选项
### 宽度不对
```
python
width
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
))
height
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
))
fps
=
cap
.
get
(
cv2
.
CAP_PROP_FPS
)
out
=
cv2
.
VideoWriter
(
'birds_concat.mp4'
,
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
),
fps
,
(
width
,
height
)
)
```
### 格式不对
```
python
width
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
))
height
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
))
fps
=
cap
.
get
(
cv2
.
CAP_PROP_FPS
)
out
=
cv2
.
VideoWriter
(
'birds_concat.mp4'
,
cv2
.
VideoWriter_fourcc
(
*
'MP4V'
),
fps
,
(
width
,
height
)
)
```
### 宽度和长度类型不对
```
python
width
=
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
)
height
=
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
)
fps
=
cap
.
get
(
cv2
.
CAP_PROP_FPS
)
out
=
cv2
.
VideoWriter
(
'birds_concat.mp4'
,
cv2
.
VideoWriter_fourcc
(
*
'MP4V'
),
fps
,
(
width
,
height
)
)
```
data/1.OpenCV初阶/1.OpenCV基础/4.IO与GUI/video_read_write.py
0 → 100644
浏览文件 @
19459fba
import
numpy
as
np
import
cv2
if
__name__
==
'__main__'
:
cap
=
cv2
.
VideoCapture
(
'birds.mp4'
)
width
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
))
height
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
))
fps
=
cap
.
get
(
cv2
.
CAP_PROP_FPS
)
out
=
cv2
.
VideoWriter
(
'birds_concat.mp4'
,
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
),
fps
,
(
width
*
2
,
height
)
)
while
(
cap
.
isOpened
()):
ret
,
bird_frame
=
cap
.
read
()
if
bird_frame
is
None
:
break
bird_flip_frame
=
cv2
.
flip
(
bird_frame
,
0
)
bird_concat_frame
=
np
.
concatenate
(
(
bird_frame
,
bird_flip_frame
),
axis
=
1
)
cv2
.
imshow
(
'bird_concat_frame'
,
bird_concat_frame
)
out
.
write
(
bird_concat_frame
)
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
break
out
.
release
()
cap
.
release
()
cv2
.
destroyAllWindows
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录