Skip to content

Commit a4d14d2

Browse files
Implemented bilateral filter and morphological operations
1 parent 2926ddc commit a4d14d2

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/utils.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "utils.h"
2+
#include <opencv2/ximgproc/edge_filter.hpp> // for guided filter
3+
24
// Constructor
35
Utils::Utils(){
46

@@ -155,7 +157,7 @@ void Utils::overlay(std::vector<Ort::Value>& output_tensors, const cv::Mat& iImg
155157
bestMaskIndex = i;
156158
}
157159
}
158-
160+
std::cout << "Best mask index: " << bestMaskIndex << ", Score: " << bestScore << std::endl;
159161
// Create OpenCV Mat for the mask
160162
cv::Mat mask = cv::Mat::zeros(height, width, CV_8UC1);
161163

@@ -197,17 +199,36 @@ void Utils::overlay(std::vector<Ort::Value>& output_tensors, const cv::Mat& iImg
197199
// Use INTER_NEAREST for binary masks - preserves hard edges
198200
cv::resize(croppedMask, finalMask, cv::Size(iImg.cols, iImg.rows), 0, 0, cv::INTER_NEAREST);
199201

200-
// Re-threshold after resizing to ensure binary mask (critical step)
201-
cv::threshold(finalMask, finalMask, 127, 255, cv::THRESH_BINARY);
202+
////////////////////// GUIDED BILATERAL FILTER /////////////////////////
203+
// Convert the upscaled mask to CV_8UC1 if necessary
204+
if (finalMask.type() != CV_8UC1)
205+
{
206+
finalMask.convertTo(finalMask, CV_8UC1);
207+
}
202208

209+
// Apply the Guided Filter
210+
// cv::Mat filteredMask;
211+
int radius = 2;
212+
double eps = 0.01;
213+
cv::ximgproc::guidedFilter(iImg, finalMask, finalMask, radius, eps);
214+
////////////////////// END: GUIDED BILATERAL FILTER /////////////////////////
215+
216+
////////////////////// MORPHOLOGICAN OPERATIONS /////////////////////////
203217
// Morphological operations to clean up the mask
204-
//int kernelSize = std::max(3, std::min(iImg.cols, iImg.rows) / 100); // Adaptive size
205-
//cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(kernelSize, kernelSize));
218+
int kernelSize = std::max(5, std::min(iImg.cols, iImg.rows) / 100); // Adaptive size
219+
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(kernelSize, kernelSize));
206220

207221
// CLOSE operation: fills small holes in the mask
208-
//cv::morphologyEx(finalMask, finalMask, cv::MORPH_CLOSE, kernel);
222+
cv::morphologyEx(finalMask, finalMask, cv::MORPH_CLOSE, kernel);
223+
224+
// OPEN operation: removes small noise
225+
cv::morphologyEx(finalMask, finalMask, cv::MORPH_OPEN, kernel);
226+
227+
////////////////////// END: MORPHOLOGICAN OPERATIONS /////////////////////////
209228

210-
// Add the mask to the result
229+
// Re-threshold after resizing to ensure binary mask (critical step)
230+
231+
cv::threshold(finalMask, finalMask, 127, 255, cv::THRESH_BINARY);
211232
result.masks.push_back(finalMask);
212233

213234
/*// Add IoU scores if available (typically second tensor)

0 commit comments

Comments
 (0)