目录
1. open3d.geometry.Image和numpy互转
2. 对open3d.geometry.Image进行高斯过滤
3. 高斯金字塔过滤
4. sobel过滤
5. 可视化o3d.geometry.Image
1. open3d.geometry.Image和numpy互转
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import open3d as o3d
#conda install pillow matplotlib
if __name__ == "__main__":
# 1. open3d.geometry.Image 转 numpy
print("Testing image in open3d ...")
print("Convert an image to numpy")
sample_image = o3d.data.JuneauImage()
x = o3d.io.read_image(sample_image.path) # open3d.geometry.Image
np_x = np.asarray(x) # (h,w,3)
print(np.asarray(x)) # open3d.geometry.Image 转 numpy
# 2. numpy转pen3d.geometry.Image
print(
"Convert a numpy image to o3d.geometry.Image and show it with DrawGeomtries()."
)
y = mpimg.imread(sample_image.path) # numpy. (h,w,3)
print(y.shape)
yy = o3d.geometry.Image(y) # numpy 转 open3d.geometry.Image
print(yy)
o3d.visualization.draw_geometries([yy])
# 3. 单通道图numpy转pen3d.geometry.Image
print("Render a channel of the previous image.")
z = np.array(y[:, :, 1]) # (h,w,3)->(h,w)
print(z.shape)
print(z.strides)
zz = o3d.geometry.Image(z) # numpy转open3d.geometry.Image
print(zz)
o3d.visualization.draw_geometries([zz])
# 保存open3d.geometry.Image
print("Write the previous image to file.")
o3d.io.write_image("test.jpg", zz, quality=100)
2. 对open3d.geometry.Image进行高斯过滤
print("Testing basic image processing module.")
sample_image = o3d.data.JuneauImage()
im_raw = mpimg.imread(sample_image.path) # numpy
im = o3d.geometry.Image(im_raw) # numpy转open3d.geometry.Image。3通道
im_g3 = im.filter(o3d.geometry.ImageFilterType.Gaussian3) # kernel 3. 单通道结果
im_g5 = im.filter(o3d.geometry.ImageFilterType.Gaussian5)
im_g7 = im.filter(o3d.geometry.ImageFilterType.Gaussian7)
im_gaussian = [im, im_g3, im_g5, im_g7] # 4个尺度:m, m/2, m/4, m/8
3. 高斯金字塔过滤
对o3d.geometry.Image,先Gaussian过滤,再下采样。
# 对im(o3d.geometry.Image) 进行下采样: im, img/2, img/4, img/8
pyramid_levels = 4
pyramid_with_gaussian_filter = True
im_pyramid = im.create_pyramid(pyramid_levels, pyramid_with_gaussian_filter) # 先Gaussian,再下采样. list
4. sobel过滤
# sobel x: 一张图片im.filter; 多张图片list. o3d.geometry.Image.filter_pyramid
im_dx = im.filter(o3d.geometry.ImageFilterType.Sobel3dx) # return open3d.geometry.Image
im_dx_pyramid = o3d.geometry.Image.filter_pyramid(im_pyramid, o3d.geometry.ImageFilterType.Sobel3dx) # sobel x
# sobel y
im_dy = im.filter(o3d.geometry.ImageFilterType.Sobel3dy)
im_dy_pyramid = o3d.geometry.Image.filter_pyramid(im_pyramid, o3d.geometry.ImageFilterType.Sobel3dy)
5. 可视化o3d.geometry.Image
switcher = {
0: im_gaussian, # 纯高斯过滤
1: im_pyramid, # 高斯金字塔
2: im_dx_pyramid, # 高斯金字塔 + sobel x
3: im_dy_pyramid, # 高斯金字塔 + sobel y
}
for i in range(4):
for j in range(pyramid_levels):
plt.subplot(4, pyramid_levels, i * 4 + j + 1)
plt.imshow(switcher.get(i)[j]) # switcher.get(i): 第一行是纯高斯过滤,第二行是高斯金字塔,第三行是sobel x,最后是y
plt.show()
以下图片:
第一行是纯高斯过滤im_gaussian; 第二行是高斯金字塔im_pyramid; 第三行是im_dx_pyramid; 最后一行是im_dy_pyramid.