OpenCV转pytorch

OpenCV的一些操作转pytorch,从而有助于使用GPU加速,甚至导出onnx和转TensorRT

需要注意opencv的输入是numpy tensor,format是HW的2D张量或者HWC的3D张量,而pytorch一般是NCHW的4D或者CHW的3D张量。

Dilation腐蚀与膨胀

12: 腐蚀与膨胀 | 陌上见花开

https://blog.51cto.com/u_16175442/8629546

python 复制代码
import cv2
import torch.nn.functional as F


def dilate_cv(img, dilate_factor=10):
    """
    input img is np 2D, HWC 3D
    """
    img = img.astype(np.uint8)
    img1 = cv2.dilate(
        img,
        np.ones((dilate_factor, dilate_factor), np.uint8),
        iterations=1
    )
    return img1


def dilate_torch(img, dilate_factor=10):
    """
    input img should be 3D CHW, or 4D NCHW
    """
    h, w = img.shape[-2:]
    img1 = F.max_pool2d(img, kernel_size=dilate_factor, stride=1, padding=dilate_factor//2)
    if dilate_factor % 2 == 0:
        img1 = img1[:, :, :h, :w]
    return img1

Resize

python 复制代码
import cv2
from torchvision.transforms.functional import resize
from torchvision.transforms import InterpolationMode

img_cv = cv2.resize(img_hwc, (scale*W, scale*H), interpolation=cv2.INTER_NEAREST)
img_torch = resize(img_chw, (scale*H, scale*W), interpolation=InterpolationMode.NEAREST)

需要注意的是opencv的resize和torch的resize结果不是完全对齐的,因为align方式的原因。

颜色转换

python 复制代码
bgr_cv = cv2.cvtColor(data_np, cv2.COLOR_RGB2BGR)

def bgr2rgb_torch_nhwc(bgr):
    # for HWC input
    b,g,r = bgr.split(split_size=1, dim=-1)
    rgb = torch.cat([r,g,b], dim=-1).numpy()
    return rgb

Blur

python 复制代码
import torch
import numpy as np
import cv2

img_hwc = np.random.rand(*[256, 256, 3]).astype("float32")
img_chw = img_hwc.transpose([2, 0, 1])
img_chw_tc = torch.from_numpy(img_chw)

kernel_size = 3

img_blur_cv = cv2.blur(img_hwc, (kernel_size, kernel_size))
img_blur_cv_chw = img_blur_cv.transpose([2, 0, 1])

def mean_blur_torch(img_chw, kernel_size):
    device = img_chw.device
    dtype = img_chw.dtype

    pad_l = kernel_size // 2
    pad_r = kernel_size // 2
    if kernel_size % 2 == 0:
        pad_r = pad_r-1

    img_chw1 = torch.nn.functional.pad(img_chw, pad=[pad_l, pad_r, pad_l, pad_r], mode='reflect')
    weight = torch.ones(*(3, 1, kernel_size, kernel_size), dtype=dtype, device=device)/kernel_size/kernel_size
    img_blur_chw = torch.nn.functional.conv2d(img_chw1, weight, padding=0, groups=3)
    return img_blur_chw

img_blur_torch_chw = mean_blur_torch(img_chw_tc, kernel_size)
img_blur_torch_chw = img_blur_torch_chw.numpy()

error = np.abs(img_blur_cv_chw - img_blur_torch_chw)
print("error:", np.max(error), np.mean(error))
相关推荐
撞南墙者2 小时前
OpenCV自学系列(2)——核心操作(core operations)
人工智能·opencv·计算机视觉
Bearnaise5 小时前
GaussianDreamer: Fast Generation from Text to 3D Gaussians——点云论文阅读(11)
论文阅读·人工智能·python·深度学习·opencv·计算机视觉·3d
CSBLOG8 小时前
OpenCV、YOLO、VOC、COCO之间的关系和区别
人工智能·opencv·yolo
qq762118229 小时前
Ubuntu20.04 rk3588交叉编译opencv4.10
opencv
盘古开天166612 小时前
第一讲,Opencv计算机视觉基础之计算机视觉概述
人工智能·python·opencv·计算机视觉
新手村领路人15 小时前
python c++ opencv打开图片路径写法
c++·python·opencv
光学测量小菜鸡1 天前
OpenCV双目立体视觉重建
opencv·3d·双目相机·结构光·sgbm
一只老虎1 天前
基于OpenCV的图片人脸检测研究
人工智能·opencv·计算机视觉
HZ355721 天前
VS2022编译32位OpenCV
人工智能·opencv·计算机视觉