From d967ae6ae844d86f6de137d002cf42706375f3e7 Mon Sep 17 00:00:00 2001 From: liang-zijian Date: Mon, 17 Apr 2023 19:46:18 +0800 Subject: [PATCH] fix the condition if masks are empty --- csrc/jetson/segment/include/yolov8-seg.hpp | 57 ++++++++++++---------- csrc/segment/normal/include/yolov8-seg.hpp | 55 ++++++++++++--------- csrc/segment/simple/include/yolov8-seg.hpp | 56 +++++++++++---------- 3 files changed, 93 insertions(+), 75 deletions(-) diff --git a/csrc/jetson/segment/include/yolov8-seg.hpp b/csrc/jetson/segment/include/yolov8-seg.hpp index 748e8fc..ec855d7 100644 --- a/csrc/jetson/segment/include/yolov8-seg.hpp +++ b/csrc/jetson/segment/include/yolov8-seg.hpp @@ -428,36 +428,41 @@ void YOLOv8_seg::postprocess(std::vector& objs, objs.push_back(obj); cnt += 1; } + if(masks.empty()) + { + //masks is empty + } + else + { + cv::Mat matmulRes = (masks * protos).t(); + cv::Mat maskMat = matmulRes.reshape(indices.size(), { seg_w, seg_h }); - cv::Mat matmulRes = (masks * protos).t(); - cv::Mat maskMat = matmulRes.reshape(indices.size(), { seg_w, seg_h }); - - std::vector maskChannels; - cv::split(maskMat, maskChannels); - int scale_dw = dw / input_w * seg_w; - int scale_dh = dh / input_h * seg_h; + std::vector maskChannels; + cv::split(maskMat, maskChannels); + int scale_dw = dw / input_w * seg_w; + int scale_dh = dh / input_h * seg_h; - cv::Rect roi( - scale_dw, - scale_dh, - seg_w - 2 * scale_dw, - seg_h - 2 * scale_dh); + cv::Rect roi( + scale_dw, + scale_dh, + seg_w - 2 * scale_dw, + seg_h - 2 * scale_dh); - for (int i = 0; i < indices.size(); i++) - { - cv::Mat dest, mask; - cv::exp(-maskChannels[i], dest); - dest = 1.0 / (1.0 + dest); - dest = dest(roi); - cv::resize( - dest, - mask, - cv::Size((int)width, (int)height), - cv::INTER_LINEAR - ); - objs[i].boxMask = mask(objs[i].rect) > 0.5f; + for (int i = 0; i < indices.size(); i++) + { + cv::Mat dest, mask; + cv::exp(-maskChannels[i], dest); + dest = 1.0 / (1.0 + dest); + dest = dest(roi); + cv::resize( + dest, + mask, + cv::Size((int)width, (int)height), + cv::INTER_LINEAR + ); + objs[i].boxMask = mask(objs[i].rect) > 0.5f; + } } - } void YOLOv8_seg::draw_objects(const cv::Mat& image, diff --git a/csrc/segment/normal/include/yolov8-seg.hpp b/csrc/segment/normal/include/yolov8-seg.hpp index fad53e6..ae26993 100644 --- a/csrc/segment/normal/include/yolov8-seg.hpp +++ b/csrc/segment/normal/include/yolov8-seg.hpp @@ -459,33 +459,40 @@ void YOLOv8_seg::postprocess(std::vector& objs, cnt += 1; } - cv::Mat matmulRes = (masks * protos).t(); - cv::Mat maskMat = matmulRes.reshape(indices.size(), { seg_w, seg_h }); + if(masks.empty()) + { + //masks is empty + } + else + { + cv::Mat matmulRes = (masks * protos).t(); + cv::Mat maskMat = matmulRes.reshape(indices.size(), { seg_w, seg_h }); - std::vector maskChannels; - cv::split(maskMat, maskChannels); - int scale_dw = dw / input_w * seg_w; - int scale_dh = dh / input_h * seg_h; + std::vector maskChannels; + cv::split(maskMat, maskChannels); + int scale_dw = dw / input_w * seg_w; + int scale_dh = dh / input_h * seg_h; - cv::Rect roi( - scale_dw, - scale_dh, - seg_w - 2 * scale_dw, - seg_h - 2 * scale_dh); + cv::Rect roi( + scale_dw, + scale_dh, + seg_w - 2 * scale_dw, + seg_h - 2 * scale_dh); - for (int i = 0; i < indices.size(); i++) - { - cv::Mat dest, mask; - cv::exp(-maskChannels[i], dest); - dest = 1.0 / (1.0 + dest); - dest = dest(roi); - cv::resize( - dest, - mask, - cv::Size((int)width, (int)height), - cv::INTER_LINEAR - ); - objs[i].boxMask = mask(objs[i].rect) > 0.5f; + for (int i = 0; i < indices.size(); i++) + { + cv::Mat dest, mask; + cv::exp(-maskChannels[i], dest); + dest = 1.0 / (1.0 + dest); + dest = dest(roi); + cv::resize( + dest, + mask, + cv::Size((int)width, (int)height), + cv::INTER_LINEAR + ); + objs[i].boxMask = mask(objs[i].rect) > 0.5f; + } } } diff --git a/csrc/segment/simple/include/yolov8-seg.hpp b/csrc/segment/simple/include/yolov8-seg.hpp index 32400b7..a8d9fee 100644 --- a/csrc/segment/simple/include/yolov8-seg.hpp +++ b/csrc/segment/simple/include/yolov8-seg.hpp @@ -431,35 +431,41 @@ void YOLOv8_seg::postprocess(std::vector& objs, cnt += 1; } - cv::Mat matmulRes = (masks * protos).t(); - cv::Mat maskMat = matmulRes.reshape(indices.size(), { seg_w, seg_h }); + if(masks.empty()) + { + //masks is empty + } + else + { + cv::Mat matmulRes = (masks * protos).t(); + cv::Mat maskMat = matmulRes.reshape(indices.size(), { seg_w, seg_h }); - std::vector maskChannels; - cv::split(maskMat, maskChannels); - int scale_dw = dw / input_w * seg_w; - int scale_dh = dh / input_h * seg_h; + std::vector maskChannels; + cv::split(maskMat, maskChannels); + int scale_dw = dw / input_w * seg_w; + int scale_dh = dh / input_h * seg_h; - cv::Rect roi( - scale_dw, - scale_dh, - seg_w - 2 * scale_dw, - seg_h - 2 * scale_dh); + cv::Rect roi( + scale_dw, + scale_dh, + seg_w - 2 * scale_dw, + seg_h - 2 * scale_dh); - for (int i = 0; i < indices.size(); i++) - { - cv::Mat dest, mask; - cv::exp(-maskChannels[i], dest); - dest = 1.0 / (1.0 + dest); - dest = dest(roi); - cv::resize( - dest, - mask, - cv::Size((int)width, (int)height), - cv::INTER_LINEAR - ); - objs[i].boxMask = mask(objs[i].rect) > 0.5f; + for (int i = 0; i < indices.size(); i++) + { + cv::Mat dest, mask; + cv::exp(-maskChannels[i], dest); + dest = 1.0 / (1.0 + dest); + dest = dest(roi); + cv::resize( + dest, + mask, + cv::Size((int)width, (int)height), + cv::INTER_LINEAR + ); + objs[i].boxMask = mask(objs[i].rect) > 0.5f; + } } - } void YOLOv8_seg::draw_objects(const cv::Mat& image,