文章目录
- 图像的放大与缩小
- 缩放算法
- 题目
- 放大
- 图像的翻转
- 图像的旋转
- 仿射变换之图像平移
- 仿射变换之获取变换矩阵
- 仿射变换之变换矩阵之二
- OpenCV透视变换
图像的放大与缩小
缩放算法
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//4.jpg')
# (600, 480, 3)new_img = cv2.resize(img, (300, 240))
new_img2 = cv2.resize(img, (300, 240), interpolation=cv2.INTER_AREA)cv2.imshow('new_img', new_img)
cv2.imshow('img', img)
cv2.imshow('new_img2', new_img2)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//4.jpg')
# (600, 480, 3)new_img = cv2.resize(img, None, fx=0.3, fy=0.3)
new_img2 = cv2.resize(img, (300, 240), interpolation=cv2.INTER_AREA)cv2.imshow('new_img', new_img)
cv2.imshow('img', img)
cv2.imshow('new_img2', new_img2)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
题目
当然选A,img第一个坐标表示高,第二个坐标表示宽,高逐渐缩小,宽逐渐增大!
# -*- coding: utf-8 -*-
import cv2
import numpy as npif __name__ == '__main__':img = cv2.imread('ball.jpg', cv2.IMREAD_COLOR)start = [493, 594]end = [112, 213]ball = img[start[0]:start[1], end[0]:end[1]]x_step = 101y_step = 10cv2.imshow('img', img)for i in range(-1, 4):# print(i)x_offset = x_step*iy_offset = y_step*i# 指的是高宽img[start[0]-y_offset:start[1]-y_offset, end[0]+x_offset:end[1]+x_offset] = ballcv2.imshow("ball_continue", img)key = cv2.waitKey(0) & 0xffif key == ord('q'):cv2.destroyAllWindows()
放大
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//4.jpg')
# (600, 480, 3)new_img = cv2.resize(img, None, fx=1.3, fy=1.3)
new_img2 = cv2.resize(img, (300, 240), interpolation=cv2.INTER_AREA)cv2.imshow('new_img', new_img)
cv2.imshow('img', img)
cv2.imshow('new_img2', new_img2)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
图像的翻转
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//4.jpg')
# 上下翻转
img2 = cv2.flip(img, 0)
# 左右翻转
img3 = cv2.flip(img, 1)
# 上下左右翻转
img4 = cv2.flip(img, -1)cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)
cv2.imshow('img4', img4)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
图像的旋转
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//4.jpg')img2 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
img3 = cv2.rotate(img, cv2.ROTATE_180)
img4 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)
cv2.imshow('img4', img4)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
仿射变换之图像平移
详细见官方文档
第一行的第三列是x轴偏移量,第二行第三列是y轴偏移量
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//4.jpg')
w, h, ch = img.shape
M = np.float32([[1, 0, 100], [0, 1, 20]])img2 = cv2.warpAffine(img, M, (h, w))cv2.imshow('img', img)
cv2.imshow('img2', img2)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
仿射变换之获取变换矩阵
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//10.jpg')
w, h, ch = img.shape
# M = np.float32([[1, 0, 100], [0, 1, 20]])# 旋转的角度为逆时针
M = cv2.getRotationMatrix2D((100, 100), 30, 1)
M2 = cv2.getRotationMatrix2D((h / 2, w / 2), 60, 0.6)# 如果想改变新图像尺寸,需修改dsize
img2 = cv2.warpAffine(img, M, (h, w))
img3 = cv2.warpAffine(img, M2, (h, w))cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
仿射变换之变换矩阵之二
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//10.jpg')
w, h, ch = img.shape
# M = np.float32([[1, 0, 100], [0, 1, 20]])# 旋转的角度为逆时针
# M = cv2.getRotationMatrix2D((100, 100), 30, 1)
# M2 = cv2.getRotationMatrix2D((h / 2, w / 2), 60, 0.6)src = np.float32([[100, 200], [300, 200], [100, 400]])
dst = np.float32([[50, 100], [200, 400], [90, 300]])M = cv2.getAffineTransform(src, dst)# 如果想改变新图像尺寸,需修改dsize
img2 = cv2.warpAffine(img, M, (h, w))
# img3 = cv2.warpAffine(img, M2, (h, w))cv2.imshow('img', img)
cv2.imshow('img2', img2)
# cv2.imshow('img3', img3)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//10.jpg')
w, h, ch = img.shape
# M = np.float32([[1, 0, 100], [0, 1, 20]])# 旋转的角度为逆时针
# M = cv2.getRotationMatrix2D((100, 100), 30, 1)
# M2 = cv2.getRotationMatrix2D((h / 2, w / 2), 60, 0.6)src = np.float32([[100, 200], [300, 200], [100, 400]])
dst = np.float32([[50, 100], [200, 400], [60, 500]])M = cv2.getAffineTransform(src, dst)# 如果想改变新图像尺寸,需修改dsize
img2 = cv2.warpAffine(img, M, (h, w))
# img3 = cv2.warpAffine(img, M2, (h, w))cv2.imshow('img', img)
cv2.imshow('img2', img2)
# cv2.imshow('img3', img3)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
OpenCV透视变换
# -*- coding: utf-8 -*-
import cv2
import numpy as npcv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('img2', cv2.WINDOW_NORMAL)
img = cv2.imread('perspective.jpeg')src = np.float32([[100, 1000], [2100, 1000], [0, 4000], [2500, 3900]])
dst = np.float32([[0, 0], [2300, 0], [0, 3000], [2300, 3000]])
M = cv2.getPerspectiveTransform(src, dst)img2 = cv2.warpPerspective(img, M, (2300, 3000))cv2.imshow('img', img)
cv2.imshow('img2', img2)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!