Parcourir la source

Display real image size.

jcsyshc il y a 1 an
Parent
commit
1d5838de06
3 fichiers modifiés avec 54 ajouts et 3 suppressions
  1. 19 2
      src/device/impl/orb_camera.cpp
  2. 15 1
      src/device/impl/orb_camera_ui.cpp
  3. 20 0
      src/image_utility.hpp

+ 19 - 2
src/device/impl/orb_camera.cpp

@@ -5,6 +5,9 @@
 
 #include <boost/asio/post.hpp>
 
+#include <opencv2/imgcodecs.hpp>
+#include <opencv2/imgproc.hpp>
+
 using boost::asio::post;
 
 namespace orb_camera_impl {
@@ -39,6 +42,15 @@ namespace orb_camera_impl {
         return create_image(info);
     }
 
+    auto mjpeg_frame_to_image(const video_frame_type &frame) { // TODO: accelerate with CUDA
+        assert(frame->format() == OB_FORMAT_MJPG);
+        auto img_data = cv::_InputArray((uchar *) frame->data(), frame->dataSize());
+        auto img_bgr = cv::imdecode(img_data, cv::IMREAD_UNCHANGED);
+        auto img_rgb_info = create_image_info<uchar3>(img_bgr.size(), MEM_HOST);
+        cv::cvtColor(img_bgr, img_rgb_info.as_mat(), cv::COLOR_BGR2RGB);
+        return create_image(img_rgb_info);
+    }
+
     image_f32c1 depth_y16_to_mm(const image_u16c1 &y16, float scale) { // TODO: accelerate with CUDA
         auto y16_info = y16->as_host_info();
         auto f32_info = create_image_info<float1>(y16_info.size, MEM_HOST);
@@ -137,8 +149,13 @@ void orb_camera::impl::frames_callback(const frames_type &frames) {
     auto c_img = image_u8c3();
     if (auto c_frame = frames->colorFrame(); c_frame != nullptr) {
         assert(c_name != invalid_obj_name);
-        assert(c_frame->format() == OB_FORMAT_RGB);
-        c_img = video_uc_frame_to_image<uchar3>(c_frame);
+        auto fmt = c_frame->format();
+        if (fmt == OB_FORMAT_RGB) {
+            c_img = video_uc_frame_to_image<uchar3>(c_frame);
+        } else {
+            assert(fmt == OB_FORMAT_MJPG);
+            c_img = mjpeg_frame_to_image(c_frame);
+        }
     }
 
     auto d_img = image_f32c1();

+ 15 - 1
src/device/impl/orb_camera_ui.cpp

@@ -1,4 +1,5 @@
 #include "orb_camera_ui_impl.h"
+#include "image_utility.hpp"
 #include "imgui_utility.hpp"
 
 #include <boost/asio/io_context.hpp>
@@ -30,7 +31,8 @@ void orb_camera_ui::impl::open_camera() {
     c_conf_list.clear();
     auto cv_info = cam->query_video_info(orb_camera::V_COLOR);
     for (auto v_info: *cv_info) {
-        if (strcmp(v_info.fmt_name, "rgb") == 0) {
+        if (strcmp(v_info.fmt_name, "rgb") == 0 ||
+            strcmp(v_info.fmt_name, "mjpg") == 0) {
             c_conf_list.emplace_back(v_info.index, std::string(v_info));
         }
     }
@@ -141,6 +143,18 @@ void orb_camera_ui::impl::show() {
 
     ImGui::SeparatorText("Configs");
     show_config();
+
+    ImGui::SeparatorText("Info");
+    auto c_img = OBJ_QUERY(image_u8c3, cam_s_conf.color.name);
+    if (c_img != nullptr) {
+        auto size = c_img->size();
+        ImGui::Text("Color Stream: %dx%d", size.width, size.height);
+    }
+    auto d_img = OBJ_QUERY(image_f32c1, cam_s_conf.depth.name);
+    if (d_img != nullptr) {
+        auto size = d_img->size();
+        ImGui::Text("Depth Stream: %dx%d", size.width, size.height);
+    }
 }
 
 orb_camera_ui::orb_camera_ui(create_config conf)

+ 20 - 0
src/image_utility.hpp

@@ -114,6 +114,14 @@ public:
         return as_cuda_info(stream).as_gpu_mat();
     }
 
+    cv::Size size() const {
+        if (cuda_info.ptr != nullptr) {
+            return cuda_info.size;
+        }
+        assert(host_info.ptr != nullptr);
+        return host_info.size;
+    }
+
 private:
     image_info_type<T> host_info;
     image_info_type<T> cuda_info;
@@ -128,6 +136,18 @@ auto create_image_info(cv::Size size, memory_location mem_loc) {
     return info;
 }
 
+template<typename T>
+auto create_image_info(const cv::Mat &img) {
+    assert(get_cv_type<T>() == img.type());
+    auto info = image_info_type<T>();
+    info.ptr = std::shared_ptr<T>( // extend cv::Mat's lifetime
+            (T *) img.data, [_img = img](void *) {});
+    info.loc = MEM_HOST;
+    info.size = img.size();
+    info.pitch = img.step;
+    return info;
+}
+
 template<typename T>
 auto create_image(image_info_type<T> info) {
     return std::make_shared<smart_image<T>>(info);