Python图像显示、保存和读取

10 篇文章 3 订阅
7 篇文章 0 订阅

1. 简介

2. 显示2D图像

2.1 显示深度图像 (matplotlib)

  • plt.imshow:显示2D图像,即图像有且只有2个维度,元素可为U8或float
import scipy.io as scio
import open3d as o3d
import numpy as np
import cv2
import matplotlib
from matplotlib import pyplot as plt

# read mat file
data = scio.loadmat("D:\xx\A2J\ITOP-Dataset\itop_side_test/1.mat")
depth_map = data['DepthNormal'][:,:,3]
image = np.zeros(depth_map.shape, dtype = np.uint8)
for r in range(depth_map.shape[0]):
    for c in range(depth_map.shape[1]):
        if depth_map[r,c] < 6.0:  # max depth value is 6.0
            image[r,c] = depth_map[r,c]/6.0 * 255

# Display image
plt.figure("test", figsize=(8,8)) # 创建一个名为astronaut的窗口,并设置大小 

plt.subplot(2,2,1)
plt.title("U8")
plt.imshow(image)

plt.subplot(2,2,2)
plt.title("Float")
plt.imshow(depth_map)

plt.subplot(2,2,3)
plt.title("U8-gray")
plt.imshow(image, plt.cm.gray)

plt.subplot(2,2,4)
plt.title("Float-gray")
plt.imshow(depth_map, plt.cm.gray)

plt.show()
## save image
plt.imsave('plt-u8.png', image)
plt.imsave('plt-float.png',depth_map)
  • 显示结果
    在这里插入图片描述

2.2 显示深度图像 (cv2)

  • 保存Numpy数组为图像
    • 保存之前,归一化为8位整数
    • 生成8位灰度数据
data = scio.loadmat("D:\xx\A2J\ITOP-Dataset\itop_side_test/1.mat")
depth_map = data['DepthNormal'][:,:,3]
image = np.zeros(depth_map.shape, dtype = np.uint8)
for r in range(depth_map.shape[0]):
    for c in range(depth_map.shape[1]):
        if depth_map[r,c] < 6.0:  # max depth value is 6.0
            image[r,c] = depth_map[r,c]/6.0 * 255
cv2.imwrite("opencv_gray.png", image)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(image, alpha=1.0), cv2.COLORMAP_JET)
cv2.imwrite("opencv_color.png", depth_colormap)
# Stack multiple images horizontal
depth_image = np.zeros((depth_map.shape[0],depth_map.shape[1], 3), dtype = np.uint8)
depth_image[:,:,0] = image[:,:]
depth_image[:,:,1] = image[:,:]
depth_image[:,:,2] = image[:,:]
images = np.hstack((depth_image, depth_colormap)) # hstack image must have the same shape
cv2.imshow('images', images)
cv2.waitKey()
cv2.destroyAllWindows()

在这里插入图片描述

2.3 显示彩色图像

  • 示例代码
img = plt.imread("D:\APractise\A2J\A2J-master\src/1.jpg")
plt.figure("test", figsize=(8,8)) # 创建一个名为test的窗口,并设置大小 

plt.subplot(2,2,1)
plt.title("RGB")
plt.imshow(img)

plt.subplot(2,2,2)
plt.title("R")
plt.imshow(img[:,:,0])

plt.subplot(2,2,3)
plt.title("G")
plt.imshow(img[:,:,1])

plt.subplot(2,2,4)
plt.title("B")
plt.imshow(img[:,:,2])

plt.show()
  • 显示结果
    在这里插入图片描述

3. 显示散点图

  • 在指定的 x,y坐标处画指定的内容,如(-)
  • 示例代码
def generate_anchors(P_h=None, P_w=None):
    if P_h is None:
        P_h = np.array([2,6,10,14])

    if P_w is None:
        P_w = np.array([2,6,10,14])

    num_anchors = len(P_h) * len(P_h)
    #print("num_anchors=", num_anchors)

    # initialize output anchors
    anchors = np.zeros((num_anchors, 2))
    k = 0
    for i in range(len(P_w)):
        for j in range(len(P_h)):
            anchors[k,1] = P_w[j]
            anchors[k,0] = P_h[i]
            k += 1  
    #print("anchors=\n", anchors)   
    #print("anchors.shape=", anchors.shape)
    print("Display Template Anchors:")
    plt.plot(anchors[:,0], anchors[:,1], marker='.', color='blue', linestyle='none')
    plt.show()    
    return anchors   # anchors.shape=(16,2)  

generate_anchors()
  • 输出
    在这里插入图片描述

4. 显示3D图像

def pixel2world(x,y,z):
    worldX = (x - 160.0)*z*0.0035
    worldY = (120.0 - y)*z*0.0035
    return worldX,worldY, z
    
data = scio.loadmat("D:\xx\A2J\ITOP-Dataset\itop_side_test-ok/0.mat")
depth_map = data['DepthNormal'][:,:,3]
np_points = np.zeros((depth_map.shape[0] * depth_map.shape[1], 3), dtype='float32')
# generate the point cloud data
height, width = np.shape(depth_map)
print("height={}, width={}".format(height, width))

print(depth_map.shape)
for y in range(0, height-1):
    index = y * width
    for x in range(0, width-1):
        if depth_map[y,x] < 3.5:
            np_points[index+x,:] = pixel2world(x,y,depth_map[y,x])

pcd = o3d.geometry.PointCloud() # create open3d PCD
pcd.points = o3d.utility.Vector3dVector(np_points)  # from numpy to Open3D
# o3d.visualization.draw_geometries([pcd]) # show the pcd
o3d.visualization.draw_geometries([pcd], 
                                  front = [ 0.0, 0.0, -1.0],
                                  lookat = [ 0.25160008668899536, 0.074648439884185791, 2.095703125 ], 
                                  up = [ 0.0, 1.0, 0.0 ],
                                  zoom = 0.5) 

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值