OpenCV cv :: Mat设置是否 [英] OpenCV cv::Mat set if
问题描述
是否有一种简单的方法可以将cv :: Mat中的所有值设置为给定值(如果它们满足某些条件).例如,我有CV_32FC1,我想将所有值设置为0到20.在MATLAB中,我可以简单地做到这一点:
Is there a simple way to set all values in a cv::Mat to a given value if they fulfill some condition. For instance, I have CV_32FC1, and I want set all values which are 0 to 20. In MATLAB I would have simply done this:
M(M == 0) = 20;
推荐答案
您可以使用
cv::Mat mask = M == 0;
M.setTo(0.5, mask);
但是,它包括使用额外的内存来创建掩码,但这是使用opencv API的解决方案,因此可以应用于所有矩阵类型.如果考虑性能问题,则始终可以直接参考Mat :: data以针对具体矩阵类型优化此解决方案.
However, it includes using additional memory for creating mask, but is a solution using opencv API therefore can be applied to all matrix types. If you consider performance issues, you can always refer directly to Mat::data to optimize this solution for concrete matrix type.
这篇关于OpenCV cv :: Mat设置是否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!