如何解决有人可以解释以下C ++语法吗?
我最近遇到了一个别人写的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,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,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。
, "my example context"
是一个cv::Mat
,其值初始化为0。
<a href="{% url 'sitemember:profile' user.username %}">
的{{3}}以operator()
作为输入被调用。返回值是一个新的roi
,它内部引用到roi
中指定的元素子集。
然后在新的cv::Mat
上调用cv::Rect
,因此将这些元素的值设置为1(roi
不需要cv::Mat
,但确实需要取operator=
,可以通过将bool
隐式转换为cv::Scalar
来构造。
通过执行cv::Rect(0,img_rows)
,您只需获得图像的前9/16列(所有行),然后称为感兴趣区域(ROI)。
这样做,您正在从cv::Mat
中提取一个名为roi
的子垫。然后使用分配运算符,然后将true
分配给该新子垫的所有像素。
我们可以如下划分代码:
// 1) Extracting the sub-mat from the 'roi' one,and keep it as reference.
cv::Mat& sub_mat = roi(cv::Rect(0,img_rows));
// 2) Assign to all pixel of sub_mat the value true.
sub_mat = true;
这意味着,最后,在前9/16列中,roi设置为true,对于其余7/16,roi设置为0。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。