0

Is there an equivalent of 'smoothstep' function in opencv (java)?It is commonly used in opengl/glsl and is available as a standard built-in function in many other standard image-processing libraries.May be we can implement it using combination of threshold/clamping functions. What is the best or most efficient way to use smoothstep function with opencv mat objects?

Here is a sample code:-

float smoothstep(float edge0, float edge1, float x) {
        // Scale, bias and saturate x to 0..1 range
        x = clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
        // Evaluate polynomial
        return x * x * (3 - 2 * x);
    }

    float clamp(float x, float lowerlimit, float upperlimit) {
        if (x < lowerlimit)
            x = lowerlimit;
        if (x > upperlimit)
            x = upperlimit;
        return x;
    }

Imgproc.GaussianBlur(mskmat, mskmat,new org.opencv.core.Size(3,3), 0);


    Utils.matToBitmap(mskmat,bitmap);
    save(bitmap,"mask");

    //Smoothing

    mskmat.convertTo(mskmat,CV_32F);

    for (int i=0; i<mskmat.rows(); i++)
    {
      for (int j=0; j<mskmat.cols(); j++)
      {
        double[] data = mskmat.get(i, j); //Stores element in an array

        for (int k = 0; k < 1; k++) //Runs for the available number of channels
        {
          data[k] = 255.0f * smoothstep(0.3f,0.5f, (float)data[k]/255.0f); //Pixel modification done here
        }
        Log.d("Value", Arrays.toString(data));
        mskmat.put(i, j, data); //Puts element back into matrix
      }
    }

    mskmat.convertTo(mskmat,CV_8U);

    Utils.matToBitmap(mskmat,bitmap);
    save(bitmap,"smoothmask");

However this method seems to be too slow and output is not as good as the standard smoothstep function.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
anilsathyan7
  • 1,423
  • 17
  • 25

2 Answers2

0

There is no equivalent for the smoothstep function in OpenCV.

Stephen Meschke
  • 2,820
  • 1
  • 13
  • 25
0

AFAIK OpenCV does not offer an equivalent of the smoothstep GLSL builtin. A smoothstep() function itself is not hard to implement (check Smoothstep on Wikipedia) and can be done in a few lines of code.

Not sure what your use-case is, so I am guessing that since you are mentioning using smoothstep() on matrices/images, you want to blend between two images. If that is the case, simply take the output of the smoothstep() function and perform a linear combination using the smoothstep and 1-smoothstep.

Martin
  • 116
  • 2