Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在openCV矩阵中查找列最大值的索引和值_C++_Matlab_Opencv - Fatal编程技术网

C++ 在openCV矩阵中查找列最大值的索引和值

C++ 在openCV矩阵中查找列最大值的索引和值,c++,matlab,opencv,C++,Matlab,Opencv,这是原始的MATLAB实现 function[m, p] = max2(im) [m1, k1] = max(im); [m, k2] = max(m1); x = k2; y = k1(k2); p = [y, x]; void Utilities::Max2(cv::Mat input_image, double& m, std::vector<int>& p) { std::vector<double> m1(input_image.

这是原始的MATLAB实现

function[m, p] = max2(im)

[m1, k1] = max(im);
[m, k2] = max(m1);

x = k2;
y = k1(k2);

p = [y, x];
void Utilities::Max2(cv::Mat input_image, double& m, std::vector<int>& p)
{
    std::vector<double> m1(input_image.cols); // the local maximum for each column
    std::vector<int> k1(input_image.cols); // the index of the local maximum
    for (int c = 0; c < input_image.cols; ++c)
    {
        float temp_max = input_image.at<float>(0, c);
        int temp_index = 0;
        for (int r = 0; r < input_image.rows; ++r)
        {
            if (temp_max < input_image.at<float>(r, c))
            {
                temp_max = input_image.at<float>(r, c);
                temp_index = r;
            }
        }
        m1[c] = temp_max;
        k1[c] = temp_index;
    }
    auto iter = std::max_element(m1.begin(), m1.end()); //max of all the local maximum;
    m = *iter;
    int k2 = std::distance(m1.begin(), iter);

    double y = k1[k2];
    p.push_back(y);
    p.push_back(k2);
}
它正在该功能中使用

for r = 2.^linspace(log2(minR),log2(maxR),numSteps);
    itestSeek = imresize(itestBase,minR/r);    
    icorr = normxcorr2(cc,itestSeek);
    [m,p] = max2(icorr); //here
    if (m>bestm)
        bestp = p*r;
        bests = ccSize*r;
        bestm = m;        
    end;
end;

这是我的OpenCV3.0.0/C++实现< /P>

function[m, p] = max2(im)

[m1, k1] = max(im);
[m, k2] = max(m1);

x = k2;
y = k1(k2);

p = [y, x];
void Utilities::Max2(cv::Mat input_image, double& m, std::vector<int>& p)
{
    std::vector<double> m1(input_image.cols); // the local maximum for each column
    std::vector<int> k1(input_image.cols); // the index of the local maximum
    for (int c = 0; c < input_image.cols; ++c)
    {
        float temp_max = input_image.at<float>(0, c);
        int temp_index = 0;
        for (int r = 0; r < input_image.rows; ++r)
        {
            if (temp_max < input_image.at<float>(r, c))
            {
                temp_max = input_image.at<float>(r, c);
                temp_index = r;
            }
        }
        m1[c] = temp_max;
        k1[c] = temp_index;
    }
    auto iter = std::max_element(m1.begin(), m1.end()); //max of all the local maximum;
    m = *iter;
    int k2 = std::distance(m1.begin(), iter);

    double y = k1[k2];
    p.push_back(y);
    p.push_back(k2);
}
void实用程序::Max2(cv::Mat input_图像、双精度&m、标准::矢量&p)
{
std::vector m1(input_image.cols);//每列的局部最大值
std::vector k1(input_image.cols);//局部最大值的索引
对于(int c=0;c
函数的c++用法

std::vector<double> best_p;
std::vector<double> best_s;
for (double i = 0; i < linspace_vector.size(); i++)
{
    cv::Mat i_test_seek;
    cv::Mat i_corr;
    double r = linspace_vector[i];
    double resize_factor = min_r / r; // minR/r in matlab
    cv::resize(i_test_base, i_test_seek, cv::Size(), resize_factor, resize_factor, cv::INTER_CUBIC);
    cv::matchTemplate(i_test_seek, cc_template, i_corr, CV_TM_CCORR_NORMED);

    cv::imshow("i_corr", i_corr);
    cv::waitKey(0);
    double m;
    std::vector<int> p;

    Utilities::Max2(i_corr, m, p);
    if (m>  best_m)
    {
        best_p.clear();
        best_s.clear();
        for (int i = 0; i < p.size(); ++i)
        {
            best_p.push_back(p[i] * r);
        }
        best_s.push_back(cc_size_height * r);
        best_s.push_back(cc_size_width * r);
        best_m = m;
    }
}
std::vector best\u p;
标准::向量最佳值;
for(双i=0;i最佳值)
{
最清晰的;
最佳清除率;
对于(int i=0;i
你能建议一种更有效的方法吗? 我找到每列的局部最大值以及该值的索引


稍后我会找到所有指数的全局最大值。

如果性能提高,您可以尝试以下方法和基准测试吗:

#include <limits>


void Utilities::Max2(cv::Mat input_image, double& m, std::vector<int>& p)
{
    m = std::numeric_limits<double>::min;
    std::pair<int, int> temp_index = 0;

    for (int r = 0; r < input_image.rows; ++r)
    {
        for (int c = 0; c < input_image.cols; ++c)
        {
            if (m < input_image.at<float>(r, c))
            {
                m = input_image.at<float>(r, c);
                temp_index = std::make_pair(c, r);
            }
        }
    }

    p[0] = temp_index.second;
    p[1] = temp_index.first;
}
#包括
void实用程序::Max2(cv::Mat input_图像、双精度&m、标准::矢量&p)
{
m=标准::数值限制::最小值;
标准:对温度指数=0;
对于(int r=0;r
如果有一种方法可以将输入获取为向量,并且可以获取列数,例如使用:

int cols = input_image.rows;
std::vector<double> v;
v.assign(input_image.datastart, input_image.dataend);
int cols=input_image.rows;
std::向量v;
v、 分配(input_image.datastart,input_image.dataend);
然后您可以一次计算:

std::vector<double>::iterator iter = std::max_element(v.begin(), v.end());
double m = *iter;
int k = std::distance(v.begin(), iter);
int y = (int)k / cols;
int x = k % cols;
std::vector::iterator iter=std::max_元素(v.begin(),v.end());
双m=*iter;
int k=std::distance(v.begin(),iter);
int y=(int)k/cols;
int x=k%cols;

但是,我不确定将数据作为向量获取是否是一种选择,也不确定将其转换为向量的性能。也许您可以运行并查看它与您的实现的比较。

您也可以尝试以下操作:

// creating a random matrix with 2 rows and 4 columns
Mat1d mat(2, 4); 
double low = -7000.0;  // minimum value for generating random numbers
double high = +7000.0; // maximum value for generating random numbers
randu(mat, Scalar(low), Scalar(high)); // generating random number matrix

double max_element = *std::max_element(mat.begin(),mat.end()); // get the max element in the matrix
int max_element_index = std::max_element(mat.begin(),mat.end()) - mat.begin(); // get the max_element_index from the matrix`
最大元素索引是一个行主顺序值,从0开始到矩阵中的项数,在本例中为7

cout << mat << endl;
cout << max_element << endl;
cout << max_element_index << endl;

cout根据我的理解,第一段代码基本上是在图像中查找最大值及其索引(x和y)

function[m, p] = max2(im)
    [m1, k1] = max(im); %find the max value in each col
    [m, k2] = max(m1);  %find the max value among maxes

    x = k2;             %find the "row" of the max value
    y = k1(k2);         %and its "col"
    p = [y, x];
这可以使用一些迭代来完成,但迭代几乎总是比向量运算或Opencv函数慢很多

所以,如果我的理解是正确的,这个操作可以通过

double minVal, maxVal;
Point minLoc, maxLoc;
minMaxLoc(im, &minVal, &maxVal, &minLoc, &maxLoc);
maxLoc.y
将给出行,
maxLoc.x
将给出列

更新:您的Matlab代码也可以简化(这可能会加快速度)


更改了标题,因为“查找列的索引”有点让人困惑。假设这是工作代码,您的问题可能更适合于如果映像在内存中是按列的,则切换内部和外部循环可能会提高性能,因为缓存未命中率较低。尽管编译器可能会为您这样做。查找全局最大值应该与您遍历图像的方式无关。为什么您必须坚持matlab的工作方式?为什么不使用
minMaxLoc
?您可以直接使用
Mat
迭代器。它们还可以正确处理非连续矩阵