1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import numpy as np
28import matplotlib.pyplot as plt
29import matplotlib.image as mpimg
30
31import open3d as o3d
32#conda install pillow matplotlib
33
34if __name__ == "__main__":
35
36 print("Testing image in open3d ...")
37 print("Convert an image to numpy")
38 sample_image = o3d.data.JuneauImage()
39 x = o3d.io.read_image(sample_image.path)
40 print(np.asarray(x))
41 print(
42 "Convet a numpy image to o3d.geometry.Image and show it with DrawGeomtries()."
43 )
44 y = mpimg.imread(sample_image.path)
45 print(y.shape)
46 yy = o3d.geometry.Image(y)
47 print(yy)
48 o3d.visualization.draw_geometries([yy])
49
50 print("Render a channel of the previous image.")
51 z = np.array(y[:, :, 1])
52 print(z.shape)
53 print(z.strides)
54 zz = o3d.geometry.Image(z)
55 print(zz)
56 o3d.visualization.draw_geometries([zz])
57
58 print("Write the previous image to file.")
59 o3d.io.write_image("test.jpg", zz, quality=100)
60
61 print("Testing basic image processing module.")
62 sample_image = o3d.data.JuneauImage()
63 im_raw = mpimg.imread(sample_image.path)
64 im = o3d.geometry.Image(im_raw)
65 im_g3 = im.filter(o3d.geometry.ImageFilterType.Gaussian3)
66 im_g5 = im.filter(o3d.geometry.ImageFilterType.Gaussian5)
67 im_g7 = im.filter(o3d.geometry.ImageFilterType.Gaussian7)
68 im_gaussian = [im, im_g3, im_g5, im_g7]
69 pyramid_levels = 4
70 pyramid_with_gaussian_filter = True
71 im_pyramid = im.create_pyramid(pyramid_levels, pyramid_with_gaussian_filter)
72 im_dx = im.filter(o3d.geometry.ImageFilterType.Sobel3dx)
73 im_dx_pyramid = o3d.geometry.Image.filter_pyramid(
74 im_pyramid, o3d.geometry.ImageFilterType.Sobel3dx)
75 im_dy = im.filter(o3d.geometry.ImageFilterType.Sobel3dy)
76 im_dy_pyramid = o3d.geometry.Image.filter_pyramid(
77 im_pyramid, o3d.geometry.ImageFilterType.Sobel3dy)
78 switcher = {
79 0: im_gaussian,
80 1: im_pyramid,
81 2: im_dx_pyramid,
82 3: im_dy_pyramid,
83 }
84 for i in range(4):
85 for j in range(pyramid_levels):
86 plt.subplot(4, pyramid_levels, i * 4 + j + 1)
87 plt.imshow(switcher.get(i)[j])
88 plt.show()