• copyTo(src, |mask, dst)
  • bitwise_and(src1, src2, |dst, mask)
  • bitwise_or(src1, src2, |dst, mask)
  • bitwise_xor(src1, src2, |dst, mask)
  • hconcat(src)
  • vconcat(src)
  • mean(src, mask)
  • flip(src, flipCode)
  • rotate(src, rotateMode)
  • solve(src1, src2, |method)
  • normalize(src, dst, alpha, beta, norm_type, |dtype, mask)
  • merge(mv)
  • split(m)
  • addWeighted(src1, alpha, src2, beta, gamma)
  • haveImageReader(filename)
  • haveImageWriter(filename)
  • imdecode(buf, |flag)
  • imencode(ext, img, |params)
  • imread(filename, |flag)
  • imwrite(filename, img, |params)
  • Rodrigues(src)
  • solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs, |flags)
  • cvtColor(src, code, |dstCn)
  • cvtColorTwoPlane(src1, src2, code)
  • bilateralFilter(src, d, sigmaColor, sigmaSpace, |borderType)
  • blur(src, ksize, |borderType)
  • boxFilter(src, ddepth, ksize, |normalize, borderType)
  • dilate(src, kernel, |iterations, borderType)
  • erode(src, kernel, |iterations, borderType)
  • filter2D(src, ddepth, kernel, |delta, borderType)
  • GaussianBlur(src, ksize, sigmaX, |sigmaY, borderType)
  • getDerivKernels(dx, dy, ksize, |normalize)
  • getGaborKernel(ksize, sigma, theta, lambd, gamma, |psi)
  • getGaussianKernel(ksize, sigma)
  • getStructuringElement(shape, ksize)
  • Laplacian(src, ddepth, |ksize, scale, delta, borderType)
  • pyrDown(src, |dstsize, borderType)
  • pyrUp(src, |dstsize, borderType)
  • Scharr(src, ddepth, dx, dy, |scale, delta, borderType)
  • sepFilter2D(src, ddepth, kx, ky, |delta, borderType)
  • Sobel(src, ddepth, dx, dy, |ksize, scale, delta, borderType)
  • spatialGradient(src, |ksize, borderType)
  • sqrBoxFilter(src, ddepth, ksize, |normalize, borderType)
  • getAffineTransform(src, dst)
  • getPerspectiveTransform(src, dst)
  • getRectSubPix(image, patchSize, center)
  • getRotationMatrix2D(center, angle, scale)
  • invertAffineTransform(m)
  • convertMaps(map1, map2, dstmap1type, |interpolation)
  • remap(src, map1, map2, interpolation, |borderMode, borderValue)
  • resize(src, dsize, |fx, fy, interpolation, code, mean, norm)
  • warpAffine(src, M, dsize, |flag, borderMode, borderValue, code, mean, norm)
  • warpPerspective(src, M, dsize, flag, borderMode, borderValue)
  • adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)
  • blendLinear(src1, src2, weight1, weight2)
  • threshold(src, thresh, maxval, type)
  • findContours(image, mode, method, offset)
  • contourArea(points, oriented)
  • convexHull(points, clockwise, returnPoints)
  • minAreaRect(points)
  • boundingRect(points)
  • connectedComponentsWithStats(image, connectivity)
  • boxPoints(box)
  • line(img, pt1, pt2, color, thickness, lineType, shift)
  • arrowedLine(img, pt1, pt2, color, thickness, lineType, shift, tipLength)
  • circle(img, center, radius, color, thickness, lineType, shift)
  • rectangle(src, pt1, pt2, color, thickness, lineType, shift)
  • drawContours(img, contours, contourIdx, color, thickness, lineType)
  • fillPoly(img, contours, color, lineType, shift, offset)
  • calcHist(imgs, channels, mask, histSize, ranges, accumulate)
  • optim
  • compress
  • linalg
  • random
  • MNN.Interpreter [deprecated]
  • MNN.Session [deprecated]
  • MNN.OpInfo
  • MNN.Tensor [deprecated]
  • MNN.CVImageProcess [deprecated]
  • 不建议使用该接口,请使用cv代替
  • MNN.CVMatrix [deprecated]
  • expr.Var
  • nn._Module
  • nn.RuntimeManager
  • optim.Optimizer
  • data.Dataset
  • data.DataLoader
  • CV模块中的枚举类型直接用int实现,所以请使用 cv.COLOR_BGR2BGRA ,不要用 cv.ColorConversionCodes.COLOR_BGR2BGRA *

  • [h, w, c] 的图形转换为模型输入的 [n, c, h, w] 不要使用 transpose ;请使用 expr.convert ,示例如下:

    import MNN.cv as cv
    import MNN.numpy as np
    import MNN.expr as expr
    # data_format: NHWC, shape: [360, 480, 3], dtype: uint8
    img = imread('cat.jpg')
    # data_format: NHWC, shape: [360, 480, 3], dtype: float32
    imgf = img.astype(np.float32)
    # data_format: NHWC, shape: [1, 360, 480, 3], dtype: float32
    imgf_batch = np.expand_dims(imgf, 0)
    # data_format: NCHW, shape: [1, 360, 480, 3], dtype: float32
    input_var = expr.convert(imgf_batch, expr.NCHW)
    

    copyTo(src, |mask, dst)

    将src复制并返回,如果mask不为空,则只拷贝mask为1的像素;如果dst不为空,则在mask为0时拷贝dst中对应的像素,参考:copyTo

    注意:目前src仅支持int32类型数据,用户使用前后需要自行转换类型

  • src:Var 源图像

  • mask:Var 掩码图像,可选

  • dst:Var mask为0时选择的图像,可选

  • 返回:复制的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> h, w, _ = img.shape
    >>> zero = np.zeros((h//3, w), dtype=np.int32)
    >>> one = np.ones((h//3, w), dtype=np.int32)
    >>> mask = np.concatenate((one, zero, one), axis=0)
    >>> img = img.astype(np.int32)
    >>> copyTo = cv.copyTo(img, mask).astype(np.uint8)
    >>> cv.imwrite('copyTo.jpg', copyTo)
    

    bitwise_and(src1, src2, |dst, mask)

    对src1和src2执行按位与操作,并对结果按照执行copyTo返回,参考:bitwise_and

  • src1:Var 源图像

  • src2:Var 源图像

  • mask:Var 掩码图像,可选

  • dst:Var mask为0时选择的图像,可选

  • 返回:按位与的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> cv.bitwise_and(img, img)
    array([[[ 49,  57,  26],
            [158, 175, 184]]], dtype=uint8)
    
    
    
    
        
    
    

    bitwise_or(src1, src2, |dst, mask)

    对src1和src2执行按位或操作,并对结果按照执行copyTo返回,参考:bitwise_or

  • src1:Var 源图像

  • src2:Var 源图像

  • mask:Var 掩码图像,可选

  • dst:Var mask为0时选择的图像,可选

  • 返回:按位或的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> cv.bitwise_or(img, img)
    array([[[ 49,  57,  26],
            [158, 175, 184]]], dtype=uint8)
    

    bitwise_xor(src1, src2, |dst, mask)

    对src1和src2执行按位异或操作,并对结果按照执行copyTo返回,参考:bitwise_xor

  • src1:Var 源图像

  • src2:Var 源图像

  • mask:Var 掩码图像,可选

  • dst:Var mask为0时选择的图像,可选

  • 返回:按位异或的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> cv.bitwise_xor(img, img)
    array([[[0, 0, 0],
            [0, 0, 0]]], dtype=uint8)
    
    >>> img = cv.imread('cat.jpg')
    >>> flip = cv.flip(img, -1)
    >>> cv.imwrite('flip.jpg', flip)
    
    >>> img = cv.imread('cat.jpg')
    >>> rotate = cv.rotate(img, cv.ROTATE_90_CLOCKWISE)
    >>> cv.imwrite('rotate.jpg', rotate)
    

    返回类型:Tuple(bool, Var)

    >>> a = np.array([2., 3., 4., 0., 1., 5., 0., 0., 3.]).reshape(3, 3)
    >>> b = np.array([1., 2., 3.]).reshape(3, 1)
    >>> cv.solve(a, b)
    (True, array([[ 3.],
                  [-3.],
                  [ 1.]], dtype=float32))
    

    normalize(src, dst, alpha, beta, norm_type, |dtype, mask)

    对输入进行归一化;参考:normalize

  • src:Var 输入矩阵

  • dst:Var Python中不需要使用该参数,直接赋为None即可

  • alpha:float 归一化的下限

  • beta:float 归一化的上限

  • norm_type:int 归一化类型,如:cv.NORM_MINMAX

  • dtype:dtype 输入类型,不需要赋值

  • mask 兼容性参数,目前还不支持mask

  • 返回:归一化结果

    返回类型:Var

    >>> x = np.arange(12).reshape(2, 2, 3).astype(np.uint8)
    >>> cv.normalize(x, None, -50, 270, cv.NORM_MINMAX)
    array([[[  0,   0,   8],
            [ 37,  66,  95]],
           [[125, 154, 183],
            [212, 241, 255]]], dtype=uint8)
    
    >>> x = np.arange(12).reshape(2, 2, 3)
    >>> cv.split(x)
    [array([[0, 3],[6, 9]], dtype=int32),
     array([[1, 4],[7, 10]], dtype=int32),
     array([[2, 5],[8, 11]], dtype=int32)]
    

    addWeighted(src1, alpha, src2, beta, gamma)

    对输入的两个矩阵执行权重相加:dst = src1 * alpha + src2 * beta + gamma;参考:addWeighted

  • src1:Var 第一个输入矩阵

  • alpha:float 第一个输入矩阵的权重

  • src2:Var 第二个输入矩阵

  • beta:float 第二个输入矩阵的权重

  • gamma:float 额外增加的常量

  • 返回:加权得到的和

    返回类型:Var

    >>> x = np.arange(3.)
    >>> cv.addWeighted(x, 0.2, x, 0.5, 1)
    array([1. , 1.7, 2.4], dtype=float32)
    

    haveImageReader(filename)

    用于判断是否支持特定图像格式的解码,目前支持的图像格式:jpg, jpeg, png, bmp,参考:haveImageReader

    移动端默认不包含该函数

  • filename:str 图像文件路径

  • 返回:是否有读取图像的接口

    返回类型:bool

    >>> cv.haveImageReader('cat.jpg')
    

    haveImageWriter(filename)

    用于判断是否支持特定图像格式的编码,目前支持的图像格式:jpg, jpeg, png, bmp,参考:haveImageWriter

    移动端默认不包含该函数

  • filename:str 图像文件路径

  • 返回:是否有写图像的接口

    返回类型:bool

    >>> cv.haveImageWriter('cat.jpg')
    
  • buf:ndarray|sequence 图像数据序列,可以是ndarray, list, tuple, bytes等

  • flag:int 解码方式,可选,默认为cv2.IMREAD_COLOR

  • 返回:解码后的图像

    返回类型:Var

    >>> cv.imdecode(bytearray(open('cat.jpg', 'rb').read()), cv.IMREAD_COLOR)
    array([[[ 49,  57,  26],
            [ 50,  58,  27],
            [ 47,  55,  25],
            [188, 205, 214],
            [158, 175, 184],
            [158, 175, 184]]], dtype=uint8)
    
  • ext:str 图像文件扩展名,如jpg, png等

  • img:Var 图像

  • params:[int] 编码参数,可选,默认为[cv2.IMWRITE_JPEG_QUALITY, 95]

  • 返回:编码后的图像数据序列,first是bool代表是否编码成功,second是listofuint8代表编码后的图像数据序列

    返回类型:pair

    >>> success, buf = cv.imencode('jpg', cv.imread('cat.jpg'))
    >>> success
    >>> buf[:10]
    [255, 216, 255, 224, 0, 16, 74, 70, 73, 70]
    

    返回类型:Var

    >>> cv.Rodrigues(np.array([[1., 0., 0.], [0.,
    
    
    
    
        
     1., 0.], [0., 0., 1.]]))
    array([[0.],
           [0.],
           [0.]], dtype=float32)
    

    solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs, |flags)

    根据输入的 3d坐标集合和2d坐标集合,相机内参和平移矩阵,计算3d坐标到2d坐标的映射关系,并返回旋转矩阵和平移矩阵,参考:solvePnP

    目前仅支持SOLVEPNP_SQPNP

  • objectPoints:Var 3d坐标集合, shape为(n,3),n为点的个数

  • imagePoints:Var 2d坐标集合, shape为(n,2),n为点的个数

  • cameraMatrix:Var 相机内参矩阵, shape为(3,3)

  • distCoeffs:Var 相机畸变系数, shape为(1,5)或(5,), 不使用可以传入[]

  • flags:int 标志位,可选,做兼容性处理,目前仅支持SOLVEPNP_SQPNP

  • 返回:返回值tuple中有3个值,第一个值为bool是否找到变换关系,第二个值为Var是旋转向量,第三个值为Var是平移矩阵

    返回类型:tuple

    >>> model_points = np.array([0.0, 0.0, 0.0, 0.0, -330.0, -65.0, -225.0, 170.0, -135.0, 225.0, 170.0, -135.0, -150.0, -150.0, -125.0, 150.0, -150.0, -125.0]).reshape(6, 3)
    >>> image_points = np.array([359., 391., 399., 561., 337., 297., 513., 301., 345., 465., 453., 469.]).reshape(6, 2)
    >>> camera_matrix = np.array([1200., 0., 600., 0., 1200., 337.5, 0., 0., 1.]).reshape(3, 3)
    >>> dist_coeffs = np.array([0.0, 0.0, 0.0, 0.0]).reshape(4, 1)
    >>> cv.solvePnP(model_points, image_points, camera_matrix, dist_coeffs, flags=cv.SOLVEPNP_SQPNP)
    (True, array([[ 3.000745  ],
           [ 0.03165916],
           [-0.9225616 ]], dtype=float32), array([[-435.97495],
           [  95.3929 ],
           [2201.46   ]], dtype=float32))
    
    >>> img = cv.imread('cat.jpg')         # bgr
    >>> cv.cvtColor(img, cv.COLOR_BGR2GRAY) # gray
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> cv.imwrite('cvtColor.jpg', gray) 
    

    cvtColorTwoPlane(src1, src2, code)

    将图像转换为另一种颜色空间,源图像存储在两个平面中,一般用于YUV_NV21和YUV_NV12到其他颜色空间的转换,参考: cvtColorTwoPlane

  • src1:Var 图像的第一个平面

  • src2:Var 图像的第二个平面

  • code:int 转换方式,使用cv.COLOR_*

  • 返回:转换后的图像

    返回类型:Var

    >>> h = w = 224
    >>> y = np.random.randint(0, 255, h * w).reshape(h, w).astype(np.uint8)
    >>> uv = np.random.randint(0, 255, h * w / 2).astype(np.uint8)
    >>> rgb = cv.cvtColorTwoPlane(y, uv, cv.COLOR_YUV2RGB_NV21)
    >>> cv.imwrite('cvtColorTwoPlane.jpg', rgb)
    

    bilateralFilter(src, d, sigmaColor, sigmaSpace, |borderType)

    双边滤波,直接实现未优化,速度较慢;参考: bilateralFilter

  • src:Var 输入图像

  • d:int 滤波时考虑周围像素的直径,如果为负数则通过sigmaSpace计算

  • sigmaColor:float 颜色空间sigma值

  • sigmaSpace:float 坐标空间sigma值

  • borderType:int 边界模式,可选值;默认为REFLECT

  • 返回:滤波后的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> img = cv.bilateralFilter(img, 20, 80.0, 35.0)
    >>> cv.imwrite('bilateralFilter.jpg', img)
    
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.blur(img, [3, 3])
    >>> cv.imwrite('blur.jpg', img)
    
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.boxFilter(img, -1, [7, 7])
    >>> cv.imwrite('boxFilter.jpg', img)
    
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.dilate(img, cv.getStructuringElement(0, (3, 3)))
    >>> cv.imwrite('dilate.jpg', img)
    
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.erode(img, cv.getStructuringElement(0, (3, 3)))
    >>> cv.imwrite('erode.jpg', img)
    
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.filter2D(
    
    
    
    
        
    img, -1, cv.getStructuringElement(0, (3, 3)))
    >>> cv.imwrite('filter2D.jpg', img)
    

    GaussianBlur(src, ksize, sigmaX, |sigmaY, borderType)

    使用高斯滤镜模糊图像,参考: GaussianBlur

  • src:Var 输入图像

  • ksize:[int] kernel大小

  • sigmaX:float X 方向的高斯核标准差

  • sigmaY:float Y方向的高斯核标准差;如果 sigmaY 为零,则设置为等于 sigmaX,可选,默认为0

  • borderType:int 边界类型,可选,默认为cv.BORDER_DEFAULT

  • 返回:模糊后的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> img = cv.GaussianBlur(img, [5, 5], 3)
    >>> cv.imwrite('GaussianBlur.jpg', img)
    

    getGaborKernel(ksize, sigma, theta, lambd, gamma, |psi)

    返回Gabor滤波器系数,参考: getGaborKernel

  • ksize:[int] 返回的kernel大小

  • sigma:float Gabor的标准差

  • theta:float Gabor函数的平行条纹的法线方向

  • lambd:float 正弦因子的波长

  • gamma:float 空间纵横比

  • psi:float 相位偏移,可选,默认为PI/2

  • 返回:滤波器系数

    返回类型:Var

    >>> cv.getGaborKernel([3, 3], 10, 5, 5, 5)
    array([[ 6.1722213e-01,  9.2025989e-01,  9.3729156e-01],
           [-3.1094351e-01, -4.3711388e-08,  3.1094342e-01],
           [-9.3729156e-01, -9.2025995e-01, -6.1722219e-01]], dtype=float32)
    

    getStructuringElement(shape, ksize)

    返回指定大小和形状的结构元素,用于形态学操作,参考: getStructuringElement

  • shape:int 元素形状

  • 0: 矩形

  • 1: 十字形

  • 2:椭圆形

  • ksize:[int] 结构元素的大小

  • 返回:结构元素

    返回类型:Var

    >>> cv.getStructuringElement(0, (3, 3))
    array([[1, 1, 1],
           [1, 1, 1],
           [1, 1, 1]], dtype=uint8)
    

    Laplacian(src, ddepth, |ksize, scale, delta, borderType)

    计算图像的拉普拉斯算子,参考: Laplacian

  • src:Var 输入图像

  • ddepth:int 图像的深度

  • ksize:int 卷积核大小,可选,默认为1

  • scale:float 缩放因子,可选,默认为1

  • delta:float 加到结果的偏移量,可选,默认为0

  • borderType:int 边界类型,可选,默认为cv.BORDER_DEFAULT

  • 返回:拉普拉斯算子计算结果

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> img = cv.Laplacian(img, -1, 3)
    >>> cv.imwrite('Laplacian.jpg', img)
    
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.pyrDown(img)
    >>> cv.imwrite('pyrDown.jpg', img)
    
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.pyrUp(img)
    >>> cv.imwrite('pyrUp.jpg', img)
    

    Scharr(src, ddepth, dx, dy, |scale, delta, borderType)

    使用Scharr算子计算图像导数,参考: Scharr

  • src:Var 输入图像

  • ddepth:int 图像的深度

  • dx:int 导数x的阶数

  • dy:int 导数y的阶数

  • scale:float 缩放因子,可选,默认为1

  • delta:float 加到结果的偏移量,可选,默认为0

  • borderType:int 边界类型,可选,默认为cv.BORDER_DEFAULT

  • 返回:Scharr算子计算结果

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> cv.Scharr(img, -1, 1, 1)
    array([[[0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0]]], dtype=uint8)
    

    sepFilter2D(src, ddepth, kx, ky, |delta, borderType)

    对图像应用可分离的线性过滤器,参考: sepFilter2D

  • src:Var 输入图像

  • ddepth:int 图像的深度

  • kx:int x方向的kernel

  • ky:int y方向的kernel

  • delta:float 加到结果的偏移量,可选,默认为0

  • borderType:int 边界类型,可选,默认为cv.BORDER_DEFAULT

  • 返回:sepFilter2D计算结果

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> kernelX = np.array([[0., -1., 0.]])
    >>> kernelY = np.array([[-1., 0., -1.]])
    >>> cv.sepFilter2D(img, -1, kernelX, kernelY, 1)
    array([[[1, 1, 1],
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 1]]], dtype=uint8)
    

    Sobel(src, ddepth, dx, dy, |ksize, scale, delta, borderType)

    使用Sobel算子计算图像导数,参考: Sobel

  • src:Var 输入图像

  • ddepth:int 图像的深度

  • dx:int 导数x的阶数

  • dy:int 导数y的阶数

  • ksize:int kernel的大小,可选,默认为3

  • scale:float 缩放因子,可选,默认为1

  • delta:float 加到结果的偏移量,可选,默认为0

  • borderType:int 边界类型,可选,默认为cv.BORDER_DEFAULT

  • 返回:Sobel算子计算结果

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> cv.Sobel(img, -1, 1, 0)
    array([[[0, 0, 0],
            [0, 0, 0],
            [0, 0, 2],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0]]], dtype=uint8)
    

    spatialGradient(src, |ksize, borderType)

    使用Sobel算子分别计算x和y方向的一阶图像导数,参考: spatialGradient

  • src:Var 输入图像

  • ksize:int Sobel kernel的大小,可选,默认为3

  • borderType:int 边界类型,可选,默认为cv.BORDER_DEFAULT

  • 返回:spatialGradient计算结果

    返回类型:Var

    sqrBoxFilter(src, ddepth, ksize, |normalize, borderType)

    计算与过滤器重叠的像素值的归一化平方和,参考: sqrBoxFilter

  • src:Var 输入图像

  • ddepth:int 图像的深度

  • ksize:[int] kernel的大小

  • normalize:bool 是否归一化,可选,默认为true

  • borderType:int 边界类型,可选,默认为cv.BORDER_DEFAULT

  • 返回:sqrBoxFilter计算结果

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> img = cv.sqrBoxFilter(img, -1, (3,3))
    >>> img = img.astype(np.uint8)
    >>> cv.imwrite('sqrBoxFilter.jpg', img)
    
  • src:[float] 输入的一组顶点,类型为list。里面为6个 float元素,分别代表三个顶点的 x, y

  • dst:[float] 计算仿射变换的另一组顶点,类型为list。里面为6个float元素,分别代表三个顶点的 x, y

  • 返回:变换矩阵

    返回类型:CVMatrix 参考:CVMatrix

    >>> src = [50.0, 50.0, 200.0, 50.0, 50.0, 200.0]
    >>> dst = [10.0, 100.0, 200.0, 20.0, 100.0, 250.0]
    >>> cv.getAffineTransform(src, dst)
        [[1.266667  0.600000    -83.333336]
        [-0.533333 1.000000    76.666664]
        [76.666664 0.000000    0.000000]]
    
  • src:[float] 输入的一组顶点,类型为list。里面为6个 float元素,分别代表三个顶点的 x, y

  • dst:[float] 计算仿射变换的另一组顶点,类型为list。里面为6个float元素,分别代表三个顶点的 x, y

  • 返回:变换矩阵

    返回类型:CVMatrix 参考:CVMatrix

    >>> src =  [100.0, 50.0, 100.0, 390.0, 600.0, 50.0, 600.0, 390.0]
    >>> dst = [200.0, 100.0, 200.0, 330.0, 500.0, 50.0, 600.0, 390.0]
    >>> cv2.getPerspectiveTransform(src, dst)
        [[0.307692  -0.104072   174.434372]
        [-0.129231 0.504751    87.685509]
        [87.685509 -0.000585   -0.000520]]
    
  • image:Var 输入的图像

  • patchSize:[int] 裁剪的patch大小(width, height)

  • center:[int] 被裁减出的矩形的中心点(x, y)

  • 返回:裁剪出的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> h, w, c = img.shape
    >>> center = (w / 2.0, h / 2.0)
    >>> img = cv2.getRectSubPix(img, [90, 90], center)
    >>> cv.imwrite('getRectSubPix.jpg', img)
    

    getRotationMatrix2D(center, angle, scale)

    作用等同与 OpenCVGeometric Image Transformations 模块的getRotationMatrix2D 函数,用于计算 2D 旋转的仿射变换矩阵。

  • center:[float] 图像中的旋转中心点(x, y)

  • angle:float 旋转角度(degrees)

  • scale:float 同向放缩因子

  • 返回:仿射变换矩阵

    返回类型:类型为 CVMatrix

    >>> cv.getRotationMatrix2D((500.0 / 2.0, 333.0 / 2.0), 90, 1.0)
    [[-0.000000	1.000000	83.500015]
     [-1.000000	-0.000000	416.500000]
     [416.500000	0.000000	0.000000]]
    

    invertAffineTransform(m)

    作用等同与 OpenCVGeometric Image Transformations 模块的invertAffineTransform 函数,计算仿射变换矩阵的逆矩阵。

  • m:CVMatrix 输入的仿射矩阵

  • 返回:仿射变换矩阵的逆矩阵

    返回类型:类型为 CVMatrix

    >>> m = MNN.CVMatrix()
    >>> m.setScale(5.0, 5.0)
    >>> cv.invertAffineTransform(m)
    [[0.200000      0.000000        -0.000000]
     [0.000000      0.200000        -0.000000]
     [-0.000000     0.000000        0.000000]]
    

    convertMaps(map1, map2, dstmap1type, |interpolation)

    映射map转换,为了兼容OpenCV中的convertMaps 函数;但实际不进行任何操作,仍返回map1, map2

  • map1:Var 原始映射关系

  • map2:Var 原始映射关系

  • dstmap1type:int 兼容性参数,不支持

  • interpolation:int 兼容性参数,不支持

  • 返回:(map1, map2)

    返回类型:类型为 Tuple

    remap(src, map1, map2, interpolation, |borderMode, borderValue)

    作用等同与 OpenCVGeometric Image Transformations 模块的remap 函数,用于图像重映射。

    不支持borderMode与borderValue

  • src:Var 输入的图像

  • map1:Var x坐标映射

  • map2:Var y坐标映射

  • interpolation:int 插值方式,仅支持cv.INTER_NEARESTcv.INTER_LINEAR

  • borderMode:int 兼容性参数,不支持

  • borderValue:int 兼容性参数,不支持

  • 返回:重映射后的图像

    返回类型:类型为 Var

    >>> img = cv.imread('cat.jpg')
    >>> row, col, ch = img.shape
    >>> mapx = np.ones(img.shape[:2], np.float32)
    >>> mapy = np.ones(img.shape[:2], np.float32)
    >>> for i in range(row):
    >>>     for j in range(col):
    >>>         mapx[i, j] = float(j)
    >>>         mapy[i
    
    
    
    
        
    , j] = float(row-i)
    >>> img = cv.remap(img, mapx, mapy, cv.INTER_LINEAR)
    >>> cv.imwrite('remap.jpg', img)
    

    resize(src, dsize, |fx, fy, interpolation, code, mean, norm)

    作用等同与 OpenCVGeometric Image Transformations 模块的resize 函数,用于放缩图像。

    该函数在兼容OpenCV函数的基础上,额外增加了3个参数可选参数:code, mean, norm可以额外完成cvtColor和typeas的功能

  • src:Var 输入的图像

  • dsize:tuple 放缩后的大小

  • fx:float 水平方向的放缩因子,如果为0,则自动计算,默认为0

  • fy:float 竖直方便的放缩因子,如果为0,则自动计算,默认为0

  • interpolation:int 放缩的插值方法,默认为cv.INTER_LINEAR

  • code:int 可以在缩放时转换颜色空间,默认为-1不执行转换

  • mean:[float] 转换为float的归一化的均值,默认为空不转换为float

  • norm:[float] 转换为float的归一化的标准差,默认为空不转换为float

  • 返回:放缩后的图像

    返回类型:类型为 Var

    >>> img = cv.imread('cat.jpg')
    >>> img = cv.resize(img, [100, 100])
    >>> cv.imwrite('resize.jpg', img)
    

    warpAffine(src, M, dsize, |flag, borderMode, borderValue, code, mean, norm)

    作用等同与 OpenCVGeometric Image Transformations 模块的warpAffine 函数,对一个图像应用仿射变换。

    该函数在兼容OpenCV函数的基础上,额外增加了3个参数可选参数:code, mean, norm可以额外完成cvtColor和typeas的功能

  • src:Var 输入的图像

  • dsize:tuple 放缩后的大小

  • interpolation:int 放缩的插值方法,默认为cv.INTER_LINEAR

  • borderMode:int 边界模式,默认为cv.BORDER_CONSTANT

  • borderValue:int 当边界模式为 cv.BORDER_CONSTANT 时设定的值,默认为0

  • code:int 可以在缩放时转换颜色空间,默认为-1不执行转换

  • mean:[float] 转换为float的归一化的均值,默认为空不转换为float

  • norm:[float] 转换为float的归一化的标准差,默认为空不转换为float

  • 返回:仿射变换的图像

    返回类型:Var

    >>> src = [50.0, 50.0, 200.0, 50.0, 50.0, 200.0, 125.0, 222.0]
    >>> dst = [10.0, 100.0, 200.0, 20.0, 100.0, 250.0, 200.0, 300.0]
    >>> transform = cv.getAffineTransform(src, dst)
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.warpAffine(img, transform, [300, 330])
    >>> cv.imwrite('warpAffine.jpg', img)
    

    warpPerspective(src, M, dsize, flag, borderMode, borderValue)

    作用等同与 OpenCVGeometric Image Transformations 模块的warpPerspective 函数,对一个图像应用透视变换。

  • src:Var 输入的图像

  • dsize:tuple 放缩后的大小

  • interpolation:int 放缩的插值方法,默认为cv.INTER_LINEAR

  • borderMode:int 边界模式,默认为cv.BORDER_CONSTANT

  • borderValue:int 当边界模式为 cv.BORDER_CONSTANT 时设定的值,默认为0

  • 返回:透视变换的图像

    返回类型:Var

    >>> src = [50.0, 50.0, 200.0, 50.0, 50.0, 200.0, 125.0, 222.0]
    >>> dst = [10.0, 100.0, 200.0, 20.0, 100.0, 250.0, 200.0, 300.0]
    >>> transform = cv.getPerspectiveTransform(src, dst)
    >>> img = cv.imread('cat.jpg')
    >>> img = cv.warpPerspective(img, transform, [500, 333])
    >>> cv.imwrite('warpPerspective.jpg', img)
    

    adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)

    作用等同与 OpenCVMiscellaneous Image Transformations 模块的adaptiveThreshold 函数,对图像逐像素进行自适应阈值变化,可以将使用此函数将图像变成二值图像。

  • src:Var 输入的图像

  • maxValue:float 阈值的最大值

  • adaptiveMethod:int 自适应方法,如:cv.ADAPTIVE_THRESH_MEAN_C

  • thresholdType:int 阈值变化的类型,如:cv.THRESH_BINARY

  • blockSize:int 计算阈值时取邻域的大小,如:3,5,7等

  • C:float

  • 返回:阈值变化后的图像

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> img = cv.adaptiveThreshold(img, 50, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 5, 2)
    >>> cv.imwrite('adaptiveThreshold.jpg', img)
    

    blendLinear(src1, src2, weight1, weight2)

    作用等同与 OpenCVMiscellaneous Image Transformations 模块的blendLinear 函数,对两幅图像进行线性混合。

  • src1:Var 输入的图像

  • src2:Var 输入的图像

  • weight1:Var src1 计算的叠加权重

  • weight2:Var src2 计算的叠加权重

  • 返回:混合后的图像。

    返回类型:Var

    >>> src1 = np.array([[2.0, 3.0], [1.0, 1.0]])
    >>> src2 = np.array
    
    
    
    
        
    ([[0.0, 1.0], [1.0, 1.0]])
    >>> weight1 = np.array([[1.0, 2.0], [1.5, 1.5]])
    >>> weight2 = np.array([[0.1, 0.5], [0.2, 0.3]])
    >>> cv.blendLinear(src1, src2, weight1, weight2)
    array([[1.8181652 , 2.5999894 ],
           [0.9999941 , 0.99999446]], dtype=float32)
    

    threshold(src, thresh, maxval, type)

    作用等同与 OpenCVMiscellaneous Image Transformations 模块的threshold 函数,对图像逐像素进行阈值变化,可以将使用此函数将图像变成二值图像,比如在寻找轮廓时(findContours)可以使用该函数。

  • src:Var 输入的图像

  • thresh:float 阈值

  • maxval:float 阈值的最大值

  • type:int 阈值变化的类型,默认为cv.THRESH_BINARY

  • >>> img = cv.imread('cat.jpg')
    >>> img = img.astype(np.float32)
    >>> img = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
    >>> img = img.astype(np.uint8)
    >>> cv.imwrite('threshold.jpg', img)
    

    findContours(image, mode, method, offset)

    作用等同与 OpenCVStructural Analysis and Shape Descriptors模块的findContours 函数,对二值图像进行轮廓查找,查找得到的结果可以用作contourAreafillPolydrawContours的参数使用。

    注意:该实现未计算hierarchy信息

  • image:Var 输入的图像

  • mode:int 轮廓查找的模式,默认为cv.RETR_EXTERNAL

  • method:int 轮廓查找的方法,默认为cv.CHAIN_APPROX_SIMPLE

  • offset:tuple 轮廓查找的偏移量,默认为(0, 0)

  • 返回:tuple的第一个元素为找到的轮廓像素,类型为list of Var,第二个值为兼容opencv的值,本函数未实现。

    返回类型:tuple

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    ([array([[[143, 294]],
             [[144, 295]]], dtype=int32),
      array([[[304,   1]],
             [[309,   1]]], dtype=int32)], 'no hierarchy')
    

    contourArea(points, oriented)

    作用等同与 OpenCVStructural Analysis and Shape Descriptors模块的contourArea 函数,计算轮廓的面积。

  • points:Var 轮廓像素

  • oriented:bool 是否计算有向面积,默认为False

  • 返回:轮廓的面积

    返回类型:float

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    >>> cv.contourArea(contours[0], False)
    

    convexHull(points, clockwise, returnPoints)

    作用等同与 OpenCVStructural Analysis and Shape Descriptors模块的convexHull 函数,计算点集的凸包。

  • points:Var 轮廓像素

  • clockwise:bool 是否按顺时针方向计算凸包,默认为False

  • returnPoints:bool 是否返回凸包的点集,默认为True

  • 返回:凸包的点集

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    >>> cv.convexHull(contours[0])
    array([[[147, 295]],
           [[147, 298]],
           [[146, 299]],
           [[143, 298]],
           [[142, 297]],
           [[142, 296]],
           [[143, 294]]], dtype=int32)
    

    minAreaRect(points)

    作用等同与 OpenCVStructural Analysis and Shape Descriptors模块的minAreaRect 函数,计算点集的最小外接矩形。

  • points:Var 轮廓像素

  • 返回:最小外接矩形的中心点坐标,长宽,旋转角度

    返回类型:tuple

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    >>> cv.minAreaRect(contours[0])
    ((144.61766052246094, 296.5294494628906), (5.3357834815979, 4.123105525970459), 14.03624439239502)
    

    boundingRect(points)

    作用等同与 OpenCVStructural Analysis and Shape Descriptors模块的boundingRect 函数,计算点集的最小外接矩形。

  • points:Var 轮廓像素

  • 返回:最小外接矩形的中心点坐标,长宽

    返回类型:tuple

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    >>> cv.boundingRect(contours[0])
    [142, 294, 6, 6]
    

    connectedComponentsWithStats(image, connectivity)

    作用等同与 OpenCVConnected Components模块的connectedComponentsWithStats 函数,计算图像的连通域。

  • image:Var 图像

  • connectivity:int 连通域的连通性,默认为8

  • 返回:连通域的数量,连通域的标签,每个标签的统计输出,每个标签的质心输出

    返回类型:tuple

    >>> img = cv.imread('cat.jpg')
    >>> cv.connectedComponentsWithStats(img)
    (2, array([[[[1], ..., [1]], ..., [[1], ..., [1]]]], dtype=int32),
        array([[213, 60, 262, 52, 3], [0, 0, 480, 360, 172797]], dtype=int32),
        array([[386., 77.333336], [239.49745, 179.50177]], dtype=float32))
    

    boxPoints(box)

    作用等同与 OpenCVStructural Analysis and Shape Descriptors模块的boxPoints 函数,计算矩形的四个顶点坐标。

  • box:tuple 矩形的中心点坐标,长宽,旋转角度,参考 minAreaRect 函数的返回值

  • 返回:四个顶点坐标

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    >>> cv.boxPoints(cv.minAreaRect(contours[0]))
    array([[141.52942, 297.8824 ],
           [142.52942, 293.8824 ],
           [147.7059 , 295.1765 ],
           [146.7059 , 299.1765 ]], dtype=float32)
    

    line(img, pt1, pt2, color, thickness, lineType, shift)

    作用等同与 OpenCVDrawing Functions 模块的line 函数,绘制从第一个点指向第二个点的直线。

    该函数为 in-replace,直接作用于原图

  • img:Var 代表需要绘制线条的图像

  • pt1:tuple 线条绘制的起点(x, y)

  • pt2:tuple 线条绘制的终点(x, y)

  • color:tuple 线条绘制的颜色(b, g, r, a)

  • thickness:int 线的粗细,默认为1

  • lineType:int 线条绘制的方式,默认为cv.LINE_8

  • shift:int 坐标小数点向前移动的位数(缩小10倍数),默认为0

  • 返回:None

    返回类型:None

    >>> img = cv.imread('cat.jpg')
    >>> cv.line(img, (10, 10), (100, 100), (255, 0, 0, 0), 5)
    >>> cv.imwrite('line.jpg', img)
    

    arrowedLine(img, pt1, pt2, color, thickness, lineType, shift, tipLength)

    作用等同与 OpenCVDrawing Functions 模块的arrowedLine 函数,绘制从第一个点指向第二个点的箭头段。

    该函数为 in-replace,直接作用于原图

  • img:Var 代表需要绘制箭头的图像

  • pt1:tuple 箭头绘制的起点(x, y)

  • pt2:tuple 箭头绘制的终点(x, y)

  • color:tuple 箭头绘制的颜色(b, g, r, a)

  • thickness:int 箭头的粗细,默认为1

  • lineType:int 箭头绘制的方式,默认为cv.LINE_8

  • shift:int 坐标小数点向前移动的位数(缩小10倍数),默认为0

  • tipLength:float 箭头部分与直线长度的百分比,默认为0.1

  • 返回:None

    返回类型:None

    >>> img = cv.imread('cat.jpg')
    >>> cv.arrowedLine(img, (10, 10), (100, 100), (255, 0, 0, 0), 5)
    >>> cv.imwrite('arrowedLine.jpg', img)
    

    circle(img, center, radius, color, thickness, lineType, shift)

    作用等同与 OpenCVDrawing Functions 模块的circle 函数,绘制一个圆。

    该函数为 in-replace,直接作用于原图

  • img:Var 代表需要绘制圆的图像

  • center:tuple 圆的中心点(x, y)

  • radius:int 圆的半径大小

  • color:tuple 圆绘制的颜色(b, g, r, a)

  • thickness:int 圆的粗细,默认为1

  • lineType:int 圆绘制的方式,默认为cv.LINE_8

  • shift:int 坐标小数点向前移动的位数(缩小10倍数),默认为0

  • 返回:None

    返回类型:None

    >>> img = cv.imread('cat.jpg')
    >>> cv.circle(img, (100, 100), 5, (255, 0, 0, 0), 5)
    >>> cv.circle(img, (
    
    
    
    
        
    100, 100), 50, (0, 0, 255, 0), 5)
    >>> cv.imwrite('circle.jpg', img)
    

    rectangle(src, pt1, pt2, color, thickness, lineType, shift)

    作用等同与 OpenCVDrawing Functions 模块的rectangle 函数,绘制一个矩形。

    该函数为 in-replace,直接作用于原图

  • img:Var 代表需要绘制圆的图像

  • pt1:tuple 矩形的一个顶(x, y)

  • pt2:tuple 矩形的另一个顶(x, y)

  • color:tuple 矩形绘制的颜色(b, g, r, a)

  • thickness:int 矩形的粗细,默认为1

  • lineType:int 矩形绘制的方式,默认为cv.LINE_8

  • shift:int 坐标小数点向前移动的位数(缩小10倍数),默认为0

  • 返回:None

    返回类型:None

    >>> img = cv.imread('cat.jpg')
    >>> cv.rectangle(img, (50, 50), (150, 150), (255, 0, 0, 0), 5)
    >>> cv.rectangle(img, (100, 100), (200, 200), (0, 0, 255, 0), 5)
    >>> cv.imwrite('rectangle.jpg', img)
    

    drawContours(img, contours, contourIdx, color, thickness, lineType)

    作用等同与 OpenCVDrawing Functions 模块的drawContours 函数,绘制轮廓边缘或对其填充。

    该函数为 in-replace,直接作用于原图

  • img:Var 代表需要绘制圆的图像

  • contours:[[int]] 其中每一个元素都是一个list,代表一组轮廓点。一组轮廓点中的元素分别代表一个点的 x 或者 y,必须配对

  • contourIdx:int 代表要绘制第几个轮廓组。如果传入负数,绘制所有轮廓组

  • color:tuple 矩形绘制的颜色(b, g, r, a)

  • thickness:int 矩形的粗细,默认为1

  • lineType:int 矩形绘制的方式,默认为cv.LINE_8

  • 返回:None

    返回类型:None

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> gray = gray.astype(np.float32)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> binary = binary.astype(np.uint8)
    >>> contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    >>> cv.drawContours(img, contours, -1, [0, 0, 255])
    >>> cv.imwrite('drawContours.jpg', img)
    

    fillPoly(img, contours, color, lineType, shift, offset)

    作用等同与 OpenCVDrawing Functions 模块的fillPoly 函数,绘制填充多边形。

    该函数为 in-replace,直接作用于原图

  • img:Var 代表需要绘制圆的图像

  • contours:[[int]] 其中每一个元素都是一个list,代表一组轮廓点。一组轮廓点中的元素分别代表一个点的 x 或者 y,必须配对

  • color:tuple 矩形绘制的颜色(b, g, r, a)

  • lineType:int 矩形绘制的方式,默认为cv.LINE_8

  • shift:int 坐标小数点向前移动的位数(缩小10倍数),默认为0

  • offset:tuple 所有点相对轮廓的偏移量,默认为(0, 0)

  • 返回:None

    返回类型:None

    >>> img = cv.imread('cat.jpg')
    >>> gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    >>> gray = gray.astype(np.float32)
    >>> binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
    >>> binary = binary.astype(np.uint8)
    >>> contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    >>> cv.fillPoly(img, contours, [0, 0, 255])
    >>> cv.imwrite('fillPoly.jpg', img)
    

    calcHist(imgs, channels, mask, histSize, ranges, accumulate)

    作用等同与 OpenCVHistograms 模块的calcHist 函数,计算图像的直方图。

  • imgs:[Var] 需要计算的图像

  • channels:[int] 需要计算的通道

  • mask:Var 需要计算的图像的掩码,本函数实现不支持mask

  • histSize:[int] 直方图的大小,如:[256]

  • ranges:[float] 直方图的范围,如:[0., 256.]

  • accumulate:bool 是否累加,默认为False 本函数实现不支持累加

  • 返回:计算得到的直方图

    返回类型:Var

    >>> img = cv.imread('cat.jpg')
    >>> cv.calcHist([img], [0], None, [256], [0., 256.])
    array([ 9.,    5.,   13.,   25., ..., 41.,   74.,   41.,  173.], dtype=float32)
    
  •