open3d 源码阅读image_processing.py

目录

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.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值