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 小时前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
湫ccc3 小时前
《Opencv》基础操作详解(3)
人工智能·opencv·计算机视觉
yinqinggong5 小时前
从源码编译支持FFmpeg的OpenCV
opencv·ffmpeg
864记忆7 小时前
关于opencv、Qt、msvc编译器之间的关系
人工智能·qt·opencv
aworkholic7 小时前
opencv sdk for java中提示无stiching模块接口的问题
java·c++·opencv·jni·opencv4android·stiching
pk_xz1234567 小时前
OpenCV实现实时人脸检测和识别
人工智能·opencv·计算机视觉
是十一月末8 小时前
Opencv实现图片和视频的加噪、平滑处理
人工智能·python·opencv·计算机视觉·音视频
jndingxin9 小时前
OpenCV相机标定与3D重建(26)计算两个二维点集之间的部分仿射变换矩阵(2x3)函数 estimateAffinePartial2D()的使用
opencv·3d
游客52012 小时前
opencv中的常用的100个API
图像处理·人工智能·python·opencv·计算机视觉
吃个糖糖13 小时前
36 Opencv SURF 关键点检测
人工智能·opencv·计算机视觉