bytedeco / javacpp-presets

The missing Java distribution of native C++ libraries
Other
2.66k stars 740 forks source link

Applying Tan Triggs algorithm for better result in different light condition #566

Open pratikvyas1991 opened 6 years ago

pratikvyas1991 commented 6 years ago

Hello All, I am trying to implement the javacv for facial recognition, It gives a good result but when we move to different lighting condition it fails to recognize, so I searched for the solution I have got Three algorithms namely 1) histogram equalization,2) CLAHE,3) tan-Triggs normalization among which Tan Triggs algorithm seems good as I read in many online platforms, Can you please guide me how to implement this algorithm in JavaCv so that we can get good result in different lighting condition .I even Tried to implemt the function in the java class but it slows the camera preview and even stopped detecting the faces so I choosed to seek help here . I am referring to this cpp file https://github.com/bytefish/opencv/blob/master/misc/tan_triggs.cpp Thanks in Advance Pratik Vyas

saudet commented 6 years ago

That file looks pretty easy to port to Java. Please give it a try yourself, and if you encounter any problems, please let me know which line you're having problems with and I will help.

pratikvyas1991 commented 6 years ago

I have tried to write it in java

Mat tan_triggs_preprocessing(Mat src) {

        float alpha = 0.1f, tau = 10.0f, gamma = 0.2f;
        int sigma0 = 1;
        int sigma1 = 2;
        // Convert to floating point:
        Mat X = src;
        X.convertTo(X, CV_32FC1);
        // Start preprocessing:
        Mat I = new Mat();
        pow(X, gamma, I);
        // Calculate the DOG Image:
        {
            Mat gaussian0 = new Mat(), gaussian1 = new Mat();
            // Kernel Size:
            int kernel_sz0 = (3 * sigma0);
            int kernel_sz1 = (3 * sigma1);
            // Make them odd for OpenCV:
            kernel_sz0 += ((kernel_sz0 % 2) == 0) ? 1 : 0;
            kernel_sz1 += ((kernel_sz1 % 2) == 0) ? 1 : 0;
            GaussianBlur(I, gaussian0, new Size(kernel_sz0, kernel_sz0), sigma0, sigma0, BORDER_REPLICATE);
            GaussianBlur(I, gaussian1, new Size(kernel_sz1, kernel_sz1), sigma1, sigma1, BORDER_REPLICATE);
//            subtract(gaussian0, gaussian1, I);
        }

        {
            double meanI = 0.0;
            {
                Mat tmp = new Mat();
                opencv_core.MatExpr absMat = abs(I);

                Mat myMat = absMat.asMat();
                pow(myMat, alpha, tmp);
                opencv_core.Scalar mean = mean(tmp);
                meanI = mean.get();

            }
            Double pows = Math.pow(meanI, 1.0 / alpha);
            divide(I, pows);
//            I = I / pows;
        }

        {
            double meanI = 0.0;
            {
                Mat tmp = new Mat();
                opencv_core.MatExpr absMat = abs(I);
                Mat myMat = absMat.asMat();
                opencv_core.MatExpr minMat = min(tau, myMat);
                Mat minMyMat = minMat.asMat();
                pow(minMyMat, alpha, tmp);
//                meanI = mean(tmp).val[0];
                opencv_core.Scalar mean = mean(tmp);
                meanI = mean.get();
            }
//            I = I / pow(meanI, 1.0/alpha);
            Double pows = Math.pow(meanI, 1.0 / alpha);
            divide(I, pows);
        }

        // Squash into the tanh:
        {
            Mat exp_x = new Mat(), exp_negx = new Mat();

            opencv_core.MatExpr tempMat = divide(I, tau);
            Mat mat = tempMat.asMat();
            exp(mat, exp_x);
//            exp( -I / tau, exp_negx );
            Mat divisor1 = new Mat();
            Mat divisor2 = new Mat();

//            absdiff(exp_x,exp_negx,divisor1);
//
//            opencv_core.MatExpr tempdivExpr = add(exp_x,exp_negx);
//            divisor2 = tempdivExpr.asMat();
//            divide(divisor1, divisor2, I);

            opencv_core.MatExpr tempmultExpr = multiply(I,tau);

            I = tempmultExpr.asMat();
        }
        return I;
    }

Mat norm0255(Mat src) {
        // Create and return normalized image:
        Mat dst =new Mat();
        Mat mask =new Mat();
        switch(src.channels()) {
            case 1:
                normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC1,mask);
//                normalize(src,dst);
                break;
            case 3:
                normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC3,mask);
                break;
            default:
                src.copyTo(dst);
                break;
        }
        return dst;
    }

It made my camera preview slow and even stopped detection

saudet commented 6 years ago

Could you try to run a profiler on that and show me the results?

pratikvyas1991 commented 6 years ago

profiler here you mean "haarcascade_profileface.xml" correct me If I am wrong.

saudet commented 6 years ago

No, I mean something like https://docs.oracle.com/javase/8/docs/technotes/guides/visualvm/intro.html

Kunnu123 commented 6 years ago

Hello, I also face same problem of lighting. I try many things and algorithm but i can't find anything. Please do needful in it. Do you have any specific algorithm which gives same result in different lighting condition ??@saudet Thanks in Advance !