python图像处理实验中记录(包括图像读取、图像缩放、图像格式转换等)

  • 图像读取
    • 使用skimage img = io.imread(img_path)
      • np.ndarray, (0, 255), RGB
    • 使用opencv img = cv2.imread(img_path)
      • np.ndarray, (0, 255), BGR
    • 使用PIL img = Image.open(img_path)
      • PIL.Image.Image对象 (H * W * C), (0, 255), RGB
  • 图像缩放
    • 一般说的102*128指:高102,宽128
    • 使用skimage(from skimage import transform) img = transform.resize(img, (height, width))
    • 使用opencv img = cv2.resize(img, (width, height))
    • 使用PIL img =img.resize((width, height), Image.ANTIALIAS)
    • 使用torch (from torchvision import transforms)
      • resize = transforms.Resize([height, width]) img = resize(img)
  • 图像格式转换
  • torchvision
    • 可以把张量或ndarray转为PIL图像
    • 可以将PIL图像或ndarray图像转为张量
  • 小操作
    • 图像转为ndarray之后可以使用切片进行裁减或者替换对应位置的像素点
      • img = np.array(img, dtype=np.uint8)
        img = img[26:-2, 11:-9]
    • 对图像进行增加一个通道的处理,类似于tensor, numpy 的操作,此处引入了新的张量转换包einops
      • mask_img = np.expand_dims(mask_img, axis=2)
      • img = einops.rearrange(img,“w h -> w h 1”)
      • input_tensor = torch.unsqueeze(img_tensor, dim=0) # 增加一个batch维度
    • 图像去除维度1
      • img = np.squeeze(img)
    • 图像维度变换,使用einops包
      • img = einops.rearrange(img,“h w c -> w h c”)
    • 给图像上下左右填充像素点,以其外侧边缘的像素类型进行填充
      • top_size, bottom_size, left_size, right_size = (24, 0, 12, 12)
        img = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE)
    • 设定阈值,把图像grayscale_cam变为二值化图像frame_cam
      • frame_cam = np.where(grayscale_cam > 0.5, 1.0, 0.0)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值