Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cpp/CMAKELists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
project(tutorial)

cmake_minimum_required(VERSION 3.0)

set(CMAKE_CXX_STANDARD 17)

set(SENSING_DEV_DIR ${SENSING_DEV_ROOT})

# opencv ion and halide
include_directories(${SENSING_DEV_DIR}/include)
link_directories(${SENSING_DEV_DIR}/bin)
link_directories(${SENSING_DEV_DIR}/lib)

# tutorial 0
include_directories(${SENSING_DEV_DIR}/include/aravis-0.8)
include_directories(${SENSING_DEV_DIR}/include/glib-2.0)
set(T0 src/tutorial0_get_device_info.cpp)
add_executable(tutorial0_get_device_info ${T0})
target_compile_features(tutorial0_get_device_info PUBLIC cxx_std_17)

target_link_libraries(tutorial0_get_device_info PRIVATE aravis-0.8.lib)

# tutorial 1
set(T1 src/tutorial1_display.cpp)
add_executable(tutorial1_display ${T1})
target_compile_features(tutorial1_display PUBLIC cxx_std_17)

# target_link_libraries(tutorieal1_display PRIVATE aravis-0.8.lib)
target_link_libraries(tutorial1_display PRIVATE opencv_world455.lib)
target_link_libraries(tutorial1_display PRIVATE ion-core.lib)
target_link_libraries(tutorial1_display PRIVATE halide.lib)

#
# Allow big object
#
if (MSVC)
add_definitions(/bigobj)
message(STATUS "Allow big object")
endif (MSVC)
54 changes: 54 additions & 0 deletions cpp/src/tutorial0_get_device_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <exception>
#include <iostream>
#include "arv.h"

int main(int argc, char *argv[])
{
arv_update_device_list ();
unsigned int n_devices = arv_get_n_devices ();
if (n_devices < 1){
throw std::runtime_error("Device is not connected.");
}

GError *error = nullptr;

for (unsigned int i = 0; i < n_devices; ++i){
const char* dev_id = arv_get_device_id (i);
ArvDevice* device = arv_open_device(dev_id, nullptr);

printf("=== device {%d} information ===========================\n", i);

printf("%20s : %s\n",
"Device Model Name",
arv_device_get_string_feature_value(device, "DeviceModelName", &error));
if (error){
throw std::runtime_error(error->message);
}
printf("%20s : %li\n",
"Width",
arv_device_get_integer_feature_value(device, "Width", &error));
if (error){
throw std::runtime_error(error->message);
}
printf("%20s : %li\n",
"Height",
arv_device_get_integer_feature_value(device, "Height", &error));
if (error){
throw std::runtime_error(error->message);
}
printf("%20s : %li\n",
"PayloadSize",
arv_device_get_integer_feature_value(device, "PayloadSize", &error));
if (error){
throw std::runtime_error(error->message);
}
printf("%20s : %s\n",
"PixelFormat",
arv_device_get_string_feature_value(device, "PixelFormat", &error));
if (error){
throw std::runtime_error(error->message);
}
}

return 0;
}
142 changes: 142 additions & 0 deletions cpp/src/tutorial1_display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#include <ion/ion.h>

#include <exception>
#include <string>
#include <map>

using namespace ion;

#ifdef _WIN32
#define MODULE_NAME "ion-bb"
#else
#define MODULE_NAME "libion-bb.so"
#endif


#define FEATURE_GAIN_KEY "Gain"
#define FEATURE_EXPOSURE_KEY "ExposureTime"


std::map<std::string, int32_t> num_bit_shift_map{
{"Mono8", 0}, {"Mono10", 6}, {"Mono12", 4}};
std::map<std::string, int32_t> opencv_mat_type{
{"Mono8", CV_8UC1}, {"Mono10", CV_16UC1}, {"Mono12", CV_16UC1}};
std::map<std::string, std::string> bb_name{
{"Mono8", "image_io_u3v_cameraN_u8x2"},
{"Mono10", "image_io_u3v_cameraN_u16x2"},
{"Mono12", "image_io_u3v_cameraN_u16x2"}};

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);
}
}


template<typename T>
int video(int width, int height, std::string pixel_format, int num_device){
// pipeline setup
Builder b;
b.set_target(Halide::get_host_target());
b.with_bb_module(MODULE_NAME);

// set port
Port dispose_p{ "dispose", Halide::type_of<bool>() };
Port gain_p{ "gain", Halide::type_of<double>(), 1 };
Port exposure_p{ "exposure", Halide::type_of<double>(), 1 };

Node n = b.add(bb_name[pixel_format])(dispose_p, gain_p, exposure_p)
.set_param(
Param{"num_devices", std::to_string(num_device)},
Param{"pixel_format_ptr", pixel_format},
Param{"frame_sync", "true"},
Param{"gain_key", FEATURE_GAIN_KEY},
Param{"exposure_key", FEATURE_EXPOSURE_KEY},
Param{"realtime_diaplay_mode", "false"}
);

double *gains = (double*) malloc (sizeof (double) * num_device);
double *exposures = (double*) malloc (sizeof (double) * num_device);
for (int i = 0; i < num_device; ++i){
gains[i] = 40.0;
exposures[i] = 100.0;
}


Halide::Buffer<double> gain_buf(gains, std::vector< int >{num_device});
Halide::Buffer<double> exposure_buf(exposures, std::vector< int >{num_device});


PortMap pm;
pm.set(gain_p, gain_buf);
pm.set(exposure_p, exposure_buf);

std::vector< int > buf_size = std::vector < int >{ width, height };
if (pixel_format == "RGB8"){
buf_size.push_back(3);
}
std::vector<Halide::Buffer<T>> output;
for (int i = 0; i < num_device; ++i){
output.push_back(Halide::Buffer<T>(buf_size));
}
pm.set(n["output"], output);

int loop_num = 1000;
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;
std::cerr << e.what() << std::endl;
exit(1);
}

// Convert the retrieved buffer object to OpenCV buffer format.
for (int i = 0; i < num_device; ++i){
cv::Mat img(height, width, opencv_mat_type[pixel_format]);
std::memcpy(img.ptr(), output[i].data(), output[i].size_in_bytes());
img *= positive_pow(2, num_bit_shift_map[pixel_format]);
cv::imshow("image" + std::to_string(i), img);
}

// 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;
}

int main(int argc, char *argv[])
{
try{
int32_t width = 640;
int32_t height = 480;
int32_t num_device = 2;
std::string pixelformat = "Mono8";

if (pixelformat == "Mono8"){
int ret = video<uint8_t>(width, height, pixelformat, num_device);
}else{
int ret = video<uint16_t>(width, height, pixelformat, num_device);
}


}catch(std::exception& e){
std::cerr << e.what() << std::endl;
exit(1);
}
return 0;
}