Ask Your Question
1

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

asked 2019-04-12 01:15:11 -0500

Pou Belle gravatar image

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/... Java result : https://image.noelshack.com/fichiers/...

Java code : https://github.com/LucasLelaidier/Foc...

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;
}
edit retag flag offensive close merge delete

Comments

but the result is not as good.

please be more explicit

also, your code seems to work on CV_8U images, not on CV_8UC3 ones. comment ?

berak gravatar imageberak ( 2019-04-12 02:11:26 -0500 )edit

"please be more explicit"

You can see an example of the result using Java and C++ in the post (both links to the images)

"your code seems to work on CV_8U images, not on CV_8UC3 ones. comment ?"

I'm sorry (this is the first time I've used OpenCV) but I don't understand what you mean. Both algorithms use a PNG image loaded from the hard disk

Pou Belle gravatar imagePou Belle ( 2019-04-12 08:24:35 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-12 09:03:07 -0500

berak gravatar image

see here:

cvtColor(image, gray, COLOR_BGR2GRAY);

probably laps[i].at<Vec3b>(x,y) must be laps[i].at<uchar>(x,y)

edit flag offensive delete link more

Comments

1

Thanks you berak, It was necessary to change Vec3b into uchar in one place

Pou Belle gravatar imagePou Belle ( 2019-04-14 08:00:07 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-04-12 01:15:11 -0500

Seen: 173 times

Last updated: Apr 14