Skip to content

Commit fdc1c2d

Browse files
committed
Add a simplified mode for contours processing
1 parent 4746e0e commit fdc1c2d

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/cpp/models/include/models/segmentation_model.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class SegmentationModel : public BaseModel {
3737
virtual std::vector<std::unique_ptr<ImageResult>> inferBatch(const std::vector<ImageInputData>& inputImgs);
3838

3939
static std::string ModelType;
40-
std::vector<Contour> getContours(const ImageResultWithSoftPrediction& imageResult);
40+
std::vector<Contour> getContours(const ImageResultWithSoftPrediction& imageResult,
41+
bool simplified_postprocessing = false);
4142

4243
protected:
4344
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;

src/cpp/models/src/segmentation_model.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,16 @@ std::unique_ptr<ResultBase> SegmentationModel::postprocess(InferenceResult& infR
283283
return std::unique_ptr<ResultBase>(result);
284284
}
285285

286-
std::vector<Contour> SegmentationModel::getContours(const ImageResultWithSoftPrediction& imageResult) {
286+
std::vector<Contour> SegmentationModel::getContours(const ImageResultWithSoftPrediction& imageResult,
287+
bool simplified_postprocessing) {
287288
if (imageResult.soft_prediction.channels() == 1) {
288289
throw std::runtime_error{"Cannot get contours from soft prediction with 1 layer"};
289290
}
290291

291292
std::vector<Contour> combined_contours = {};
292293
cv::Mat label_index_map;
293294
cv::Mat current_label_soft_prediction;
295+
int find_contours_mode = simplified_postprocessing ? cv::RETR_EXTERNAL : cv::RETR_CCOMP;
294296
for (int index = 1; index < imageResult.soft_prediction.channels(); index++) {
295297
cv::extractChannel(imageResult.soft_prediction, current_label_soft_prediction, index);
296298
cv::inRange(imageResult.resultImage,
@@ -299,7 +301,7 @@ std::vector<Contour> SegmentationModel::getContours(const ImageResultWithSoftPre
299301
label_index_map);
300302
std::vector<std::vector<cv::Point>> contours;
301303
std::vector<cv::Vec4i> hierarchy;
302-
cv::findContours(label_index_map, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE);
304+
cv::findContours(label_index_map, contours, hierarchy, find_contours_mode, cv::CHAIN_APPROX_NONE);
303305

304306
std::string label = getLabelName(index - 1);
305307

src/python/model_api/models/segmentation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,24 @@ def postprocess(self, outputs: dict, meta: dict) -> ImageResultWithSoftPredictio
186186
def get_contours(
187187
self,
188188
prediction: ImageResultWithSoftPrediction,
189+
simplified_postprocessing: bool = False,
189190
) -> list[Contour]:
191+
"""Represents existing masks with contours.
192+
193+
Args:
194+
prediction (ImageResultWithSoftPrediction): Input segmentation prediction.
195+
simplified_postprocessing (bool, optional): Disables searching for holes in masks. Defaults to False.
196+
197+
Returns:
198+
list[Contour]: Contours found.
199+
"""
190200
n_layers = prediction.soft_prediction.shape[2]
191201

192202
if n_layers == 1:
193203
msg = "Cannot get contours from soft prediction with 1 layer"
194204
raise RuntimeError(msg)
205+
206+
find_contours_mode = cv2.RETR_CCOMP if not simplified_postprocessing else cv2.RETR_EXTERNAL
195207
combined_contours = []
196208
for layer_index in range(1, n_layers): # ignoring background
197209
label = self.get_label_name(layer_index - 1)
@@ -209,7 +221,7 @@ def get_contours(
209221

210222
contours, hierarchy = cv2.findContours(
211223
label_index_map,
212-
cv2.RETR_CCOMP,
224+
find_contours_mode,
213225
cv2.CHAIN_APPROX_NONE,
214226
)
215227
if len(contours):

0 commit comments

Comments
 (0)