This commit is contained in:
xixu-me committed 2024-05-09 08:57:32 +08:00
1 parent 603a581a78
commit e65363405b
4 files changed
+132

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimSun"]
img = cv.imread(r"img\alphabet.jpg", cv.IMREAD_GRAYSCALE)
dx = dy = 50
rows, cols = img.shape[:2]
M1 = np.float32([[1, 0, dx], [0, 1, dy]]) # type: ignore
dst1 = cv.warpAffine(img, M1, (cols, rows)) # type: ignore
M2 = cv.getRotationMatrix2D((cols / 2, rows / 2), 30, 1)
dst2 = cv.warpAffine(img, M2, (cols, rows))
M3 = np.float32([[1, 0, 0], [0, -1, rows]]) # type: ignore
dst3 = cv.warpAffine(img, M3, (cols, rows)) # type: ignore
plt.figure()
plt.subplot(141)
plt.imshow(img, cmap="gray")
plt.title("待处理灰度图像")
plt.axis("off")
plt.subplot(142)
plt.imshow(dst1, cmap="gray")
plt.title("平移变换")
plt.axis("off")
plt.subplot(143)
plt.imshow(dst2, cmap="gray")
plt.title("旋转 30 度")
plt.axis("off")
plt.subplot(144)
plt.imshow(dst3, cmap="gray")
plt.title("垂直镜像")
plt.axis("off")
plt.tight_layout()
plt.show()
+31
View File
@@ -0,0 +1,31 @@
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimSun"]
img0 = cv.imread(r"img\alphabet.jpg", cv.IMREAD_GRAYSCALE)
img1 = cv.copyMakeBorder(img0, 50, 50, 50, 50, cv.BORDER_CONSTANT, value=0) # type: ignore
pts1 = np.float32([[50, 50], [200, 50], [50, 200]]) # type: ignore
pts2 = np.float32([[10, 70], [160, 50], [40, 220]]) # type: ignore
rows, cols = img1.shape[:2]
M1 = cv.getAffineTransform(pts1, pts2) # type: ignore
dst1 = cv.warpAffine(img1, M1, (cols, rows))
M2 = cv.getAffineTransform(pts2, pts1) # type: ignore
dst2 = cv.warpAffine(dst1, M2, (cols, rows))
plt.figure()
plt.subplot(131)
plt.imshow(img1, cmap="gray")
plt.title("扩展后的图像")
plt.axis("off")
plt.subplot(132)
plt.imshow(dst1, cmap="gray")
plt.title("仿射变换图像")
plt.axis("off")
plt.subplot(133)
plt.imshow(dst2, cmap="gray")
plt.title("放射校正图像")
plt.axis("off")
plt.tight_layout()
plt.show()
+32
View File
@@ -0,0 +1,32 @@
import cv2 as cv
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimSun"]
img0 = cv.imread(r"img\alphabet.jpg")
imgT = cv.cvtColor(img0, cv.COLOR_BGR2GRAY)
cv.rectangle(img0, (160, 140), (190, 170), (255, 0, 0), 3)
img = imgT[140:170, 160:190]
img1 = cv.resize(img, None, fx=10, fy=10, interpolation=cv.INTER_NEAREST)
img2 = cv.resize(img, None, fx=10, fy=10, interpolation=cv.INTER_LINEAR)
img3 = cv.resize(img, None, fx=10, fy=10, interpolation=cv.INTER_CUBIC)
plt.figure()
plt.subplot(141)
plt.imshow(img0, cmap="gray")
plt.title("原图像")
plt.axis("off")
plt.subplot(142)
plt.imshow(img1, cmap="gray")
plt.title("最近邻插值")
plt.axis("off")
plt.subplot(143)
plt.imshow(img2, cmap="gray")
plt.title("双线性插值")
plt.axis("off")
plt.subplot(144)
plt.imshow(img3, cmap="gray")
plt.title("双三次插值")
plt.axis("off")
plt.tight_layout()
plt.show()
+31
View File
@@ -0,0 +1,31 @@
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimSun"]
img0 = cv.imread(r"img\alphabet.jpg", cv.IMREAD_GRAYSCALE)
img1 = cv.copyMakeBorder(img0, 20, 20, 20, 20, cv.BORDER_CONSTANT, value=0) # type: ignore
pts1 = np.float32([[20, 20], [220, 20], [20, 220]]) # type: ignore
pts2 = np.float32([[10, 70], [160, 50], [40, 220]]) # type: ignore
rows, cols = img1.shape[:2]
M1 = cv.getAffineTransform(pts1, pts2) # type: ignore
dst1 = cv.warpAffine(img1, M1, (cols, rows))
M2 = cv.getAffineTransform(pts2, pts1) # type: ignore
dst2 = cv.warpAffine(dst1, M2, (cols, rows))
plt.figure()
plt.subplot(131)
plt.imshow(img1, cmap="gray")
plt.title("扩展后的图像")
plt.axis("off")
plt.subplot(132)
plt.imshow(dst1, cmap="gray")
plt.title("仿射变换图像")
plt.axis("off")
plt.subplot(133)
plt.imshow(dst2, cmap="gray")
plt.title("放射校正图像")
plt.axis("off")
plt.tight_layout()
plt.show()