namespace MediaVision {
namespace ROITracker {
+
+using TrackerResult = std::tuple<int, int, int, int>;
+
class ROITracker {
public:
- ROITracker();
- ~ ROITracker() = default;
-
- void perform(cv::Mat frame);
- void getResult(int *x, int *y, int *width, int *height);
- bool initialized();
- int setConfiguration(mv_engine_config_h engine_config);
- int setRoi(int x, int y, int width, int height);
+ ROITracker() = default;
+ virtual ~ROITracker() = default;
+
+ void setType(mv_roi_tracker_type_e type) noexcept;
+ void setRoi(int x, int y, int width, int height) noexcept;
+ void perform(cv::Mat& frame);
+ TrackerResult result();
+
private:
- void initialize(cv::Mat &frame);
- void update(cv::Mat &frame);
+ void initialize(cv::Mat& frame);
+ void update(cv::Mat& frame);
- cv::Ptr<cv::Tracker> cvTracker;
- cv::Rect boundingBox;
- mv_roi_tracker_type_e type;
- mv_roi_tracker_result_s result;
+ cv::Ptr<cv::Tracker> __cvTracker;
+ cv::Rect __boundingBox;
+ mv_roi_tracker_type_e __type { MV_ROI_TRACKER_TYPE_BALANCE };
+ bool __initialized { false };
};
} /* ROITracker */
#include "ROITracker.h"
#include "ROITrackerUtil.h"
-using namespace std;
-
namespace MediaVision {
namespace ROITracker {
-
-ROITracker::ROITracker()
- : type(MV_ROI_TRACKER_TYPE_BALANCE)
-{
- result = (mv_roi_tracker_result_s) {
- .x = -1,
- .y = -1,
- .width = -1,
- .height = -1,
- .initialized = false
- };
-}
-
-void ROITracker::initialize(cv::Mat &frame)
+void ROITracker::initialize(cv::Mat& frame)
{
- LOGI("ENTER");
-
- //initializes the tracker
- LOGD("Init pos : x:%d, y:%d, w:%d, h:%d is set.",
- boundingBox.x,
- boundingBox.y,
- boundingBox.width,
- boundingBox.height);
-
- if (cvTracker) {
+ if (__cvTracker) {
LOGE("cvTracker already exists. 'mv_roi_tracker_destroy' should be called for removing cvTracker.");
throw std::runtime_error("tracker Initialize failed.");
}
- cvTracker = createTrackerByName(type);
- cvTracker->init(frame, boundingBox);
-
- result = (mv_roi_tracker_result_s) {
- .x = boundingBox.x,
- .y = boundingBox.y,
- .width = boundingBox.width,
- .height = boundingBox.height,
- .initialized = true
- };
- LOGI("LEAVE");
+ LOGD("Init pos : x:%d, y:%d, w:%d, h:%d is set.",
+ __boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
+
+ __cvTracker = createTrackerByName(__type);
+ __cvTracker->init(frame, __boundingBox);
+
+ __initialized = true;
+
+ LOGI("Initialized done");
}
-void ROITracker::update(cv::Mat &frame)
+void ROITracker::update(cv::Mat& frame)
{
- LOGI("ENTER");
-
- //updates the tracker
- if (cvTracker->update(frame, boundingBox)) {
- result = (mv_roi_tracker_result_s) {
- .x = boundingBox.x,
- .y = boundingBox.y,
- .width = boundingBox.width,
- .height = boundingBox.height,
- .initialized = true
- };
- LOGD(" Updated: x: %d, y: %d, w: %d, h: %d",
- result.x,
- result.y,
- result.width,
- result.height);
-
- } else {
+ if (!__cvTracker->update(frame, __boundingBox)) {
LOGE("update failed.");
- result.initialized = false;
+ __initialized = false;
throw std::runtime_error("tracker update failed.");
}
- LOGI("LEAVE");
+ LOGD(" Updated: x: %d, y: %d, w: %d, h: %d",
+ __boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
}
-void ROITracker::perform(cv::Mat frame)
+void ROITracker::perform(cv::Mat& frame)
{
- if (!result.initialized)
+ if (!__initialized)
initialize(frame);
else
update(frame);
}
-void ROITracker::getResult(int *x, int *y, int *width, int *height)
+TrackerResult ROITracker::result()
{
- if (!result.initialized)
- throw std::runtime_error("tracker getResult failed.");
+ if (!__initialized)
+ throw std::runtime_error("not initialized yet!!!");
- *x = result.x;
- *y = result.y;
- *width = result.width;
- *height = result.height;
+ return std::make_tuple(__boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
}
-int ROITracker::setConfiguration(mv_engine_config_h engine_config)
+void ROITracker::setType(mv_roi_tracker_type_e type) noexcept
{
- int tracker_type = 0;
- int ret = mv_engine_config_get_int_attribute(
- engine_config, MV_ROI_TRACKER_TYPE, &tracker_type);
- if (ret != MEDIA_VISION_ERROR_NONE) {
- LOGE("Fail to get roi tracker type");
- return MEDIA_VISION_ERROR_INVALID_OPERATION;
- }
-
- type = static_cast<mv_roi_tracker_type_e>(tracker_type);
- LOGD("tracker_type: [%d] is set.", type);
+ __type = type;
- return MEDIA_VISION_ERROR_NONE;
+ LOGD("tracker_type: [%d] is set.", static_cast<int>(__type));
}
-int ROITracker::setRoi(int x, int y, int width, int height)
+void ROITracker::setRoi(int x, int y, int width, int height) noexcept
{
- boundingBox = { x, y, width, height };
+ __boundingBox = { x, y, width, height };
LOGD("ROI : x:%d, y:%d, w:%d, h:%d is set.",
- boundingBox.x,
- boundingBox.y,
- boundingBox.width,
- boundingBox.height);
-
- LOGD("LEAVE");
-
- return MEDIA_VISION_ERROR_NONE;
+ __boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
}
-bool ROITracker::initialized()
-{
- return result.initialized;
-}
} /* ROITracker */
} /* MediaVision */
{
LOGD("ENTER");
- //instantiates the specific Tracker
ROITracker *pTracker = new (std::nothrow)ROITracker;
if (!pTracker) {
LOGE("Failed to create tracker");
int mv_roi_tracker_destroy_open(mv_roi_tracker_h handle)
{
LOGD("ENTER");
+
if (!handle) {
LOGE("Handle can't be destroyed because handle is NULL");
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
+ int tracker_type;
+ if (mv_engine_config_get_int_attribute(engine_config,
+ MV_ROI_TRACKER_TYPE, &tracker_type) != MEDIA_VISION_ERROR_NONE)
+ return MEDIA_VISION_ERROR_INVALID_OPERATION;
+
auto pTracker = static_cast<ROITracker *>(handle);
+ pTracker->setType(static_cast<mv_roi_tracker_type_e>(tracker_type));
- int ret = pTracker->setConfiguration(engine_config);
- if (ret != MEDIA_VISION_ERROR_NONE) {
- LOGE("Fail to set configuration");
- return ret;
- }
LOGI("LEAVE");
-
return MEDIA_VISION_ERROR_NONE;
}
}
auto pTracker = static_cast<ROITracker *>(handle);
- int ret = pTracker->setRoi(x, y, width, height);
- if (ret != MEDIA_VISION_ERROR_NONE) {
- LOGE("Fail to set roi");
- return ret;
- }
+ pTracker->setRoi(x, y, width, height);
LOGI("LEAVE");
-
return MEDIA_VISION_ERROR_NONE;
}
int mv_roi_tracker_perform_open(mv_roi_tracker_h handle, mv_source_h source, mv_roi_tracker_tracked_cb tracked_cb, void *user_data)
{
LOGD("ENTER");
+
if (!handle) {
LOGE("Handle is NULL.");
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
- auto pTracker = static_cast<ROITracker *>(handle);
-
unsigned int channels = 0;
unsigned int width = 0, height = 0;
unsigned int bufferSize = 0;
LOGD(" w: %d, h: %d, c: %d", width, height, channels);
try {
- pTracker->perform(getTrackerFrame(colorspace, channels, width, height, buffer));
+ cv::Mat frame = getTrackerFrame(colorspace, channels, width, height, buffer);
+
+ auto pTracker = static_cast<ROITracker *>(handle);
+ pTracker->perform(frame);
+
+ mv_rectangle_s roi;
+ std::tie(roi.point.x, roi.point.y, roi.width, roi.height) = pTracker->result();
+ tracked_cb(source, roi, user_data);
} catch (const std::exception &e) {
LOGE("Failed : %s", e.what());
return MEDIA_VISION_ERROR_INVALID_OPERATION;
}
- mv_rectangle_s roi;
- pTracker->getResult(&roi.point.x, &roi.point.y, &roi.width, &roi.height);
-
- tracked_cb(source, roi, user_data);
-
LOGD("LEAVE");
return MEDIA_VISION_ERROR_NONE;
}