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/... 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;
}
please be more explicit
also, your code seems to work on CV_8U images, not on CV_8UC3 ones. comment ?
"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