//兩種方法都使用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);
}