|
1 | 1 | #include "utils.h" |
| 2 | +#include <opencv2/ximgproc/edge_filter.hpp> // for guided filter |
| 3 | + |
2 | 4 | // Constructor |
3 | 5 | Utils::Utils(){ |
4 | 6 |
|
@@ -155,7 +157,7 @@ void Utils::overlay(std::vector<Ort::Value>& output_tensors, const cv::Mat& iImg |
155 | 157 | bestMaskIndex = i; |
156 | 158 | } |
157 | 159 | } |
158 | | - |
| 160 | + std::cout << "Best mask index: " << bestMaskIndex << ", Score: " << bestScore << std::endl; |
159 | 161 | // Create OpenCV Mat for the mask |
160 | 162 | cv::Mat mask = cv::Mat::zeros(height, width, CV_8UC1); |
161 | 163 |
|
@@ -197,17 +199,36 @@ void Utils::overlay(std::vector<Ort::Value>& output_tensors, const cv::Mat& iImg |
197 | 199 | // Use INTER_NEAREST for binary masks - preserves hard edges |
198 | 200 | cv::resize(croppedMask, finalMask, cv::Size(iImg.cols, iImg.rows), 0, 0, cv::INTER_NEAREST); |
199 | 201 |
|
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 | + } |
202 | 208 |
|
| 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 ///////////////////////// |
203 | 217 | // 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)); |
206 | 220 |
|
207 | 221 | // 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 ///////////////////////// |
209 | 228 |
|
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); |
211 | 232 | result.masks.push_back(finalMask); |
212 | 233 |
|
213 | 234 | /*// Add IoU scores if available (typically second tensor) |
|
0 commit comments