Skip to content

Commit d1dacf7

Browse files
committed
update 133-135(Colorization, ENet, Style-transfer
1 parent 056cd2b commit d1dacf7

File tree

18 files changed

+247
-1
lines changed

18 files changed

+247
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,15 @@ code_128 | [DNN- Call the Detection Model of Tensorflow](python/code_128) |
170170
code_129 | [DNN- Call the Openpose Implementation Attitude Assessment](python/code_129) | ✔️
171171
code_130 | [DNN- Call YOLO Object Detection Network](python/code_130) | ✔️
172172
code_131 | [DNN- YOLOv3-tiny Real-time Object Detection](python/code_131) | ✔️
173-
code_132 | [DNN- Single and Multiple Image Detection](python/code_132) | ✔️
173+
code_132 | [DNN- Single and Multiple Image Detection](python/code_132) | ✔️
174+
code_133 | [DNN- Colorful Image Colorization ](python/code_133) | ✔️
175+
code_134 | [DNN- ENet Image Segmentation](python/code_134) | ✔️
176+
code_135 | [DNN- Real-time Fast Image Style Transfer](python/code_135) | ✔️
177+
178+
---
179+
180+
### Appendix
181+
182+
⛳️ The weight can be download from Google Driver:
183+
184+
🌱 [Weight for DNN](https://drive.google.com/drive/folders/1mg6VXpkvEmyL1scaelX5FnW8uw1gk9iq?usp=sharing)

README_CN.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,14 @@ code_129 | [DNN- 调用openpose模型实现姿态评估](python/code_129) |
170170
code_130 | [DNN- 调用YOLO对象检测网络](python/code_130) | ✔️
171171
code_131 | [DNN- YOLOv3-tiny版本实时对象检测](python/code_131) | ✔️
172172
code_132 | [DNN- 单张与多张图像的推断](python/code_132) | ✔️
173+
code_133 | [DNN- 图像颜色化模型使用 ](python/code_133) | ✔️
174+
code_134 | [DNN- ENet实现图像分割](python/code_134) | ✔️
175+
code_135 | [DNN- 实时快速的图像风格迁移](python/code_135) | ✔️
176+
177+
---
178+
179+
### 附录
180+
181+
⛳️ DNN模块的一些模型下载可以从下面的谷歌云中获取:
182+
183+
🌱 [Weight for DNN](https://drive.google.com/drive/folders/1mg6VXpkvEmyL1scaelX5FnW8uw1gk9iq?usp=sharing)

python/code_133/2.jpg

23.6 KB
Loading

python/code_133/2color.jpg

63.9 KB
Loading

python/code_133/4.jpg

114 KB
Loading

python/code_133/4color.jpg

131 KB
Loading

python/code_133/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## OpenCV DNN 图像颜色化模型使用
2+
3+
OpenCV DNN在4.0还支持灰度图像的彩色化模型,是根据2016年ECCV的论文而来,基于卷积神经网络模型,通过对Lab色彩空间进行量化分割,映射到最终的CNN输出结果,最后转换为RGB彩色图像。
4+
5+
相关论文详见:
6+
7+
Arxiv: [https://arxiv.org/pdf/1603.08511.pdf](https://arxiv.org/pdf/1603.08511.pdf)
8+
9+
OpenCV DNN使用该模型时候,除了正常的Caffe模型与配置文件之外,还需要一个Lab的量化表。
10+
11+
原图:
12+
13+
<img src='2.jpg'>
14+
15+
Colorization:
16+
17+
<img src='2color.jpg'>
18+
19+
20+
原图:
21+
22+
<img src='4.jpg'>
23+
24+
Colorization:
25+
26+
<img src='4color.jpg'>

python/code_133/opencv_133.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
import cv2 as cv
3+
4+
W_in = 224
5+
H_in = 224
6+
modelTxt = "../model/color/colorization_deploy_v2.prototxt";
7+
modelBin = "../model/color/colorization_release_v2.caffemodel";
8+
pts_txt = "../model/color/pts_in_hull.npy";
9+
10+
# Select desired model
11+
net = cv.dnn.readNetFromCaffe(modelTxt, modelBin)
12+
pts_in_hull = np.load(pts_txt) # load cluster centers
13+
14+
# populate cluster centers as 1x1 convolution kernel
15+
pts_in_hull = pts_in_hull.transpose().reshape(2, 313, 1, 1)
16+
net.getLayer(net.getLayerId('class8_ab')).blobs = [pts_in_hull.astype(np.float32)]
17+
net.getLayer(net.getLayerId('conv8_313_rh')).blobs = [np.full([1, 313], 2.606, np.float32)]
18+
19+
frame = cv.imread("4.jpg")
20+
h, w = frame.shape[:2]
21+
img_rgb = (frame[:,:,[2, 1, 0]] * 1.0 / 255).astype(np.float32)
22+
23+
img_lab = cv.cvtColor(img_rgb, cv.COLOR_RGB2Lab)
24+
img_l = img_lab[:,:,0] # pull out L channel
25+
(H_orig,W_orig) = img_rgb.shape[:2] # original image size
26+
27+
# resize image to network input size
28+
img_rs = cv.resize(img_rgb, (W_in, H_in))
29+
img_lab_rs = cv.cvtColor(img_rs, cv.COLOR_RGB2Lab)
30+
img_l_rs = img_lab_rs[:,:,0]
31+
img_l_rs -= 50 # subtract 50 for mean-centering
32+
33+
# run network
34+
net.setInput(cv.dnn.blobFromImage(img_l_rs))
35+
ab_dec = net.forward()[0,:,:,:].transpose((1,2,0))
36+
37+
(H_out,W_out) = ab_dec.shape[:2]
38+
ab_dec_us = cv.resize(ab_dec, (W_orig, H_orig))
39+
img_lab_out = np.concatenate((img_l[:,:,np.newaxis],ab_dec_us),axis=2)
40+
41+
img_bgr_out = np.clip(cv.cvtColor(img_lab_out, cv.COLOR_Lab2BGR), 0, 1)
42+
print(img_bgr_out.shape)
43+
frame = cv.resize(frame, (w, h))
44+
cv.imshow('origin', frame)
45+
gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
46+
cv.imshow('gray', gray)
47+
#cv.imwrite('gray.jpg', gray)
48+
# fix 4.0 imshow issue
49+
cv.normalize(img_bgr_out, img_bgr_out, 0, 255, cv.NORM_MINMAX)
50+
cv.imshow('colorized', cv.resize(np.uint8(img_bgr_out), (w, h)))
51+
cv.imwrite('4color.jpg', cv.resize(np.uint8(img_bgr_out), (w, h)))
52+
cv.waitKey(0)
53+
cv.destroyAllWindows()

python/code_134/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## OpenCV DNN ENet实现图像分割
2+
3+
OpenCV DNN支持ENet网络模型的图像分割,这里采用的预先训练的ENet网络:
4+
5+
[GitHub - e-lab/ENet-training](https://github.com/e-lab/ENet-training)
6+
7+
基于Cityscapes数据集的训练预测结果
8+
9+
<img src=street.jpg>
10+
11+
<img src=enet_result.png>

python/code_134/enet_result.png

397 KB
Loading

0 commit comments

Comments
 (0)