【问题标题】:matlab to C++/openCV normalization functionmatlab 到 C++/openCV 归一化函数
【发布时间】:2015-03-09 16:38:21
【问题描述】:

这是我的matlab代码:

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

这是我的 openCV/c++ 代码,矩阵 dst 是一个 openCV 矩阵
cv::Mat 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&gt;1) = 1 实现此功能的最有效方法是什么 - 我当然可以像我一样遍历 dst 。有没有更好的办法?

【问题讨论】:

    标签: c++ matlab opencv optimization


    【解决方案1】:

    你想要的是用 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

    【讨论】:

      【解决方案2】:

      这就是我现在正在做的事情

       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;
             }
          }   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-04
        • 2015-07-25
        • 2023-03-22
        • 1970-01-01
        • 2020-04-04
        • 2011-10-11
        • 1970-01-01
        • 2013-03-30
        相关资源
        最近更新 更多