Skip to content

Commit

Permalink
U3V example seems to be working
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-iizuka committed Jan 11, 2024
1 parent 4ad44b3 commit eba8caa
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 163 deletions.
163 changes: 68 additions & 95 deletions example/u3v_camera1_opencv/u3v_camera1_opencv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,108 +22,81 @@ const int32_t height = 480;
double gain = 400;
double exposure = 400;

#ifdef _WIN32
#define MODULE_NAME "ion-bb.dll"
#elif __APPLE__
#define MODULE_NAME "libion-bb.dylib"
#else
#define MODULE_NAME "libion-bb.so"
#endif

int positive_pow(int base, int expo){
if (expo <= 0){
return 1;
}
if (expo == 1){
return base;
}else{
return base * positive_pow(base, expo-1);
}
if (expo <= 0){
return 1;
}
if (expo == 1){
return base;
}else{
return base * positive_pow(base, expo-1);
}
}

int main(int argc, char *argv[])
{
// Define builders to build, compile, and execute pipelines.
// Build the pipeline by adding nodes to the Builder.
Builder b;

// Set the target hardware. The default is CPU.
b.set_target(Halide::get_host_target());

// Load standard building block
b.with_bb_module(MODULE_NAME);

// Define the input port
// Port class is used to define dynamic I/O for each node.
Port dispose_p{ "dispose", Halide::type_of<bool>() };
Port gain0_p{ "gain0", Halide::type_of<double>() };
Port gain1_p{ "gain1", Halide::type_of<double>() };
Port exposure0_p{ "exposure0", Halide::type_of<double>() };
Port exposure1_p{ "exposure1", Halide::type_of<double>() };

// Connect the input port to the Node instance created by b.add().
Node n = b.add("image_io_u3v_camera1_u16x2")(dispose_p, gain0_p, exposure0_p)
.set_param(
Param{"pixel_format_ptr", "Mono12"},
Param{"frame_sync", "false"},
Param{"gain_key", FEATURE_GAIN_KEY},
Param{"exposure_key", FEATURE_EXPOSURE_KEY},
Param{"realtime_diaplay_mode", "true"}
);

// Define output ports and pass each object from Node instance.
Port rp = n["output0"];
Port frame_count_p = n["frame_count"];

// Using PortMap, define output ports that map data to input ports and pass each object from a Node instance.
PortMap pm;

// In this sample, the gain value and exposure time of both sensors are set statically.
pm.set(gain0_p, gain);
pm.set(exposure0_p, exposure);

// Map data from output ports by using PortMap.
// Of the output of "u3v_camera1_u16x2",
// - rp (output of the obtained video data) to output0,
// - frame_count_p (output of the frame number of the obtained video) to frame_count,
// each stored in the buffer.
std::vector< int > buf_size = std::vector < int >{ width, height };
Halide::Buffer<uint16_t> output0(buf_size);
Halide::Buffer<uint32_t> frame_count(1);

pm.set(rp, output0);
pm.set(frame_count_p, frame_count);

// Obtain image data continuously for 100 frames to facilitate operation check.
int loop_num = 100;
for (int i = 0; i < loop_num; ++i)
{
pm.set(dispose_p, i == loop_num-1);
// JIT compilation and execution of pipelines with Builder.
try {
b.run(pm);
}catch(std::exception& e){
// e.what() shows the error message if pipeline build/run was failed.
std::cerr << "Failed to build pipeline" << std::endl;
// Define builders to build, compile, and execute pipelines.
// Build the pipeline by adding nodes to the Builder.
Builder b;

// Set the target hardware. The default is CPU.
b.set_target(Halide::get_host_target());

// Load standard building block
b.with_bb_module("ion-bb");

Buffer<double> gainb(1);
gainb.fill(gain);

Buffer<double> exposureb(1);
exposureb.fill(gain);

// Connect the input port to the Node instance created by b.add().
Node n = b.add("image_io_u3v_cameraN_u16x2")(gainb, exposureb)
.set_param(
Param("num_devices", 1),
Param("frame_sync", "false"),
Param("gain_key", FEATURE_GAIN_KEY),
Param("exposure_key", FEATURE_EXPOSURE_KEY),
Param("realtime_diaplay_mode", "true")
);

// Map output buffer and ports by using Port::bind.
// - output: output of the obtained video data
// - frame_count: output of the frame number of the obtained video
std::vector< int > buf_size = std::vector < int >{ width, height };
Buffer<uint16_t> output(buf_size);
Buffer<uint32_t> frame_count(1);

n["output"][0].bind(output);
n["frame_count"][0].bind(frame_count);

// Obtain image data continuously for 100 frames to facilitate operation check.
int loop_num = 100;
for (int i = 0; i < loop_num; ++i)
{
// JIT compilation and execution of pipelines with Builder.
b.run();

// Convert the retrieved buffer object to OpenCV buffer format.
cv::Mat A(height, width, CV_16UC1, output.data());

// Depends on sensor image pixel format, apply bit shift on images
A = A * positive_pow(2, NUM_BIT_SHIFT);

// Display the image
cv::imshow("A", A);

// Wait for key input
// When any key is pressed, close the currently displayed image and proceed to the next frame.
cv::waitKey(1);
}

} catch (const Halide::Error& e) {
std::cerr << e.what() << std::endl;
exit(1);
return 1;
}

// Convert the retrieved buffer object to OpenCV buffer format.
cv::Mat A(height, width, CV_16UC1);

std::memcpy(A.ptr(), output0.data(), output0.size_in_bytes());

// Depends on sensor image pixel format, apply bit shift on images
A = A * positive_pow(2, NUM_BIT_SHIFT);

// Display the image
cv::imshow("A", A);

// Wait for key input
// When any key is pressed, close the currently displayed image and proceed to the next frame.
cv::waitKey(1);
}

return 0;
}
}
Loading

0 comments on commit eba8caa

Please sign in to comment.