aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWind Yuan <feng.yuan@intel.com>2017-11-22 14:58:52 +0800
committerwindyuan <feng.yuan@intel.com>2017-11-22 17:50:50 +0800
commit0199a5e905b67ce7eaf6c69f97f810a5a6159e39 (patch)
treee58299460e9c62281276089cd7416419e28c15f9
parent2997bc86bde5b3bde05355ecd86dcc56128e15d1 (diff)
downloadlibxcam-0199a5e905b67ce7eaf6c69f97f810a5a6159e39.tar.gz
cv: extract convert_to_mat and remove duplicated code
Signed-off-by: Wind Yuan <feng.yuan@intel.com>
-rw-r--r--modules/ocl/cl_utils.cpp2
-rw-r--r--modules/ocl/cv_base_class.cpp46
-rw-r--r--modules/ocl/cv_base_class.h7
-rw-r--r--modules/ocl/cv_context.cpp16
-rw-r--r--modules/ocl/cv_context.h3
-rw-r--r--tests/test-image-stitching.cpp53
-rw-r--r--tests/test-video-stabilization.cpp42
7 files changed, 57 insertions, 112 deletions
diff --git a/modules/ocl/cl_utils.cpp b/modules/ocl/cl_utils.cpp
index a2db38a..7fa78c3 100644
--- a/modules/ocl/cl_utils.cpp
+++ b/modules/ocl/cl_utils.cpp
@@ -117,7 +117,7 @@ convert_to_clbuffer (
XCAM_UNUSED (context);
#endif
- XCAM_FAIL_RETURN (DEBUG, cl_buf.ptr (), NULL, "convert to clbuffer failed");
+ XCAM_FAIL_RETURN (WARNING, cl_buf.ptr (), NULL, "convert to clbuffer failed");
return cl_buf;
}
diff --git a/modules/ocl/cv_base_class.cpp b/modules/ocl/cv_base_class.cpp
index 48fad1e..7dbecaa 100644
--- a/modules/ocl/cv_base_class.cpp
+++ b/modules/ocl/cv_base_class.cpp
@@ -27,39 +27,41 @@ CVBaseClass::CVBaseClass ()
{
_cv_context = CVContext::instance ();
XCAM_ASSERT (_cv_context.ptr ());
- _use_ocl = cv::ocl::useOpenCL ();
+ _use_ocl = _cv_context->is_ocl_enabled ();
+}
+
+bool
+CVBaseClass::set_ocl (bool use_ocl)
+{
+ if (use_ocl && !_cv_context->is_ocl_enabled ()) {
+ return false;
+ }
+ _use_ocl = use_ocl;
+ return true;
}
bool
CVBaseClass::convert_to_mat (SmartPtr<VideoBuffer> buffer, cv::Mat &image)
{
- cv::Mat mat;
+
VideoBufferInfo info = buffer->get_video_info ();
XCAM_FAIL_RETURN (WARNING, info.format == V4L2_PIX_FMT_NV12, false, "convert_to_mat only support NV12 format");
- SmartPtr<CLBuffer> cl_buffer = convert_to_clbuffer (_cv_context->get_cl_context (), buffer);
- if (!cl_buffer.ptr ()) {
- uint8_t *ptr = buffer->map ();
- mat = cv::Mat (info.height * 3 / 2, info.width, CV_8UC1, ptr, info.strides[0]);
- } else {
- cl_mem cl_mem_id = cl_buffer->get_mem_id ();
-
- cv::UMat umat;
- cv::ocl::convertFromBuffer (cl_mem_id, info.strides[0], info.height * 3 / 2, info.width, CV_8U, umat);
- if (umat.empty ()) {
- XCAM_LOG_ERROR ("convert buffer to UMat failed");
- return false;
- }
-
- umat.copyTo (mat);
- if (mat.empty ()) {
- XCAM_LOG_ERROR ("copy UMat to Mat failed");
- return false;
- }
- }
+ uint8_t *ptr = buffer->map ();
+ XCAM_FAIL_RETURN (WARNING, ptr, false, "convert_to_mat buffer map failed");
+ cv::Mat mat = cv::Mat (info.aligned_height * 3 / 2, info.width, CV_8UC1, ptr, info.strides[0]);
cv::cvtColor (mat, image, cv::COLOR_YUV2BGR_NV12);
+ //buffer->unmap ();
+
return true;
}
+bool
+convert_to_mat (SmartPtr<VideoBuffer> buffer, cv::Mat &image)
+{
+ CVBaseClass cv_obj;
+ return cv_obj.convert_to_mat (buffer, image);
+}
+
}
diff --git a/modules/ocl/cv_base_class.h b/modules/ocl/cv_base_class.h
index 0bf3425..7fb0520 100644
--- a/modules/ocl/cv_base_class.h
+++ b/modules/ocl/cv_base_class.h
@@ -44,9 +44,7 @@ class CVBaseClass
{
public:
explicit CVBaseClass ();
- void set_ocl (bool use_ocl) {
- _use_ocl = use_ocl;
- }
+ bool set_ocl (bool use_ocl);
bool is_ocl_path () {
return _use_ocl;
}
@@ -58,6 +56,9 @@ protected:
bool _use_ocl;
};
+extern bool
+convert_to_mat (SmartPtr<VideoBuffer> buffer, cv::Mat &image);
+
}
#endif // XCAM_CV_BASE_CLASS_H
diff --git a/modules/ocl/cv_context.cpp b/modules/ocl/cv_context.cpp
index a8f5508..79d8ab7 100644
--- a/modules/ocl/cv_context.cpp
+++ b/modules/ocl/cv_context.cpp
@@ -52,6 +52,22 @@ CVContext::init_opencv_ocl ()
XCAM_LOG_DEBUG("Use OpenCL is: %s", cv::ocl::haveOpenCL() ? "true" : "false");
}
+bool
+CVContext::enable_ocl (bool flag)
+{
+ if (flag && !cv::ocl::haveOpenCL()) {
+ return false;
+ }
+ cv::ocl::setUseOpenCL (flag);
+ return true;
+}
+
+bool
+CVContext::is_ocl_enabled () const
+{
+ return cv::ocl::useOpenCL ();
+}
+
CVContext::CVContext ()
{
diff --git a/modules/ocl/cv_context.h b/modules/ocl/cv_context.h
index 0fbdfa2..613eead 100644
--- a/modules/ocl/cv_context.h
+++ b/modules/ocl/cv_context.h
@@ -49,6 +49,9 @@ public:
return _context;
}
~CVContext();
+ bool enable_ocl (bool flag);
+ bool is_ocl_enabled () const;
+
private:
CVContext ();
void init_opencv_ocl ();
diff --git a/tests/test-image-stitching.cpp b/tests/test-image-stitching.cpp
index faaa3f4..8d7acce 100644
--- a/tests/test-image-stitching.cpp
+++ b/tests/test-image-stitching.cpp
@@ -40,7 +40,6 @@
using namespace XCam;
-#if HAVE_OPENCV
#if XCAM_TEST_STITCH_DEBUG
static void dbg_write_image (
SmartPtr<CLContext> context, SmartPtr<CLImage360Stitch> image_360,
@@ -49,42 +48,6 @@ static void dbg_write_image (
bool all_in_one, int fisheye_num, int input_count);
#endif
-void
-init_opencv_ocl (SmartPtr<CLContext> context)
-{
- cl_platform_id platform_id = CLDevice::instance()->get_platform_id ();
- char *platform_name = CLDevice::instance()->get_platform_name ();
- cl_device_id device_id = CLDevice::instance()->get_device_id ();
- cl_context context_id = context->get_context_id ();
- cv::ocl::attachContext (platform_name, platform_id, context_id, device_id);
-}
-
-bool
-convert_to_mat (SmartPtr<CLContext> context, SmartPtr<VideoBuffer> buffer, cv::Mat &image)
-{
- SmartPtr<CLBuffer> cl_buffer = convert_to_clbuffer (context, buffer);
- VideoBufferInfo info = buffer->get_video_info ();
- cl_mem cl_mem_id = cl_buffer->get_mem_id ();
-
- cv::UMat umat;
- cv::ocl::convertFromBuffer (cl_mem_id, info.strides[0], info.height * 3 / 2, info.width, CV_8U, umat);
- if (umat.empty ()) {
- XCAM_LOG_ERROR ("convert bo buffer to UMat failed");
- return false;
- }
-
- cv::Mat mat;
- umat.copyTo (mat);
- if (mat.empty ()) {
- XCAM_LOG_ERROR ("copy UMat to Mat failed");
- return false;
- }
-
- cv::cvtColor (mat, image, cv::COLOR_YUV2BGR_NV12);
- return true;
-}
-#endif
-
static bool
parse_calibration_params (
IntrinsicParameter intrinsic_param[],
@@ -506,8 +469,6 @@ int main (int argc, char *argv[])
}
#if HAVE_OPENCV
- init_opencv_ocl (context);
-
cv::VideoWriter writer;
cv::VideoWriter top_view_writer;
cv::VideoWriter rectified_view_writer;
@@ -584,15 +545,15 @@ int main (int argc, char *argv[])
#if HAVE_OPENCV
if (need_save_output) {
cv::Mat out_mat;
- convert_to_mat (context, output_buf, out_mat);
+ convert_to_mat (output_buf, out_mat);
writer.write (out_mat);
cv::Mat top_view_mat;
- convert_to_mat (context, top_view_buf, top_view_mat);
+ convert_to_mat (top_view_buf, top_view_mat);
top_view_writer.write (top_view_mat);
cv::Mat rectified_view_mat;
- convert_to_mat (context, rectified_view_buf, rectified_view_mat);
+ convert_to_mat (rectified_view_buf, rectified_view_mat);
rectified_view_writer.write (rectified_view_mat);
#if XCAM_TEST_STITCH_DEBUG
@@ -627,7 +588,7 @@ static void dbg_write_image (
if (!all_in_one)
std::snprintf (file_name, 1023, "orig_fisheye_%d_%d.jpg", frame_count, i);
- convert_to_mat (context, input_bufs[i], mat);
+ convert_to_mat (input_bufs[i], mat);
int fisheye_per_frame = all_in_one ? fisheye_num : 1;
for (int i = 0; i < fisheye_per_frame; i++) {
cv::circle (mat, cv::Point(stitch_info.fisheye_info[i].center_x, stitch_info.fisheye_info[i].center_y),
@@ -639,19 +600,19 @@ static void dbg_write_image (
char frame_str[1024];
std::snprintf (frame_str, 1023, "%d", frame_count);
- convert_to_mat (context, output_buf, mat);
+ convert_to_mat (output_buf, mat);
cv::putText (mat, frame_str, cv::Point(120, 120), cv::FONT_HERSHEY_COMPLEX, 2.0,
cv::Scalar(0, 0, 255), 2, 8, false);
std::snprintf (file_name, 1023, "stitched_img_%d.jpg", frame_count);
cv::imwrite (file_name, mat);
- convert_to_mat (context, top_view_buf, mat);
+ convert_to_mat (top_view_buf, mat);
cv::putText (mat, frame_str, cv::Point(120, 120), cv::FONT_HERSHEY_COMPLEX, 2.0,
cv::Scalar(0, 0, 255), 2, 8, false);
std::snprintf (file_name, 1023, "top_view_img_%d.jpg", frame_count);
cv::imwrite (file_name, mat);
- convert_to_mat (context, rectified_view_buf, mat);
+ convert_to_mat (rectified_view_buf, mat);
cv::putText (mat, frame_str, cv::Point(120, 120), cv::FONT_HERSHEY_COMPLEX, 2.0,
cv::Scalar(0, 0, 255), 2, 8, false);
std::snprintf (file_name, 1023, "rectified_view_img_%d.jpg", frame_count);
diff --git a/tests/test-video-stabilization.cpp b/tests/test-video-stabilization.cpp
index 010cd95..c4304da 100644
--- a/tests/test-video-stabilization.cpp
+++ b/tests/test-video-stabilization.cpp
@@ -33,49 +33,13 @@
#if HAVE_OPENCV
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
+#include "ocl/cv_base_class.h"
#endif
using namespace XCam;
static int read_device_pose (const char *file, DevicePoseList &pose, uint32_t pose_size);
-#if HAVE_OPENCV
-void
-init_opencv_ocl (SmartPtr<CLContext> context)
-{
- cl_platform_id platform_id = CLDevice::instance()->get_platform_id ();
- char *platform_name = CLDevice::instance()->get_platform_name ();
- cl_device_id device_id = CLDevice::instance()->get_device_id ();
- cl_context context_id = context->get_context_id ();
- cv::ocl::attachContext (platform_name, platform_id, context_id, device_id);
-}
-
-bool
-convert_to_mat (SmartPtr<CLContext> context, SmartPtr<VideoBuffer> buffer, cv::Mat &image)
-{
- SmartPtr<CLBuffer> cl_buffer = convert_to_clbuffer (context, buffer);
- VideoBufferInfo info = buffer->get_video_info ();
- cl_mem cl_mem_id = cl_buffer->get_mem_id ();
-
- cv::UMat umat;
- cv::ocl::convertFromBuffer (cl_mem_id, info.strides[0], info.height * 3 / 2, info.width, CV_8U, umat);
- if (umat.empty ()) {
- XCAM_LOG_ERROR ("convert bo buffer to UMat failed");
- return false;
- }
-
- cv::Mat mat;
- umat.copyTo (mat);
- if (mat.empty ()) {
- XCAM_LOG_ERROR ("copy UMat to Mat failed");
- return false;
- }
-
- cv::cvtColor (mat, image, cv::COLOR_YUV2BGR_NV12);
- return true;
-}
-#endif
-
static void
usage(const char* arg0)
{
@@ -249,8 +213,6 @@ int main (int argc, char *argv[])
CHECK (ret, "open %s failed", file_in_name);
#if HAVE_OPENCV
- init_opencv_ocl (context);
-
cv::VideoWriter writer;
if (need_save_output) {
cv::Size dst_size = cv::Size (output_width, output_height);
@@ -296,7 +258,7 @@ int main (int argc, char *argv[])
#if HAVE_OPENCV
if (need_save_output) {
cv::Mat out_mat;
- convert_to_mat (context, output_buf, out_mat);
+ convert_to_mat (output_buf, out_mat);
writer.write (out_mat);
} else
#endif