我的代码中有这个表达式:
*h = ((*R) / ((*G) + (*R)));
其中 R、G 和 B 是 Mat 指针,uchar
因为存在除法我想将答案转换为 Mat 指针float
所以我尝试了这个
*h = Mat_<float>((*R) / ((*G) + (*R)));
但我得到了分段错误。
我该怎么做?
您需要将矩阵 *R 转换为浮点数。使用方法Mat::convert_to
。假设 R8 是 uchar 的矩阵,您可以按如下方式创建浮点矩阵:
Mat R32; R8.convertTo(R32,CV_32F);
这在OpenCV简介中得到了很好的解释。
Note 1: This expression *h = Mat_<float>((*R) / ((*G) + (*R)))
performs division using uchars and later re-interpret the result as float, which is wrong. Mat_<float>
is only to simplify element access for arrays that are already floats.
Note 2: You don't need to use pointers to Mat most of the time.