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

C++ matlab到C++/openCV归一化函数,c++,matlab,opencv,optimization,C++,Matlab,Opencv,Optimization,以下是我的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(

以下是我的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;
std::向量结果;
对于(std::vector::iterator it=index.begin();it!=index.end();++it)
{
int ind=*it;
浮子温度=dst.at(ind-1);
结果:推回(温度);
}
浮点数除法器=t浮点数(结果,99.2);
dst=dst/分配器;
百分位效用函数

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);
}
float-Utils::tprctile(标准::向量通道,双pt)
{       
排序(channel.begin(),channel.end());
int-ptInd=Utilities::MatlabRound(pt/100*channel.size());
返回通道[ptInd];
//Matlab代码
//函数val=tpctile(数据,pt)
//数据=排序(数据);
//ptInd=圆形(pt/100*长度(数据));
//val=数据(ptInd);
}
我的问题是关于
imageData(imageData>1)=1

实现这个函数最有效的方法是什么?当然,我可以像以前一样迭代dst。有更好的办法吗

这就是我目前正在做的事

 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;
       }
    }   
int matrixSize=dst.rows*dst.cols;
cv::Matconditerator_u=dst.begin(),it_end=dst.end();
for(int i=0;i1.0)
{
(i)处的dst=1.0;
}
}   

您想要的是使用cv::threshold截断图像

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

cv::threshold(dst, dst, 1, 1, CV_THRESH_TRUNC);
这将截断1以上的所有值,并将结果存储在dst中