美文网首页
30. 图像移位

30. 图像移位

作者: 十里江城 | 来源:发表于2019-11-12 15:08 被阅读0次

    通过api、算法原理和源码解释图像移位:

    • api实现
    # api 算法 源码
    import cv2
    import numpy as np
    img = cv2.imread('face.jpg', 1)
    cv2.imshow('src', img)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    # 移位矩阵 水平(width)移动100像素 竖直y(height)方向移动200像素
    matShift = np.float32([[1, 0, 100], [0, 1, 200]])  # 2*3
    print('hello')
    # 放射变换之wrapAffine
    dst = cv2.warpAffine(img, matShift, (height, width))
    cv2.imshow('dst', dst)
    cv2.waitKey(0)
     
    

    结果如下:


    image.png
    • 算法原理
      23 -> 22 and 21
      [1, 0] [0, 1] A
      [[100], [200]] B
      xy C
      A
      C+B = [[x * 1 + 0 * y], [0 * x + 1 * y]] + [[100], [200]]
      = [[x +100], [y + 200]]
      (10, 20) -> (110, 220)

    • 源码实现

    import cv2
    import numpy as np
    img = cv2.imread('face.jpg', 1)
    cv2.imshow('src', img)
    imgInfo = img.shape
    dst = np.zeros((img.shape), np.uint8)
    height = imgInfo[0]
    width = imgInfo[1]
    # 水平右移100像素 竖直下移200像素
    for i in range(0, height - 200):
        for j in range(0, width - 100):
            dst[i + 200, j + 100] = img[i, j]
    cv2.imshow('dst', dst)
    cv2.waitKey(0)
    

    结果如下:


    image.png

    相关文章

      网友评论

          本文标题:30. 图像移位

          本文链接:https://www.haomeiwen.com/subject/dkcqictx.html