Ask Your Question

Revision history [back]

Look like you try to get intensity values of divided sub-images, in order, from a bigger one and store them in a Mat or a vector of double? IMO, real code makes it is easier to get real answers. However, my answer is: you do not need an extra statement before the push_back call of singleGridIntensity, just performing a direct emplace_back:

singleGridIntensity.emplace_back(double(inputImage.at<uchar>(yPose, xPose)));

Indeed, the trick here is emplace_back function (a C++ 11 one). In my experience, it often gives about 20% improvement in speed, especially with large inputs and a huge number of calls. If the numbers of intensity values of grids are the same, you can declare a local vector and do a direct assignment:

singleGridIntensity[i++]=double(inputImage.at<uchar>(yPose, xPose));

Then, with the allGridIntensity variable: change push_back to emplace_back and, if you run it thousands of times, do not return a Mat or a vector, just pass a reference of that type in the parameter list and change its content as your expected return values, this will offer another enhancement in speed. Finally, the code will be something like that:

void getIntensity (Grids &inputGrids, Mat inputImage, vector<type> & allGridIntensity)
{
    for()//takes single grid
    {
        vector<type> singleGridIntensity[N];
        for(i=0;i<N;)//takes single point
            singleGridIntensity[i++] = type(inputImage.at<uchar>(yPose, xPose)); // a type cast

        allGridIntensity.emplace_back(singleGridIntensity);
    }
}

Also, try to access each pixel by pointer (like from this link), you may gain some more improvements.

Look like you try to get intensity values of divided sub-images, in order, from a bigger one and store them in a Mat or a vector of double? IMO, real code makes it is easier to get real answers. However, my answer is: you do not need an extra statement before the push_back call of singleGridIntensity, just performing a direct emplace_back:

singleGridIntensity.emplace_back(double(inputImage.at<uchar>(yPose, xPose)));

Indeed, the trick here is emplace_back function (a C++ 11 one). In my experience, it often gives about 20% improvement in speed, especially with large inputs and a huge number of calls. If the numbers of intensity values of grids are the same, you can declare a local vector and do a direct assignment:

singleGridIntensity[i++]=double(inputImage.at<uchar>(yPose, xPose));

Then, with the allGridIntensity variable: change push_back to emplace_back and, if you run it thousands of times, do not return a Mat or a vector, just pass a reference of that type in the parameter list and change its content as your expected return values, this will offer another enhancement in speed. Finally, the code will be something like that:this:

void getIntensity (Grids &inputGrids, Mat inputImage, vector<type> & allGridIntensity)
{
    for()//takes single grid
    {
        vector<type> singleGridIntensity[N];
        for(i=0;i<N;)//takes single point
            singleGridIntensity[i++] = type(inputImage.at<uchar>(yPose, xPose)); // a type cast

        allGridIntensity.emplace_back(singleGridIntensity);
    }
}

Also, try to access each pixel by pointer (like from this link), you may gain some more improvements.