Image#
image_processing.py#
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# Copyright (c) 2018-2023 www.open3d.org
5# SPDX-License-Identifier: MIT
6# ----------------------------------------------------------------------------
7
8import numpy as np
9import matplotlib.pyplot as plt
10import matplotlib.image as mpimg
11
12import open3d as o3d
13#conda install pillow matplotlib
14
15if __name__ == "__main__":
16
17 print("Testing image in open3d ...")
18 print("Convert an image to numpy")
19 sample_image = o3d.data.JuneauImage()
20 x = o3d.io.read_image(sample_image.path)
21 print(np.asarray(x))
22 print(
23 "Convert a numpy image to o3d.geometry.Image and show it with DrawGeomtries()."
24 )
25 y = mpimg.imread(sample_image.path)
26 print(y.shape)
27 yy = o3d.geometry.Image(y)
28 print(yy)
29 o3d.visualization.draw_geometries([yy])
30
31 print("Render a channel of the previous image.")
32 z = np.array(y[:, :, 1])
33 print(z.shape)
34 print(z.strides)
35 zz = o3d.geometry.Image(z)
36 print(zz)
37 o3d.visualization.draw_geometries([zz])
38
39 print("Write the previous image to file.")
40 o3d.io.write_image("test.jpg", zz, quality=100)
41
42 print("Testing basic image processing module.")
43 sample_image = o3d.data.JuneauImage()
44 im_raw = mpimg.imread(sample_image.path)
45 im = o3d.geometry.Image(im_raw)
46 im_g3 = im.filter(o3d.geometry.ImageFilterType.Gaussian3)
47 im_g5 = im.filter(o3d.geometry.ImageFilterType.Gaussian5)
48 im_g7 = im.filter(o3d.geometry.ImageFilterType.Gaussian7)
49 im_gaussian = [im, im_g3, im_g5, im_g7]
50 pyramid_levels = 4
51 pyramid_with_gaussian_filter = True
52 im_pyramid = im.create_pyramid(pyramid_levels, pyramid_with_gaussian_filter)
53 im_dx = im.filter(o3d.geometry.ImageFilterType.Sobel3dx)
54 im_dx_pyramid = o3d.geometry.Image.filter_pyramid(
55 im_pyramid, o3d.geometry.ImageFilterType.Sobel3dx)
56 im_dy = im.filter(o3d.geometry.ImageFilterType.Sobel3dy)
57 im_dy_pyramid = o3d.geometry.Image.filter_pyramid(
58 im_pyramid, o3d.geometry.ImageFilterType.Sobel3dy)
59 switcher = {
60 0: im_gaussian,
61 1: im_pyramid,
62 2: im_dx_pyramid,
63 3: im_dy_pyramid,
64 }
65 for i in range(4):
66 for j in range(pyramid_levels):
67 plt.subplot(4, pyramid_levels, i * 4 + j + 1)
68 plt.imshow(switcher.get(i)[j])
69 plt.show()