提交 19459fba 编写于 作者: F feilong

add io and gui

上级 87003ee3
......@@ -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
{
"type": "code_options",
"author": "huanhuilong",
"source": "img_buffer_convert.md",
"notebook_enable": true
}
\ No newline at end of file
# 甲壳虫的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
```
# -*- 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()
{
"type": "code_options",
"author": "huanhuilong",
"source": "img_read_write.md",
"notebook_enable": true
}
\ No newline at end of file
# 甲壳虫乐队
一只甲壳虫想组个乐队,但是临时找不到队友。请使用 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()
```
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()
{
"type": "code_options",
"author": "huanhuilong",
"source": "video_read_write.md",
"notebook_enable": true
}
\ No newline at end of file
# 早起的鸟儿有虫吃
早起的鸟儿不但有虫吃,还可以照镜子。请你帮助鸟儿们跟镜像合影。
这是鸟儿的视频:
<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)
)
```
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.
先完成此消息的编辑!
想要评论请 注册