fix 4k draw slow prob

This commit is contained in:
jario 2023-12-16 20:24:54 +08:00
parent c74319529c
commit 20814d15d2
2 changed files with 75 additions and 0 deletions

View File

@ -394,6 +394,16 @@ void drawTargetsInFrame(
bool with_aruco=false, bool with_aruco=false,
bool with_yaw=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(); std::string get_home();
bool is_file_exist(std::string& fn); bool is_file_exist(std::string& fn);
void list_dir(std::string dir, std::vector<std::string>& files, std::string suffixs="", bool r=false); void list_dir(std::string dir, std::vector<std::string>& files, std::string suffixs="", bool r=false);

View File

@ -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<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> 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 Target::getJsonStr()
{ {
std::string json_str = "{"; std::string json_str = "{";