图片放大、平移、旋转、透视操作

    1. import cv2 as cv
    2. import numpy as np
    3. from matplotlib import pyplot as plot
    4. def resize():
    5. img = cv.imread("images/ly.jpg")
    6. # 缩放
    7. # cv.INTER_CUBIC放大
    8. # small = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_LINEAR)
    9. # big = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)
    10. # 等价
    11. height, width = img.shape[:2]
    12. size = (int(width * 0.5), int(height * 0.4))
    13. # 缩小
    14. small = cv.resize(img, size, interpolation=cv.INTER_AREA)
    15. # 放大
    16. big = cv.resize(img, (2 * width, 2 * height), interpolation=cv.INTER_CUBIC)
    17. cv.imshow("small", small)
    18. cv.imshow("big", big)
    19. cv.waitKey(0)
    20. cv.destroyAllWindows()
    21. def warpFfile():
    22. # 灰色读取
    23. img = cv.imread("images/ly.jpg", cv.IMREAD_GRAYSCALE)
    24. # 获取行跟列
    25. plot.subplot(221)
    26. plot.imshow(img)
    27. width, height = img.shape
    28. # 第一个参数是100 100是向x偏移 100 50是向y偏移50
    29. M = np.float32([[1, 0, 100], [0, 1, 50]])
    30. dst = cv.warpAffine(img, M, (width, height))
    31. plot.subplot(222)
    32. plot.imshow(img)
    33. plot.subplot(223)
    34. plot.imshow(dst)
    35. plot.show()
    36. # 旋转
    37. def RotationMatrix():
    38. img = cv.imread("images/ly.jpg", 0)
    39. width, height = img.shape
    40. M = cv.getRotationMatrix2D(((width - 1) / 2.0, (height - 1) / 2.0), 90, 1)
    41. dst = cv.warpAffine(img, M, (width, height))
    42. cv.imshow("test", dst)
    43. cv.waitKey(0)
    44. cv.destroyAllWindows()
    45. # 透视
    46. def toushi():
    47. # 读取图片
    48. img = cv.imread("images/ly.jpg")
    49. image2 = cv.cvtColor(img, cv.COLOR_RGB2BGR)
    50. plot.imshow(image2)
    51. # cv.waitKey(0)
    52. # cv.destroyAllWindows()
    53. plot.show()
    54. if __name__ == '__main__':
    55. # warpFfile()
    56. # RotationMatrix()
    57. toushi()
    58. import cv2 as cv
    59. import numpy as np
    60. from matplotlib import pyplot as plot
    61. def resize():
    62. img = cv.imread("images/ly.jpg")
    63. # 缩放
    64. # cv.INTER_CUBIC放大
    65. # small = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_LINEAR)
    66. # big = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)
    67. # 等价
    68. height, width = img.shape[:2]
    69. size = (int(width * 0.5), int(height * 0.4))
    70. # 缩小
    71. small = cv.resize(img, size, interpolation=cv.INTER_AREA)
    72. # 放大
    73. big = cv.resize(img, (2 * width, 2 * height), interpolation=cv.INTER_CUBIC)
    74. cv.imshow("small", small)
    75. cv.imshow("big", big)
    76. cv.waitKey(0)
    77. cv.destroyAllWindows()
    78. def warpFfile():
    79. # 灰色读取
    80. img = cv.imread("images/ly.jpg", cv.IMREAD_GRAYSCALE)
    81. # 获取行跟列
    82. plot.subplot(221)
    83. plot.imshow(img)
    84. width, height = img.shape
    85. # 第一个参数是100 100是向x偏移 100 50是向y偏移50
    86. M = np.float32([[1, 0, 100], [0, 1, 50]])
    87. dst = cv.warpAffine(img, M, (width, height))
    88. plot.subplot(222)
    89. plot.imshow(img)
    90. plot.subplot(223)
    91. plot.imshow(dst)
    92. plot.show()
    93. # 旋转
    94. def RotationMatrix():
    95. img = cv.imread("images/ly.jpg", 0)
    96. width, height = img.shape
    97. M = cv.getRotationMatrix2D(((width - 1) / 2.0, (height - 1) / 2.0), 90, 1)
    98. dst = cv.warpAffine(img, M, (width, height))
    99. cv.imshow("test", dst)
    100. cv.waitKey(0)
    101. cv.destroyAllWindows()
    102. # 透视
    103. def toushi():
    104. img = cv.imread('images/ly.jpg', 0)
    105. image2 = cv.cvtColor(img, cv.COLOR_BGR2RGB)
    106. # 左上 右上 左下 右下
    107. pts1 = np.float32([[150, 150], [450, 150], [150, 800], [450, 800]])
    108. # 第一个参数是值 第一个点的展示位置0,0表示左上角 300,0表示右上角依次类推
    109. pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
    110. M = cv.getPerspectiveTransform(pts1, pts2)
    111. dst = cv.warpPerspective(image2, M, (300, 300))
    112. plot.subplot(121), plot.imshow(image2), plot.title('Input')
    113. plot.subplot(122), plot.imshow(dst), plot.title('Output')
    114. plot.show()
    115. if __name__ == '__main__':
    116. # warpFfile()
    117. # RotationMatrix()
    118. toushi()