From 20814d15d2e1112d720d53982222a7b6673a27a5 Mon Sep 17 00:00:00 2001 From: jario Date: Sat, 16 Dec 2023 20:24:54 +0800 Subject: [PATCH] fix 4k draw slow prob --- include/sv_video_base.h | 10 ++++++ video_io/sv_video_base.cpp | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/sv_video_base.h b/include/sv_video_base.h index 64fd2cd..ff74885 100644 --- a/include/sv_video_base.h +++ b/include/sv_video_base.h @@ -394,6 +394,16 @@ void drawTargetsInFrame( bool with_aruco=false, bool with_yaw=false ); +cv::Mat drawTargetsInFrame( + const cv::Mat img_, + const TargetsInFrame& tgts_, + const double scale, + bool with_all=true, + bool with_category=false, + bool with_tid=false, + bool with_seg=false, + bool with_box=false +); std::string get_home(); bool is_file_exist(std::string& fn); void list_dir(std::string dir, std::vector& files, std::string suffixs="", bool r=false); diff --git a/video_io/sv_video_base.cpp b/video_io/sv_video_base.cpp index e7530ad..7ff38a9 100644 --- a/video_io/sv_video_base.cpp +++ b/video_io/sv_video_base.cpp @@ -454,6 +454,71 @@ void drawTargetsInFrame( } } + +cv::Mat drawTargetsInFrame( + const cv::Mat img_, + const TargetsInFrame& tgts_, + const double scale, + bool with_all, + bool with_category, + bool with_tid, + bool with_seg, + bool with_box +) +{ + cv::Mat img_show; + cv::resize(img_, img_show, cv::Size(0, 0), scale, scale); + if (tgts_.rois.size() > 0) + { + cv::Mat image_ret; + cv::addWeighted(img_show, 0.5, cv::Mat::zeros(cv::Size(img_show.cols, img_show.rows), CV_8UC3), 0, 0, image_ret); + cv::Rect roi = cv::Rect((int)(tgts_.rois[0].x1*scale), (int)(tgts_.rois[0].y1*scale), (int)((tgts_.rois[0].x2 - tgts_.rois[0].x1)*scale), (int)((tgts_.rois[0].y2 - tgts_.rois[0].y1)*scale)); + img_show(roi).copyTo(image_ret(roi)); + image_ret.copyTo(img_show); + } + + for (Target tgt : tgts_.targets) + { + cv::circle(img_show, cv::Point(int(tgt.cx * tgts_.width * scale), int(tgt.cy * tgts_.height * scale)), 4, cv::Scalar(0,255,0), 2); + if ((with_all || with_box) && tgt.has_box) + { + Box b; + tgt.getBox(b); + cv::rectangle(img_show, cv::Rect((int)(b.x1 * scale), (int)(b.y1 * scale), (int)((b.x2-b.x1+1)*scale), (int)((b.y2-b.y1+1)*scale)), cv::Scalar(0,0,255), 1, 1, 0); + if ((with_all || with_category) && tgt.has_category) + { + cv::putText(img_show, tgt.category, cv::Point((int)(b.x1*scale), (int)(b.y1*scale)-4), cv::FONT_HERSHEY_DUPLEX, 0.4, cv::Scalar(255,0,0)); + } + if ((with_all || with_tid) && tgt.has_tid) + { + char tmp[32]; + sprintf(tmp, "TID: %d", tgt.tracked_id); + cv::putText(img_show, tmp, cv::Point((int)(b.x1*scale), (int)(b.y1*scale)-14), cv::FONT_HERSHEY_DUPLEX, 0.4, cv::Scalar(0,0,255)); + } + } + if ((with_all || with_seg) && tgt.has_seg) + { + cv::Mat mask = tgt.getMask() * 255; + cv::threshold(mask, mask, 127, 255, cv::THRESH_BINARY); + mask.convertTo(mask, CV_8UC1); + + cv::resize(mask, mask, cv::Size(img_show.cols, img_show.rows)); + std::vector > contours; + std::vector hierarchy; + + cv::findContours(mask, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); + cv::Mat mask_disp = img_show.clone(); + cv::fillPoly(mask_disp, contours, cv::Scalar(255,255,255), cv::LINE_AA); + cv::polylines(img_show, contours, true, cv::Scalar(255,255,255), 2, cv::LINE_AA); + + double alpha = 0.6; + cv::addWeighted(img_show, alpha, mask_disp, 1.0-alpha, 0, img_show); + } + } + return img_show; +} + + std::string Target::getJsonStr() { std::string json_str = "{";