Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Focus stacking with C++ are not the same as in Java

Firstly, sorry for my english, it's not my mother tongue I have to implement a system of focus stacking in a software, I found an algorithm on Github in Java and I transcribed it in C ++ but the result is not as good. I use OpenCV Thank you in advance

PS : if you have a better algorithm in C ++, I do not refuse ^^

C++ result : https://image.noelshack.com/fichiers/2019/15/4/1554990509-test.jpg Java result : https://image.noelshack.com/fichiers/2019/15/4/1554990509-fusion.png

Java code : https://github.com/LucasLelaidier/Focus-stacking-java/blame/master/FocusStacking.java

My code :

FocusStacking::FocusStacking(vector<Mat> img) : m_img(img)
{

}

Mat FocusStacking::laplacien(Mat image)
{
    int kernel_size = 5;
    int blur_size = 5;

    Mat gray;
    cvtColor(image, gray, COLOR_BGR2GRAY);

    Mat gauss;
    Size size(blur_size, blur_size);
    GaussianBlur(gray, gauss, size, 0);

    Mat laplace;
    Laplacian(gauss, laplace, CV_64F, kernel_size, 1, 0);

    Mat absolute;
    convertScaleAbs(laplace, absolute);

    return absolute;
}

Mat FocusStacking::work()
{
    vector<Mat> laps;
    for (unsigned i = 0 ; i < m_img.size() ; i++)
    {
        laps.push_back(laplacien(m_img[i]));
    }
    Mat stack = Mat(laps[0].size(), m_img[0].type(), double(0));

    int index;
    int indexValue;
    for(int y = 0 ; y < laps[0].cols ; y++)
    {
        for(int x = 0 ; x < laps[0].rows ; x++)
        {
            index = -1;
            indexValue = -1;
            for(unsigned int i = 0 ; i < laps.size(); i++)
            {
                if(indexValue < 0 || laps[i].at<Vec3b>(x,y)[0] > indexValue)
                {
                    indexValue = laps[i].at<Vec3b>(x,y)[0];
                    index = static_cast<int>(i);
                }
            }
            stack.at<Vec3b>(x,y) = m_img[static_cast<unsigned>(index)].at<Vec3b>(x,y);
        }
    }
    return stack;
}