有人可以解释以下 C++ 语法吗?
Can someone explain the following C++ Syntax?
我最近在别人写的C++计算机视觉项目中遇到一句我不太明白的代码。该项目使用 OpenCV 库。该行位于名为 Board 的结构的构造函数中。
struct Board {
BoardID id;
Origin origin;
Vec<int> offset;
std::vector<std::vector<int>> pattern;
int n;
// Rows and cols
int nr;
int nc;
std::vector<std::vector<Vec<float>>> board;
// ROI for saddle points detection
const int img_rows;
const int img_cols;
cv::Mat roi;
...
Board(BoardID _id, Origin ori, int imgr, int imgc): id(_id), origin(ori), img_rows(imgr), img_cols(imgc) {
...
roi = cv::Mat::zeros(img_rows, img_cols, CV_8U);
...
// the line that I need help with understanding
roi(cv::Rect(0, 0, img_cols*9/16, img_rows)) = true;
...
...
}
使用 IDE 的快捷方式,我能够提取以下信息:
一、cv::Rect()的定义:
template<typename _Tp> inline
Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)
: x(_x), y(_y), width(_width), height(_height) {}
cv::Mat 上的 operator() 重载的定义:
inline
Mat Mat::operator()( const Rect& roi ) const
{
return Mat(*this, roi);
}
让我感到困惑的部分是 = true;
。为什么 operator()
的结果(从技术上讲是应用于 cv::Mat
roi
的函数)分配了一个布尔值?这是什么类型的 C++ 语法?
查看 OpenCV 的文档,似乎 cv::Mat
有一个采用标量的重载 operator=
:请参阅 this overload。此重载将 RHS 上的值写入 LHS 矩阵的所有元素。
关键的另一部分是显然复制 cv::Mat
是浅拷贝:即不复制数据,只复制结构。
因此,我认为您的台词可以描述如下:
// the part of the matrix we want to filter
cv::Rect filter = cv::Rect(0, 0, img_cols*9/16, img_rows)
// get a matrix referring to only the values corresponding to the above rect
cv::Mat filtered_matrix = roi(filter);
// write the value `true` to every element in our filtered section
filtered_matrix = true;
对此持保留态度;我个人对 OpenCV 不是很熟悉。
通过执行 cv::Rect(0, 0, img_cols*9/16, img_rows)
,您只需获得图像的前 9/16 列(所有行),然后称为感兴趣区域 (ROI)。
通过这样做,您从 cv::Mat
中提取了一个名为 roi
的 sub-mat。然后使用赋值运算符,然后将 true
分配给新 sub-mat.
的所有像素
我们可以这样划分代码:
// 1) Extracting the sub-mat from the 'roi' one, and keep it as reference.
cv::Mat& sub_mat = roi(cv::Rect(0, 0, img_cols*9/16, img_rows));
// 2) Assign to all pixel of sub_mat the value true.
sub_mat = true;
这意味着在最后,roi 在前 9/16 列中设置为 true,在剩余的 7/16 中设置为 0。
roi
是一个cv::Mat
,其值被初始化为0。
roi
的 operator()
is being called with a cv::Rect
作为输入。 return 值是一个新的 cv::Mat
, 在内部引用 到 roi
.
中指定的元素子集
然后 operator=
在那个新的 cv::Mat
上被调用,因此将这些元素的值设置为 1(operator=
不采用 bool
,但是它确实需要一个 cv::Scalar
,可以通过将 bool
隐式转换为 double
).
来构造
我最近在别人写的C++计算机视觉项目中遇到一句我不太明白的代码。该项目使用 OpenCV 库。该行位于名为 Board 的结构的构造函数中。
struct Board {
BoardID id;
Origin origin;
Vec<int> offset;
std::vector<std::vector<int>> pattern;
int n;
// Rows and cols
int nr;
int nc;
std::vector<std::vector<Vec<float>>> board;
// ROI for saddle points detection
const int img_rows;
const int img_cols;
cv::Mat roi;
...
Board(BoardID _id, Origin ori, int imgr, int imgc): id(_id), origin(ori), img_rows(imgr), img_cols(imgc) {
...
roi = cv::Mat::zeros(img_rows, img_cols, CV_8U);
...
// the line that I need help with understanding
roi(cv::Rect(0, 0, img_cols*9/16, img_rows)) = true;
...
...
}
使用 IDE 的快捷方式,我能够提取以下信息: 一、cv::Rect()的定义:
template<typename _Tp> inline
Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)
: x(_x), y(_y), width(_width), height(_height) {}
cv::Mat 上的 operator() 重载的定义:
inline
Mat Mat::operator()( const Rect& roi ) const
{
return Mat(*this, roi);
}
让我感到困惑的部分是 = true;
。为什么 operator()
的结果(从技术上讲是应用于 cv::Mat
roi
的函数)分配了一个布尔值?这是什么类型的 C++ 语法?
查看 OpenCV 的文档,似乎 cv::Mat
有一个采用标量的重载 operator=
:请参阅 this overload。此重载将 RHS 上的值写入 LHS 矩阵的所有元素。
关键的另一部分是显然复制 cv::Mat
是浅拷贝:即不复制数据,只复制结构。
因此,我认为您的台词可以描述如下:
// the part of the matrix we want to filter
cv::Rect filter = cv::Rect(0, 0, img_cols*9/16, img_rows)
// get a matrix referring to only the values corresponding to the above rect
cv::Mat filtered_matrix = roi(filter);
// write the value `true` to every element in our filtered section
filtered_matrix = true;
对此持保留态度;我个人对 OpenCV 不是很熟悉。
通过执行 cv::Rect(0, 0, img_cols*9/16, img_rows)
,您只需获得图像的前 9/16 列(所有行),然后称为感兴趣区域 (ROI)。
通过这样做,您从 cv::Mat
中提取了一个名为 roi
的 sub-mat。然后使用赋值运算符,然后将 true
分配给新 sub-mat.
我们可以这样划分代码:
// 1) Extracting the sub-mat from the 'roi' one, and keep it as reference.
cv::Mat& sub_mat = roi(cv::Rect(0, 0, img_cols*9/16, img_rows));
// 2) Assign to all pixel of sub_mat the value true.
sub_mat = true;
这意味着在最后,roi 在前 9/16 列中设置为 true,在剩余的 7/16 中设置为 0。
roi
是一个cv::Mat
,其值被初始化为0。
roi
的 operator()
is being called with a cv::Rect
作为输入。 return 值是一个新的 cv::Mat
, 在内部引用 到 roi
.
然后 operator=
在那个新的 cv::Mat
上被调用,因此将这些元素的值设置为 1(operator=
不采用 bool
,但是它确实需要一个 cv::Scalar
,可以通过将 bool
隐式转换为 double
).