SMACC2
Loading...
Searching...
No Matches
cp_table_segmenter.cpp
Go to the documentation of this file.
1// Copyright 2026 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#include <opencv2/imgcodecs.hpp>
18
19namespace cl_rgb
20{
21
22CpTableSegmenter::CpTableSegmenter(const SegmenterParams & params) : params_(params) {}
23
25
27{
28 const std::string path = this->params().referenceImagePath;
29 if (path.empty())
30 {
31 return;
32 }
33 const cv::Mat reference = cv::imread(path, cv::IMREAD_COLOR);
34 if (reference.empty())
35 {
36 RCLCPP_ERROR(
37 getLogger(),
38 "CpTableSegmenter: cannot read the reference image %s; segmenting by the table's own colour",
39 path.c_str());
40 return;
41 }
42 this->setReference(reference);
43 RCLCPP_INFO(
44 getLogger(), "CpTableSegmenter: reference image %s (%dx%d)", path.c_str(), reference.cols,
45 reference.rows);
46}
47
48void CpTableSegmenter::setReference(const cv::Mat & bgr)
49{
50 std::lock_guard<std::mutex> lock(mutex_);
51 reference_ = bgr.clone();
52}
53
55{
56 std::lock_guard<std::mutex> lock(mutex_);
57 return !reference_.empty();
58}
59
61{
62 std::lock_guard<std::mutex> lock(mutex_);
64}
65
67{
68 std::lock_guard<std::mutex> lock(mutex_);
69 return params_;
70}
71
72cv::Mat CpTableSegmenter::foreground(const cv::Mat & bgr, const cv::Mat & tableMask) const
73{
74 cv::Mat reference;
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 reference = reference_; // shares the pixels; they are never written after setReference
78 }
79 if (!reference.empty() && reference.size() == bgr.size())
80 {
81 return segmentAgainstReference(bgr, reference, tableMask, this->params());
82 }
83 return segmentForeground(bgr, tableMask, this->params());
84}
85
86} // namespace cl_rgb
SegmenterParams params() const
void setParams(const SegmenterParams &params)
void setReference(const cv::Mat &bgr)
cv::Mat foreground(const cv::Mat &bgr, const cv::Mat &tableMask) const
CpTableSegmenter(const SegmenterParams &params)
rclcpp::Logger getLogger() const
cv::Mat segmentAgainstReference(const cv::Mat &bgr, const cv::Mat &referenceBgr, const cv::Mat &tableMask, const SegmenterParams &params)
cv::Mat segmentForeground(const cv::Mat &bgr, const cv::Mat &tableMask, const SegmenterParams &params)