matlab to C++/openCV 归一化函数

matlab to C++/openCV normalization function

本文关键字:函数 openCV to C++ matlab      更新时间:2023-10-16

这是我的matlab代码:

imageData = imageData ./ toolbox.c3d.p.tprctile(imageData(xy),99.2);
imageData(imageData>1) = 1;

这是我的 openCV/C++ 代码,矩阵 dst 是一个 openCV 矩阵
cv::垫子 dst

std::vector<float> result;
for (std::vector<int>::iterator it = index.begin() ; it != index.end(); ++it)
{
    int ind = *it;
    float temp = dst.at<float>(ind - 1);    
    result.push_back(temp);
}
float divider = tprctile(result,99.2);
dst = dst/ divider;

百分位数的效用函数

float Utils::tprctile(std::vector<float> channel, double pt)
{       
    std::sort(channel.begin(),channel.end());   
    int ptInd = Utilities::MatlabRound (pt/100 * channel.size() );
    return channel[ptInd];
    // Matlab code
    // function val = tprctile(data, pt)
    //   data = sort(data);
    //   ptInd = round( pt/100 * length(data) );
    //   val = data(ptInd);
}

我的问题是关于imageData(imageData>1) = 1实现此功能的最有效方法是什么 - 我当然可以像以前一样遍历 DST。有没有更好的方法?

你想要的是

用cv::threshold截断图像。

以下内容应满足您的要求:

cv::threshold(dst, dst, 1, 1, CV_THRESH_TRUNC);

这将截断所有大于 1 的值,并将结果存储在 dst 中。

http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#threshold

这就是

我目前正在做的事情

 int matrixSize = dst.rows *dst.cols;
    cv::MatConstIterator_<float> it = dst.begin<float>(), it_end = dst.end<float>();
    for(int i = 0 ; i < matrixSize ; ++i, ++it)
    {
       float value = *it;
       if(value > 1.0)
       {
          dst.at<float>(i) = 1.0;
       }
    }