HPP_UIUC_MP2: basic matrix multiplication-----BlockDIM!

本文介绍了一个在CUDA编程中遇到的问题:当DimBlock尺寸设置过大时,会导致输出的C矩阵全为零。通过调整DimBlock的大小到合适的范围(例如8x8),可以有效解决该问题并获得正确的计算结果。
部署运行你感兴趣的模型镜像

这次作业有个地方搞的我输出的C矩阵一直都是全000000.


然后发现原来是DimBlock设置的太大了。

//@@ Initialize the grid and block dimensions here
    dim3 DimBlock(8, 8, 1); // if the threads each block is too large, say
			    // 32, then the C output will be all 000000000
	                    // so fail!!!!
    dim3 DimGrid((numCColumns - 1) / DimBlock.x + 1,
                 (numCRows - 1) / DimBlock.y + 1, 1);

这样就可以得到正确结果了。

"level":"Trace",
"message":"The dimensions of B are 64 x 64",
"file":"F:/1MOOC_SourceCode/HPP/libwb/skel/mp2.cu",
"function":"main",
"line":65,
"time":25975425239252
}]
}
,
"solution_exists": true,
"solution":{
"correctq": true,
"message": "Solution is correct."
}
}

F:\1MOOC_SourceCode\HPP\libwb\build\Debug>


您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

