You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

62 lines
1.8 KiB

import cv2
import numpy as np
import 标靶识别 as ie
def perspective_transformation(image):
if image is None:
print("Error: Unable to load image.")
exit()
# 定义源图像中的四个点
src_points = np.float32([
[4, 16], # 左上角
[795, 14], # 右上角
[1027, 736], # 右下角
[181, 856] # 左下角
])
# 定义目标图像中的四个点 # 1050 * 900
dst_points = np.float32([
[4, 16], # 左上角
[795, 14], # 右上角
[795, 850], # 右下角
[4, 850] # 左下角
])
# 计算透视变换矩阵
M = cv2.getPerspectiveTransform(src_points, dst_points)
print(f"原矩阵={M}")
# 逆矩阵
inverse_matrix = np.linalg.inv(M)
print(f"逆矩阵={inverse_matrix}")
# 应用透视变换
transformed_image = cv2.warpPerspective(image, M, (1050, 900))
# 应用透视变换
inv_transformed_image = cv2.warpPerspective(transformed_image, inverse_matrix, (1050, 900))
# 显示原始图像和变换后的图像
cv2.imshow("Original Image", image)
cv2.imshow("Trans Image", transformed_image)
cv2.imshow("iTrans Image", inv_transformed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def sub_img():
# 读取图像
image = cv2.imread("images/target/need_trans.jpg")
if image is None:
print("Error: Unable to load image.")
exit()
# 1050 * 900
startPoint = [550, 700]
endPoint = [1600, 1600]
# 绘制标靶区域
# 检测
subImg = ie.extract_sub_image(image, startPoint, endPoint)
# cv2.imshow("subImg", subImg)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return subImg
if __name__ == '__main__':
img=sub_img()
perspective_transformation(img)