Skip to content

Commit da04650

Browse files
authored
Merge pull request #410 from kaustubh-sadekar/master
Uploaded files for the blog - Funny Mirrors Using OpenCV
2 parents 9230f87 + 7cbb4ec commit da04650

File tree

10 files changed

+174
-0
lines changed

10 files changed

+174
-0
lines changed

FunnyMirrors/FunnyMirrorsImages.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import cv2
2+
import numpy as np
3+
import math
4+
from vcam import vcam,meshGen
5+
6+
paths = ["./data/chess.png","./data/im2.jpeg","./data/img3.jpg"]
7+
8+
9+
for mode in range(8):
10+
for i, path in enumerate(paths):
11+
# Reading the input image
12+
img = cv2.imread(path)
13+
img = cv2.resize(img,(300,300))
14+
H,W = img.shape[:2]
15+
16+
# Creating the virtual camera object
17+
c1 = vcam(H=H,W=W)
18+
19+
# Creating the surface object
20+
plane = meshGen(H,W)
21+
22+
# We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y)
23+
24+
if mode == 0:
25+
plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
26+
elif mode == 1:
27+
plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
28+
elif mode == 2:
29+
plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
30+
elif mode == 3:
31+
plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
32+
elif mode == 4:
33+
plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H))
34+
elif mode == 5:
35+
plane.Z -= 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) - 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H))
36+
elif mode == 6:
37+
plane.Z += 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
38+
elif mode == 7:
39+
plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
40+
else:
41+
print("Wrong mode selected")
42+
exit(-1)
43+
44+
# Extracting the generated 3D plane
45+
pts3d = plane.getPlane()
46+
47+
# Projecting (Capturing) the plane in the virtual camera
48+
pts2d = c1.project(pts3d)
49+
50+
# Deriving mapping functions for mesh based warping.
51+
map_x,map_y = c1.getMaps(pts2d)
52+
53+
# Generating the output
54+
output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR)
55+
output = cv2.flip(output,1)
56+
57+
cv2.imshow("Funny Mirror",output)
58+
cv2.imshow("Input and output",np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output)))
59+
# Uncomment following line to save the outputs
60+
# cv2.imwrite("Mirror-effect-%d-image-%d.jpg"%(mode+1,i+1),np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output)))
61+
cv2.waitKey(0)

FunnyMirrors/FunnyMirrorsVideo.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import cv2
2+
import numpy as np
3+
import math
4+
from vcam import vcam,meshGen
5+
import sys
6+
7+
8+
cap = cv2.VideoCapture(sys.argv[1])
9+
ret, img = cap.read()
10+
11+
H,W = img.shape[:2]
12+
fps = 30
13+
14+
# Creating the virtual camera object
15+
c1 = vcam(H=H,W=W)
16+
17+
# Creating the surface object
18+
plane = meshGen(H,W)
19+
20+
mode = int(sys.argv[2])
21+
22+
# We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y)
23+
if mode == 0:
24+
plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
25+
elif mode == 1:
26+
plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
27+
elif mode == 2:
28+
plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
29+
elif mode == 3:
30+
plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
31+
elif mode == 4:
32+
plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H))
33+
elif mode == 5:
34+
plane.Z -= 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) - 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H))
35+
elif mode == 6:
36+
plane.Z += 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
37+
elif mode == 7:
38+
plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
39+
else:
40+
print("Wrong mode selected")
41+
exit(-1)
42+
43+
# Extracting the generated 3D plane
44+
pts3d = plane.getPlane()
45+
46+
# Projecting (Capturing) the plane in the virtual camera
47+
pts2d = c1.project(pts3d)
48+
49+
# Deriving mapping functions for mesh based warping.
50+
map_x,map_y = c1.getMaps(pts2d)
51+
52+
ret, img = cap.read()
53+
54+
while 1:
55+
ret, img = cap.read()
56+
if ret:
57+
output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR,borderMode=4)
58+
output = cv2.flip(output,1)
59+
out1 = np.hstack((img,output))
60+
out1 = cv2.resize(out1,(700,350))
61+
cv2.imshow("output",out1)
62+
if cv2.waitKey(1)&0xFF == 27:
63+
break
64+
else:
65+
break
29.4 KB
Loading
25.9 KB
Loading

FunnyMirrors/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
### Funny Mirrors using OpenCV
2+
3+
We show how to create some funny mirror effects using OpenCV. The blog is based on fundamental concepts like camera projection, intrinsic and extrinsic
4+
camera parameters and mesh-based image warping.
5+
6+
### Installing additional library
7+
```shell
8+
pip3 install vcam
9+
```
10+
11+
### How to run the code
12+
13+
Command line usage for running the code
14+
15+
* Python
16+
17+
* Running on sample images:
18+
19+
```
20+
python3 FunnyMirrorsImages.py
21+
```
22+
23+
* Running on a video file:
24+
25+
```
26+
python3 FunnyMirrorsVideo.py ./data/Video3.mp4 0
27+
```
28+
29+
The syntax here is `python3 FunnyMirrorsVideo.py <VIDEO_FILE_PATH> <MODE_NUMBER>`. The `MODE_NUMBER` ranges from 0 to 7. It determines which funny mirror effect will be applied.
30+
31+
32+
### Some funny mirrors generated
33+
<img src = "./Mirror-effect-1-image-3.jpg" width = 1000 height = 282/>
34+
<img src = "./Mirror-effect-5-image-3.jpg" width = 1000 height = 282/>
35+
36+
37+
38+
# AI Courses by OpenCV
39+
40+
Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/courses/) is a great place to start.
41+
42+
<a href="https://opencv.org/courses/">
43+
<p align="center">
44+
<img src="https://www.learnopencv.com/wp-content/uploads/2020/04/AI-Courses-By-OpenCV-Github.png">
45+
</p>
46+
</a>
47+

FunnyMirrors/data/Video3.mp4

2.96 MB
Binary file not shown.

FunnyMirrors/data/chess.png

1.71 KB
Loading

FunnyMirrors/data/im2.jpeg

9.05 KB
Loading

FunnyMirrors/data/img3.jpg

297 KB
Loading

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/course
1313

1414
| Blog Post | |
1515
| ------------- |:-------------|
16+
|[Funny Mirrors Using OpenCV](https://www.learnopencv.com/Funny-Mirrors-Using-OpenCV/)|[code](https://github.com/spmallick/learnopencv/tree/master/FunnyMirrors)|
1617
|[t-SNE for ResNet feature visualization](https://www.learnopencv.com/t-sne-for-resnet-feature-visualization/)|[Code](https://github.com/spmallick/learnopencv/tree/master/TSNE)|
1718
|[Multi-Label Image Classification with Pytorch](https://www.learnopencv.com/multi-label-image-classification-with-pytorch/)|[Code](https://github.com/spmallick/learnopencv/tree/master/PyTorch-Multi-Label-Image-Classification)|
1819
|[CNN Receptive Field Computation Using Backprop](https://www.learnopencv.com/cnn-receptive-field-computation-using-backprop/)|[Code](https://github.com/spmallick/learnopencv/tree/master/PyTorch-Receptive-Field-With-Backprop)|

0 commit comments

Comments
 (0)