%YAML:1.0 calibrationDate: "Mon Apr 01 16:14:21 2019\n" # flags: fix_k3 + fix_principal_point flags: 132 imageSize: - 768 - 576 cameraMatrix: !!opencv-matrix rows: 3 cols: 3 dt: d data: [ 4.6874604679611883e+002, 0., 3.8350000000000000e+002, 0., 4.6879439393265602e+002, 2.8750000000000000e+002, 0., 0., 1. ] distortionCoefficients: !!opencv-matrix rows: 1 cols: 5 dt: d data: [ -2.5342359872429532e-001, 8.5600118766813921e-002, 5.6244265779694345e-004, -7.8384257012846525e-004, 0. ] nImages: 15 rotaionMatrix_0: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 2.0675728693835911e+000, 2.1075107474976873e+000, 2.9336165222510824e-001 ] rotaionMatrix_1: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 1.9940114749521032e+000, 2.3974858804729884e+000, -1.8750405701889838e-001 ] rotaionMatrix_2: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -1.8508345829987935e-001, -3.0721320273878070e+000, 2.0099928906234399e-001 ] rotaionMatrix_3: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 1.8561798916226813e+000, 2.2574253657276580e+000, 1.8075071703156042e-002 ] rotaionMatrix_4: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 1.9143330893475794e+000, 2.2654143145437464e+000, 5.5136561232423087e-001 ] rotaionMatrix_5: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -1.2176711916278464e-001, -2.8387954340707187e+000, -1.7867568550508553e-001 ] rotaionMatrix_6: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 2.1455070420741249e+000, 2.1214948680266836e+000, -4.6090545899241777e-003 ] rotaionMatrix_7: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 2.0377945599630385e+000, -2.3925292139513301e+000, 4.0630825031139844e-001 ] rotaionMatrix_8: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -2.5437740160710737e-001, -2.8685080517013670e+000, -2.1191323541716614e-001 ] rotaionMatrix_9: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 6.7896166401865587e-001, 2.9203185839292107e+000, 2.1616783717319016e-001 ] rotaionMatrix_10: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 1.4862747025589098e+000, -2.6427103906043348e+000, 6.2730976108590752e-001 ] rotaionMatrix_11: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 4.9097469295410967e-001, 2.5802824180346433e+000, 8.2549111324101287e-001 ] rotaionMatrix_12: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -5.9329273551092010e-001, -2.8406274978108086e+000, -1.2038427172612680e-001 ] rotaionMatrix_13: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 4.4866443913566595e-001, 2.8809831584432062e+000, 2.9157343452658618e-001 ] rotaionMatrix_14: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 4.0925300388373193e-001, 2.3504680059324068e+000, 5.8092187472717938e-001 ] translationMatrix_0: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -2.1688720011873690e+002, -3.8482859993755244e+002, 7.5892212322704984e+002 ] translationMatrix_1: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -7.2598857858311035e+001, -4.1095056756996280e+002, 8.7065484310140323e+002 ] translationMatrix_2: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 3.1108053390777576e+002, -4.4065058470458894e+002, 8.4938948907721942e+002 ] translationMatrix_3: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -9.1224334551975915e+001, -4.2514537231758669e+002, 8.9808378894780935e+002 ] translationMatrix_4: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -6.6207738709959045e+001, -4.0840657182118690e+002, 8.2150062853842633e+002 ] translationMatrix_5: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 3.0835078587362551e+002, -4.1193102193529757e+002, 8.2353802219900638e+002 ] translationMatrix_6: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ -2.9223387581025196e+002, -3.8847217027632860e+002, 1.1263946076136303e+003 ] translationMatrix_7: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 3.5562785476260422e+002, 2.5282460579194407e+002, 1.1991223847570261e+003 ] translationMatrix_8: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 2.1656314827051602e+002, -4.3804713525289219e+002, 8.1633526671164771e+002 ] translationMatrix_9: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 1.2866173732912361e+002, -5.7017025106022118e+002, 1.0717308421936129e+003 ] translationMatrix_10: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 4.5769875864385961e+002, -3.5558068321513403e+001, 1.0858289147247763e+003 ] translationMatrix_11: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 2.0864134034698048e+002, -4.5061460733041241e+002, 9.3960218863939292e+002 ] translationMatrix_12: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 4.4922191591590170e+001, -5.4620924115010405e+002, 1.0262957887395842e+003 ] translationMatrix_13: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 2.6578597130156345e+002, -4.8469186163723572e+002, 9.8283699264293682e+002 ] translationMatrix_14: !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 2.0091674629264978e+002, -5.1570686057689363e+002, 1.0525920807382834e+003 ] 分析一下这个yml文件,并把这个读进 cameraParams这个结构体struct CameraParams { cv::size imageSize; cv::Mat cameraMatrix; Cv::Mat distortionCoefficients; vector<cv::Mat> rotations; vector<cv::Mat> translations; int flags; };
最新发布
07-09
<think>我们有一个结构体CameraParams,它包含以下成员: imageSize, cameraMatrix, distortionCoefficients, rotations, translations 我们有一个由OpenCV生成的YAML文件,该文件通常包含相机的标定参数。 根据OpenCV的文档,当我们使用FileStorage读取YAML文件时,我们可以使用类似Map的方式来读取数据。 步骤: 1. 使用cv::FileStorage打开YAML文件(读取模式)。 2. 读取imageSize:这是一个cv::Size对象,在YAML中通常以两个整数(width和height)存储。 3. 读取cameraMatrix:这是一个3x3的cv::Mat矩阵。 4. 读取distortionCoefficients:这是一个1xN(通常N=5,8,12或14)的cv::Mat矩阵。 5. 读取rotations和translations:这两个通常是多个旋转和平移向量的数组。但在标定文件中,它们可能是多个矩阵(每个视图一个)。注意,在OpenCV的相机标定输出中,通常每个视图对应一个旋转向量(3x1或1x3)和一个平移向量(3x1或1x3)。因此,我们可能需要在结构体中使用std::vector<cv::Mat>来存储它们。 但是,请注意,用户的结构体CameraParams中rotations和translations的具体类型并没有明确说明。我们假设它们可能是向量(每个视图一个向量)的向量。因此,我们定义结构体如下(如果用户没有给出,我们可以这样定义): 假设结构体定义如下: ```cpp struct CameraParams { cv::Size imageSize; cv::Mat cameraMatrix; cv::Mat distortionCoefficients; std::vector<cv::Mat> rotations; // 每个元素是一个旋转向量(3x1) std::vector<cv::Mat> translations; // 每个元素是一个平移向量(3x1) }; ``` 然而,在OpenCV的标定输出中,旋转和平移向量通常以矩阵形式存储,每个视图对应一个向量。在YAML文件中,它们通常被存储为一个序列,每个元素是一个向量。 示例YAML文件(部分): ``` %YAML:1.0 --- image_width: 640 image_height: 480 camera_matrix: !!opencv-matrix rows: 3 cols: 3 dt: d data: [ 6.0000000000000000e+02, 0., 3.2000000000000000e+02, 0., 6.0000000000000000e+02, 2.4000000000000000e+02, 0., 0., 1. ] distortion_coefficients: !!opencv-matrix rows: 1 cols: 5 dt: d data: [ 0., 0., 0., 0., 0. ] rotation_vectors: # 注意:OpenCV的标定函数输出的是旋转向量(Rodrigues向量),而不是旋转矩阵 - !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 0.1, 0.2, 0.3 ] - ... # 可能有多个 translation_vectors: - !!opencv-matrix rows: 3 cols: 1 dt: d data: [ 1.0, 2.0, 3.0 ] - ... # 可能有多个 ``` 注意:上面的YAML结构是假设的,实际OpenCV生成的YAML可能会有所不同。常见的OpenCV标定输出(如使用calibrateCamera函数)会输出如下: - camera_matrix - distortion_coefficients - 然后是每个视图的旋转向量(rotation_vectors)和平移向量(translation_vectors) 因此,我们需要按照文件中的实际节点名称来读取。 读取步骤: 1. 打开文件: cv::FileStorage fs(filename, cv::FileStorage::READ); 2. 读取图像尺寸:注意,OpenCV的标定输出有时会分别存储image_width和image_height,有时会存储在一个image_size节点(包含两个整数)。所以我们需要根据实际文件调整。 如果文件中有"image_size"节点(类型为Size): fs["image_size"] >> imageSize; 或者分别读取宽和高,然后组合: int width, height; fs["image_width"] >> width; fs["image_height"] >> height; imageSize = cv::Size(width, height); 3. 读取相机矩阵和畸变系数: fs["camera_matrix"] >> cameraMatrix; fs["distortion_coefficients"] >> distortionCoefficients; 4. 读取旋转向量和平移向量:它们通常是序列(序列中的每个元素是一个矩阵)。我们可以使用FileNode和FileNodeIterator来遍历。 例如: cv::FileNode rvecsNode = fs["rotation_vectors"]; for (cv::FileNodeIterator it = rvecsNode.begin(); it != rvecsNode.end(); it++) { cv::Mat rvec; *it >> rvec; rotations.push_back(rvec); } // 同样处理平移向量 cv::FileNode tvecsNode = fs["translation_vectors"]; for (cv::FileNodeIterator it = tvecsNode.begin(); it != tvecsNode.end(); it++) { cv::Mat tvec; *it >> tvec; translations.push_back(tvec); } 5. 关闭文件:fs.release(); 但是,请注意:OpenCV的FileStorage在读取序列时,如果序列中的每个元素都是矩阵,我们可以直接使用>>操作符读取到Mat。 然而,我们需要注意,在标定输出中,旋转向量和平移向量都是3x1的矩阵(或1x3,但通常是3x1)。 如果YAML文件中没有直接使用"rotation_vectors"和"translation_vectors"这样的名称,而是其他名称(如"rot_vecs"和"trans_vecs"),则需要根据实际文件调整节点名称。 因此,我们需要根据实际的YAML文件结构来调整节点名称。 示例代码: 假设我们有一个YAML文件,其结构如下(这是OpenCV的calibrateCamera函数输出的典型结构): ``` %YAML:1.0 --- image_width: 640 image_height: 480 camera_matrix: !!opencv-matrix rows: 3 cols: 3 dt: d data: [ ... ] distortion_coefficients: !!opencv-matrix rows: 1 cols: 5 dt: d data: [ ... ] avg_reprojection_error: 0.1 rotation_vectors: - !!opencv-matrix rows: 3 cols: 1 dt: d data: [ ... ] - ... # 多个 translation_vectors: - !!opencv-matrix rows: 3 cols: 1 dt: d data: [ ... ] - ... # 多个 ``` 那么,我们可以这样读取: ```cpp #include <opencv2/opencv.hpp> #include <iostream> struct CameraParams { cv::Size imageSize; cv::Mat cameraMatrix; cv::Mat distortionCoefficients; std::vector<cv::Mat> rotations; std::vector<cv::Mat> translations; }; bool loadCameraParams(const std::string&amp; filename, CameraParams&amp; params) { cv::FileStorage fs(filename, cv::FileStorage::READ); if (!fs.isOpened()) { std::cerr << "Failed to open " << filename << std::endl; return false; } // 读取图像尺寸 int width = 0, height = 0; fs["image_width"] >> width; fs["image_height"] >> height; params.imageSize = cv::Size(width, height); // 读取相机内参和畸变系数 fs["camera_matrix"] >> params.cameraMatrix; fs["distortion_coefficients"] >> params.distortionCoefficients; // 读取旋转向量序列 cv::FileNode rvecsNode = fs["rotation_vectors"]; if (rvecsNode.type() != cv::FileNode::SEQ) { std::cerr << "rotation_vectors is not a sequence!" << std::endl; return false; } for (cv::FileNodeIterator it = rvecsNode.begin(); it != rvecsNode.end(); it++) { cv::Mat rvec; *it >> rvec; params.rotations.push_back(rvec); } // 读取平移向量序列 cv::FileNode tvecsNode = fs["translation_vectors"]; if (tvecsNode.type() != cv::FileNode::SEQ) { std::cerr << "translation_vectors is not a sequence!" << std::endl; return false; } for (cv::FileNodeIterator it = tvecsNode.begin(); it != tvecsNode.end(); it++) { cv::Mat tvec; *it >> tvec; params.translations.push_back(tvec); } fs.release(); return true; } ``` 注意:在实际的OpenCV标定输出中,节点名称可能略有不同。例如,旋转向量节点可能叫"rot_vecs"或"rotation_vectors",平移向量节点可能叫"trans_vecs"或"translation_vectors"。另外,图像尺寸节点也可能直接叫"image_size",如果是这样,我们可以直接读取到一个cv::Size对象: fs["image_size"] >> params.imageSize; 因此,我们需要根据实际YAML文件调整节点名称。 如果YAML文件中没有分别存储宽和高,而是有一个"image_size"节点,那么我们可以这样读取: cv::FileNode sizeNode = fs["image_size"]; if (!sizeNode.empty()) { params.imageSize.width = static_cast<int>(sizeNode[0]); params.imageSize.height = static_cast<int>(sizeNode[1]); } else { // 尝试分别读取 fs["image_width"] >> params.imageSize.width; fs["image_height"] >> params.imageSize.height; } 为了代码的健壮性,我们可以先尝试读取"image_size"节点,如果不存在再分别读取"image_width"和"image_height"。 同样,旋转向量和平移向量的节点名称也可能不同,我们可以尝试几个常见的名称。 因此,我们可以改进代码,使其适应不同的节点名称。 但为了简单起见,我们假设用户知道自己的YAML文件结构,并按照实际节点名称读取。 最后,请确保在读取矩阵时,矩阵的类型(dt)与代码中期望的一致(通常为double,即CV_64F)。在OpenCV的标定中,通常使用双精度浮点数。 如果用户的结构体中的rotations和translations需要是其他类型(比如旋转矩阵而不是旋转向量),则需要在读取后转换。因为OpenCV的calibrateCamera输出的是旋转向量(Rodrigues向量),如果需要旋转矩阵,则使用Rodrigues函数转换。 但根据问题描述,我们只需要读取到结构体,所以保持为旋转向量(cv::Mat)的向量。 总结:我们提供了一个函数loadCameraParams,它从YAML文件中读取数据并填充到CameraParams结构体中。 注意:我们假设YAML文件的结构与上述示例类似。如果实际文件结构不同,需要调整节点名称。 如果用户提供的YAML文件是由OpenCV的FileStorage直接生成的(例如使用calibrateCamera函数并保存结果),那么通常节点名称是固定的,我们可以参考OpenCV文档。 参考文献:OpenCV的FileStorage使用示例和文档[^1][^2]。</think>### 解析 OpenCV YAML 标定文件到 C++ 结构体 要将 OpenCV 生成的 YAML 标定文件解析到 `CameraParams` 结构体,请按照以下步骤操作: #### 1. 定义结构体 ```cpp #include <opencv2/opencv.hpp> #include <vector> struct CameraParams { cv::Size imageSize; // 图像尺寸 cv::Mat cameraMatrix; // 相机内参矩阵 3x3 cv::Mat distortionCoefficients; // 畸变系数 1x5 std::vector<cv::Mat> rotations; // 旋转向量 (每个元素是 3x1 Mat) std::vector<cv::Mat> translations; // 平移向量 (每个元素是 3x1 Mat) }; ``` #### 2. 实现解析函数 ```cpp bool parseCameraParams(const std::string&amp; filePath, CameraParams&amp; params) { cv::FileStorage fs(filePath, cv::FileStorage::READ); if (!fs.isOpened()) { std::cerr << "无法打开文件: " << filePath << std::endl; return false; } // 解析图像尺寸 int width = 0, height = 0; fs["image_width"] >> width; fs["image_height"] >> height; params.imageSize = cv::Size(width, height); // 解析相机内参矩阵 fs["camera_matrix"] >> params.cameraMatrix; // 解析畸变系数 fs["distortion_coefficients"] >> params.distortionCoefficients; // 解析旋转向量序列 cv::FileNode rvecsNode = fs["rotation_vectors"]; for (cv::FileNodeIterator it = rvecsNode.begin(); it != rvecsNode.end(); ++it) { cv::Mat rvec; *it >> rvec; params.rotations.push_back(rvec); } // 解析平移向量序列 cv::FileNode tvecsNode = fs["translation_vectors"]; for (cv::FileNodeIterator it = tvecsNode.begin(); it != tvecsNode.end(); ++it) { cv::Mat tvec; *it >> tvec; params.translations.push_back(tvec); } fs.release(); return true; } ``` #### 3. 使用示例 ```cpp int main() { CameraParams cameraParams; if (parseCameraParams("calibration_data.yaml", cameraParams)) { std::cout << "图像尺寸: " << cameraParams.imageSize << std::endl; std::cout << "相机矩阵:\n" << cameraParams.cameraMatrix << std::endl; std::cout << "畸变系数: " << cameraParams.distortionCoefficients.t() << std::endl; std::cout << "\n旋转向量数量: " << cameraParams.rotations.size() << std::endl; for (size_t i = 0; i < cameraParams.rotations.size(); ++i) { std::cout << "Rvec " << i << ": " << cameraParams.rotations[i].t() << std::endl; } std::cout << "\n平移向量数量: " << cameraParams.translations.size() << std::endl; for (size_t i = 0; i < cameraParams.translations.size(); ++i) { std::cout << "Tvec " << i << ": " << cameraParams.translations[i].t() << std::endl; } } return 0; } ``` #### 4. 关键说明 1. **YAML 文件结构**:确保 YAML 文件包含以下节点: ```yaml image_width: 640 image_height: 480 camera_matrix: !!opencv-matrix rows: 3 cols: 3 dt: d data: [fx, 0, cx, 0, fy, cy, 0, 0, 1] distortion_coefficients: !!opencv-matrix rows: 1 cols: 5 dt: d data: [k1, k2, p1, p2, k3] rotation_vectors: - !!opencv-matrix rows: 3 cols: 1 dt: d data: [rvec1_x, rvec1_y, rvec1_z] - ... translation_vectors: - !!opencv-matrix rows: 3 cols: 1 dt: d data: [tvec1_x, tvec1_y, tvec1_z] - ... ``` 2. **数据类型**:OpenCV 通常使用双精度(`d`)存储标定数据,确保结构体中的 `cv::Mat` 使用 `CV_64F` 类型。 3. **错误处理**:添加检查确保节点存在: ```cpp if (rvecsNode.empty()) { std::cerr << "警告: 未找到旋转向量数据" << std::endl; } ``` #### 5. 常见问题解决 - **节点名称不匹配**:如果 YAML 使用不同节点名(如 `rot_vecs` 代替 `rotation_vectors`),修改代码中的节点名称 - **空数据检查**:添加 `if (params.cameraMatrix.empty())` 检查关键数据 - **类型转换**:若数据是整数,使用 `static_cast<int>` 转换: ```cpp width = static_cast<int>(fs["image_width"]); ``` 此方法直接利用 OpenCV 的 `FileStorage` 类实现高效解析,保持数据类型一致性[^1][^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值