我想实现一个函数,它接受 acv::Mat并将所有负值设置为零。执行此操作的最直接操作似乎是此处threshold记录的函数,以便该函数pos(x)
void pos(cv::Mat x, cv::Mat result)
{
cv::threshold(x, result, 0, 0, CV_THRESH_TOZERO);
return;
}
Run Code Online (Sandbox Code Playgroud)
这是将所有负值设置为零的最合适方法还是有更快或更直接的方法?
尝试pass reference other than value并使用CV_THRESH_TRUNC:
void pos(cv::Mat &src, cv::Mat &dst)
{
cv::threshold(-src, dst, 0, 0, CV_THRESH_TRUNC);
dst = -dst;
return;
}
Run Code Online (Sandbox Code Playgroud)
测试:
[-3, -2, -1;
0, 1, 2]
Run Code Online (Sandbox Code Playgroud)
输出:
[0, 0, 0;
0, 1, 2]
Run Code Online (Sandbox Code Playgroud)
更新:
@Steve建议使用cv::max(x,0)来截断矩阵。经过测试,已经好多了。
void pos(Mat& src, Mat& dst){
dst = cv::max(src, 0);
return void;
}
Run Code Online (Sandbox Code Playgroud)