//两种方法都使用FILE类,保存txt文件
//第一种方法 #include <opencv2/opencv.hpp> //头文件
using namespace cv; //包含cv命名空间 using namespace std;
static void saveXYZ(const char* filename, const Mat mat) { const double max_z = 1.0e4; FILE* fp = fopen(filename, "wt"); for (int y = 0; y < mat.rows; y++) { for (int x = 0; x < mat.cols; x++) { Vec3b point = mat.at<Vec3b>(y, x); //if (fabs(point[2] - max_z) < FLT_EPSILON || fabs(point[2]) > max_z) continue; //fprintf(fp, "%f %f %f\n", point[0], point[1], point[2]); fprintf(fp, "%d %d %d \n", point[0], point[1], point[2]); } } fclose(fp); }
void main(int argc, char** argv) { std::string point_cloud_filename = ""; cv::CommandLineParser parser(argc, argv, "{p|point_cloud.txt|}");
// 【1】读入一张图片,载入图像 Mat srcImage = imread("1.jpg"); // 【2】显示载入的图片 imshow("【原始图】", srcImage); point_cloud_filename = parser.get<std::string>("p"); saveXYZ(point_cloud_filename.c_str(), srcImage); cout << srcImage.channels(); // 【3】等待任意按键按下 waitKey(0); }
//第二种方法
#include <opencv2/opencv.hpp> //头文件
using namespace cv; //包含cv命名空间 using namespace std;
static void saveXYZ( Mat mat) { const double max_z = 1.0e4; FILE* fp = fopen("point_cloud.txt", "wt"); for (int y = 0; y < mat.rows; y++) { for (int x = 0; x < mat.cols; x++) { Vec3b point = mat.at<Vec3b>(y, x); //if (fabs(point[2] - max_z) < FLT_EPSILON || fabs(point[2]) > max_z) continue; //fprintf(fp, "%f %f %f\n", point[0], point[1], point[2]); fprintf(fp, "%d %d %d \n", point[0], point[1], point[2]); } } fclose(fp); }
void main(int argc, char** argv) {
// 【1】读入一张图片,载入图像 Mat srcImage = imread("1.jpg"); // 【2】显示载入的图片 imshow("【原始图】", srcImage); saveXYZ( srcImage); cout << srcImage.channels(); // 【3】等待任意按键按下 waitKey(0); }