output_sensor_file.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c), 2023 - Analog Devices Inc. All Rights Reserved.
3 This software is PROPRIETARY & CONFIDENTIAL to Analog Devices, Inc.
4 and its licensors.
5 ******************************************************************************/
6 
7 #include "output_sensor_file.h"
8 #include "image_proc_utils.h"
9 
14 {
15  video_enabled_ = false;
16  output_video_writer_ = NULL;
18 }
19 
27 void OutputSensorFile::open(std::string input_file_name, int image_width, int image_height)
28 {
29  // Open output video file
30  openOutputVideoFile(input_file_name, image_width,
31  image_height * 2); // Multiplied height by 2 as we will be sending
32  // both depth and IR out, vertically concatenated
33 }
34 
43 void OutputSensorFile::write(const cv::Mat& stitched_depth_frame_16bb,
44  const cv::Mat& stitched_ir_frame_16bb, int image_width, int image_height)
45 {
46  // Get 8bit image
47  cv::Mat depth_8bit_image = cv::Mat::zeros(image_height, image_width, CV_8UC1);
48  unsigned short max_element = 8192;
49  float scale_factor = 255.0f / max_element;
50  stitched_depth_frame_16bb.convertTo(depth_8bit_image, CV_8UC1, scale_factor, 0);
51 
52  // Get rgb 8 bit image
53  cv::Mat depth_8bit_rgb_image = cv::Mat::zeros(image_height, image_width, CV_8UC3);
54  cv::cvtColor(depth_8bit_image, depth_8bit_rgb_image, cv::COLOR_GRAY2BGR);
55 
56  // Get 8bit image
57  cv::Mat ir_8bit_image = cv::Mat::zeros(image_height, image_width, CV_8UC1);
58  ImageProcUtils::gammaCorrect((unsigned short*)stitched_ir_frame_16bb.data, image_height * image_width);
59  stitched_ir_frame_16bb.convertTo(ir_8bit_image, CV_8UC1, 1, 0);
60 
61  // Get rgb 8 bit image
62  cv::Mat ir_8bit_rgb_image = cv::Mat::zeros(image_height, image_width, CV_8UC3);
63  cv::cvtColor(ir_8bit_image, ir_8bit_rgb_image, cv::COLOR_GRAY2BGR);
64 
65  // Concatenation of input depth and algo output image
66  cv::Mat final_out_image = cv::Mat::zeros(cv::Size(image_width, image_height * 2), CV_8UC3);
67  cv::vconcat(ir_8bit_rgb_image, depth_8bit_rgb_image, final_out_image);
68 
69  // Show output in opencv window
70  cv::namedWindow("Stitch_Output", cv::WINDOW_AUTOSIZE);
71  cv::imshow("Stitch_Output", final_out_image);
72  cv::waitKey(1);
73 
74  writeOutputVideoFile(final_out_image);
75 }
76 
82 {
84 }
85 
93 void OutputSensorFile::openOutputVideoFile(const std::string& input_file_name, int image_width, int image_height)
94 {
95  output_video_file_name_ = input_file_name.substr(0, input_file_name.find_last_of('.')) + ".avi";
96  output_video_writer_ = new cv::VideoWriter(output_video_file_name_, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10,
97  cv::Size(image_width, image_height), true);
98  if (!output_video_writer_->isOpened())
99  {
100  std::cout << "Could not open output video file for the input " << input_file_name << std::endl;
101  }
102 }
103 
109 void OutputSensorFile::writeOutputVideoFile(const cv::Mat& image)
110 {
111  if (output_video_writer_->isOpened())
112  {
113  output_video_writer_->write(image);
114  }
115 }
116 
122 {
123  if (output_video_writer_->isOpened())
124  {
125  output_video_writer_->release();
126  output_video_writer_ = nullptr;
127  }
128 }
OutputSensorFile::close
void close()
Closes all opened output files.
Definition: output_sensor_file.cpp:81
OutputSensorFile::write
void write(const cv::Mat &stitched_depth_frame_16bb, const cv::Mat &stitched_ir_frame_16bb, int image_width, int image_height)
Writes one frame to output video file.
Definition: output_sensor_file.cpp:43
OutputSensorFile::output_video_file_name_
std::string output_video_file_name_
Definition: output_sensor_file.h:33
output_sensor_file.h
OutputSensorFile::OutputSensorFile
OutputSensorFile()
Constructor for OutputSensorFile class.
Definition: output_sensor_file.cpp:13
OutputSensorFile::open
void open(std::string input_file_name, int image_width, int image_height)
Opens output files (video and/or csv)
Definition: output_sensor_file.cpp:27
OutputSensorFile::video_enabled_
bool video_enabled_
Definition: output_sensor_file.h:31
OutputSensorFile::openOutputVideoFile
void openOutputVideoFile(const std::string &input_file_name, int image_width, int image_height)
Opens output video file.
Definition: output_sensor_file.cpp:93
image_proc_utils.h
OutputSensorFile::closeOutputVideoFile
void closeOutputVideoFile()
Closes output video file.
Definition: output_sensor_file.cpp:121
OutputSensorFile::writeOutputVideoFile
void writeOutputVideoFile(const cv::Mat &image)
Writes output video file.
Definition: output_sensor_file.cpp:109
OutputSensorFile::output_video_writer_
cv::VideoWriter * output_video_writer_
Definition: output_sensor_file.h:32
ImageProcUtils::gammaCorrect
static void gammaCorrect(unsigned short *src_dst, int num_pixels)
Function for Gamma Correction.
Definition: image_proc_utils.h:134


adi_3dtof_image_stitching
Author(s):
autogenerated on Fri Mar 21 2025 02:27:20