import cv2 as cv
import numpy as np
import math
class Pointcloud:
def getcalidata(self):
yamlpath = “D:/20241018/calibration.yml”
cv_file = cv.FileStorage(yamlpath, cv.FILE_STORAGE_READ) # 实例化一个 FileStorage
cam_K = cv_file.getNode(“cam_K”).mat() # getNode 成员函数将获得一个 FileNode 类型的对象,mat() 函数将 FileNode 转换为矩阵
proj_K = cv_file.getNode(“proj_K”).mat()
R = cv_file.getNode(“R”).mat() # R,T为相机到投影仪的矩阵c to p
T = cv_file.getNode(“T”).mat()
print(cam_K, type(cam_K))
print(proj_K, type(proj_K))
print(R, type(R))
print(T, type(T))
return cam_K, proj_K, R, T
def getdata(self, c_width: int = 2448, c_height: int = 2048, p_width: int = 1280, p_height: int = 720):
cam_K, proj_K, R, T = self.getcalidata()
RT = np.hstack((R.T, -np.dot(R.T, T)))
matrix1 = np.zeros((3, 4), dtype=np.float64)
matrix1[:3, :3] = cam_K
matrix2 = np.dot(proj_K, RT)
full_white_img = cv.imread("D:\\20250119\\ball\\full_white_image.png", cv.IMREAD_GRAYSCALE)
full_black_img = cv.imread("D:\\20250119\\ball\\full_black_image.png", cv.IMREAD_GRAYSCALE)
x_map = cv.imread("D:\\20250119\\ball\\ver_Absolute_pha.png", -1)
y_map = cv.imread("D:\\20250119\\ball\\her_Absolute_pha.png", -1)
mask = full_white_img - full_black_img
_, binary_mask = cv.threshold(mask, 1, 255, cv.THRESH_BINARY)
# Find non-zero points in the mask
proj_points1 = cv.findNonZero(binary_mask)
proj_points1 = proj_points1.reshape(-1, 2) # Convert to Nx2 format
proj_points2 = np.zeros((proj_points1.shape[0], 2), dtype=np.float32)
for p in range(proj_points1.shape[0]):
proj_points2[p, 0] = x_map[proj_points1[p, 1], proj_points1[p, 0]]
proj_points2[p, 1] = y_map[proj_points1[p, 1], proj_points1[p, 0]]
print(proj_points1.shape)
print(proj_points2.shape)
# Get the projected points
points4D = cv.triangulatePoints(matrix1, matrix2, proj_points1.T, proj_points2.T)
points4D /= points4D[3]
print(points4D)
if name == “main”:
p = Pointcloud()
p.getdata()
Traceback (most recent call last):
File “D:\Anaconda3\Lib\site-packages\IPython\core\interactiveshell.py”, line 3526, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
runfile(‘D:\PycharmProjects\FourStepPhaseShifting-master\src\python\restruct.py’, wdir=‘D:\PycharmProjects\FourStepPhaseShifting-master\src\python’)
File “D:\Pycharm\PyCharm 2023.2.5\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py”, line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Pycharm\PyCharm 2023.2.5\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+“\n”, file, ‘exec’), glob, loc)
File “D:\PycharmProjects\FourStepPhaseShifting-master\src\python\restruct.py”, line 50, in
p.getdata()
File “D:\PycharmProjects\FourStepPhaseShifting-master\src\python\restruct.py”, line 43, in getdata
points4D = cv.triangulatePoints(matrix1, matrix2, proj_points1.T, proj_points2.T)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: Unknown C++ exception from OpenCV code