Let's analyze both code snippets carefully and spot potential errors or problems.
1) Error analysis for SegmentationTrt::onMessage
VStatus SegmentationTrt::onMessage(std::vector<cv::Mat> &data)
{
auto start = std::chrono::high_resolution_clock::now();
std::vector<cv::Mat> batchCombined;
//inference
std::vector<cv::Mat> batchMasks = segmentator_.process(data);
if (maskPut_)
{
//image with masks on it
batchCombined = std::move(segmentator_.putMasksOnImage(batchMasks, data));
//if crop is required
if (maskCrop_ > 0)
{
// iterate by batches
for (size_t imgNum = 0; imgNum < data.size(); ++imgNum)
{
std::int16_t cropRows = data[imgNum].rows - maskCrop_;
cv::Mat imageBot = data[imgNum].rowRange(cropRows, data[imgNum].rows).clone();
batchCombined[imgNum] = std::move(batchCombined[imgNum].rowRange(0, cropRows));
cv::vconcat(batchCombined[imgNum], imageBot, batchCombined[imgNum]);
}
}
}
batchMasks.insert(batchMasks.end(),
std::make_move_iterator(batchCombined.begin()),
std::make_move_iterator(batchCombined.end()));
data = std::move(batchMasks);
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
if (showFPS_)
{
std::cout << "TIME: " << elapsed.count() << " FPS: " << 1 / elapsed.count() << std::endl;
}
return ST_OK;
}
Potential issues here:
batchCombined might be empty if maskPut_ is false.
In this case, after the if (maskPut_) block, batchCombined remains empty, but then it is used in
batchMasks.insert(batchMasks.end(),
std::make_move_iterator(batchCombined.begin()),
std::make_move_iterator(batchCombined.end()));
Inserting elements of an empty vector is okay (no elements inserted), but then right after:
data = std::move(batchMasks);
That should be fine but is maybe not expected.
So check if you want to insert batchCombined even if it's empty. Maybe OK.
batchCombined[imgNum] = std::move(batchCombined[imgNum].rowRange(0, cropRows));
Calling rowRange returns a view (matrix header) over the original data; it does not copy data. Moving a view likely leads to invalid results.
cv::Mat uses shared pointers to data internally, so moving the header is generally not very meaningful. Although std::move on a Mat will just move the header. Since rowRange returns a new header referencing the same data, move is unnecessary and might confuse code readers.
Better to clone():
batchCombined[imgNum] = batchCombined[imgNum].rowRange(0, cropRows).clone();
This way the data is copied, and further vconcat on batchCombined[imgNum] is safe.
If
std::int16_t cropRows = data[imgNum].rows - maskCrop_;
Make sure maskCrop_ is always less than or equal to data[imgNum].rows. Otherwise negative index or invalid ranges may happen.
- Avoid mixing signed and unsigned types in loops
imgNum is size_t, but cropRows is int16_t. This may lead to problems if negative values appear.
Fix suggestions in onMessage function:
// ...
for (size_t imgNum = 0; imgNum < data.size(); ++imgNum)
{
// Defensive check for cropRows
int cropRows = data[imgNum].rows - maskCrop_;
if (cropRows <= 0)
{
// If cropRows invalid, skip cropping this image
continue;
}
cv::Mat imageBot = data[imgNum].rowRange(cropRows, data[imgNum].rows).clone();
batchCombined[imgNum] = batchCombined[imgNum].rowRange(0, cropRows).clone();
cv::vconcat(batchCombined[imgNum], imageBot, batchCombined[imgNum]);
}
2) Error analysis for Segmentator::putMasksOnImage
std::vector<cv::Mat> Segmentator::putMasksOnImage(std::vector<cv::Mat> &masks, std::vector<cv::Mat> &images)
{
std::vector<cv::Mat> batchCombined;
batchCombined.reserve(images.size());
// iterate by batches
for (size_t imgNum = 0; imgNum < images.size(); ++imgNum)
{
//masks has only 1 channel. To paint the input image, we need a mask with 3 channels
cv::Mat masksTemp = cv::Mat::zeros(masks[imgNum].size(), CV_8UC3);
//set classes to different colors
for (int i = 0; i < numClasses_; ++i)
{
masksTemp.setTo(colors_[i % numClasses_], masks[imgNum] == i + start_channel_);
}
batchCombined.emplace_back(images[imgNum] + masksTemp);
}
return batchCombined;
}
Potential issues:
start_channel_ variable usage
masks[imgNum] == i + start_channel_
Make sure start_channel_ is valid and understood (usually 0 or 1).
- Potential mismatch of types in
operator==
masks[imgNum] must have type CV_8U for == to work as a mask. Check if masks are of type CV_8UC1. If not, might get issues.
Adding these two might cause problems with data types or saturation.
It’s better to convert to a compatible type prior to addition or use cv::addWeighted/cv::add.
batchCombined.reserve(images.size()) but then using emplace_back
reserve doesn't add elements, so vector size is initially zero. emplace_back is good and will add elements, so no problem.
Fix suggestions for putMasksOnImage
-
Ensure images and masksTemp are same type, e.g., both CV_8UC3.
-
If images are not 8UC3, convert them before addition.
-
Use cv::addWeighted instead of + for better mixing.
Here is an improved version:
std::vector<cv::Mat> Segmentator::putMasksOnImage(std::vector<cv::Mat> &masks, std::vector<cv::Mat> &images)
{
std::vector<cv::Mat> batchCombined;
batchCombined.reserve(images.size());
for (size_t imgNum = 0; imgNum < images.size(); ++imgNum)
{
// create color mask
cv::Mat masksTemp = cv::Mat::zeros(masks[imgNum].size(), CV_8UC3);
for (int i = 0; i < numClasses_; ++i)
{
cv::Mat classMask = (masks[imgNum] == i + start_channel_);
masksTemp.setTo(colors_[i % numClasses_], classMask);
}
cv::Mat imageColor;
if (images[imgNum].type() != CV_8UC3)
{
images[imgNum].convertTo(imageColor, CV_8UC3, 255.0); // if input float images scaled 0-1
}
else
{
imageColor = images[imgNum];
}
cv::Mat blended;
double alpha = 0.5; // transparency factor for mask overlay
cv::addWeighted(imageColor, 1.0, masksTemp, alpha, 0.0, blended);
batchCombined.emplace_back(blended);
}
return batchCombined;
}
Summary of fixes:
-
In onMessage:
-
Replace std::move(batchCombined[imgNum].rowRange(0, cropRows)) with .rowRange(0, cropRows).clone().
-
Add check to ensure cropRows > 0.
-
In putMasksOnImage:
-
For mask application, use cv::addWeighted rather than direct +.
-
Convert images to CV_8UC3 if necessary.
Please let me know if you want me to rewrite the entire functions with fixes, or if you want help with something specific!