将相同的函数应用于android中openCV中图像的所有像素

时间:2017-06-09 07:54:41

标签: android opencv

我在android中使用openCV库3.20进行java编程,我正在尝试对JavaCameraView相机对象捕获的实时图像进行自适应伽马校正,因此我遇到了严重的滞后问题。

目前,代码使用嵌套for循环访问每个像素值并对其应用伽玛校正。有没有一种方法可以更有效地将相同的函数应用于图像的每个像素?

我正在研究论文进行自适应伽马校正 - https://jivp-eurasipjournals.springeropen.com/articles/10.1186/s13640-016-0138-1

我的相同代码 -

    List<Mat> channels = new ArrayList<Mat>();
    MatOfDouble mean=new MatOfDouble();
    MatOfDouble stdDev=new MatOfDouble();

    //convert image to HSV format, going to work with the V channel
    Imgproc.cvtColor(mRgba,mRgba,Imgproc.COLOR_BGR2HSV);

    //split into channels
    Core.split(mRgba,channels);

    //find mean and standard deviation of values channel and map to 0-1 scale
    Core.meanStdDev(channels.get(2),mean,stdDev);
    double stdDevVal=stdDev.get(0,0)[0]/255.0;
    double meanVal=mean.get(0,0)[0]/255.0;


    /*parameter initialization for adaptive gamma filtering
    gamma, heaviside, c, k and newPixelIntensity
    c and gamma are parameters that control shape of transformation curve (gamma transformation)
    k and heaviside are parameters that depict the value of c, newPixelIntensity is the final
    intensity value of each separate pixel

    Relation between c, k and heaviside.
    c=1/(1+Heaviside(0.5-standard deviation)*(k-1))
    Where k is defined by
    k=(oldPixelIntensity)^gamma + (1-(oldPixelIntensity^gamma))*mean^gamma

    newPixelIntensity=c*(oldPixelIntensity)^gamma
    */
    double gamma, heaviside,c,newPixelIntensity,k=0;


   //based on the standard deviation of the pixel intensities, classify the image as low/high contrast
        if(4*stdDevVal<=1/3)
        {
            //since it is low contrast, gamma=-log{base 2} (std. dev) as we need large increase of constrast through gamma
            gamma=-Math.log10(stdDevVal)/Math.log10(2);

            //get heaviside value
            heaviside=heaviside(meanVal);

            for(int i=0;i<channels.get(2).rows();i++)
            {
                for(int j=0;j<channels.get(2).cols();j++)
                {
                    //access the pixel value and map it down to range 0-1
                    double pixelIntensity=channels.get(2).get(i,j)[0]/255.0;



                    if(heaviside==1)
                    {
                        k=Math.pow(pixelIntensity,gamma)+(1-Math.pow(pixelIntensity,gamma))*Math.pow(meanVal,gamma);
                    }

                    //according to k and heaviside, calculate c
                    c=1/(1+heaviside*(k-1));

                    //find new pixel intensity, map it to 0-255 scale and put it back in the Matrix
                    newPixelIntensity=255*c*Math.pow(pixelIntensity,gamma);
                    channels.get(2).put(i,j,newPixelIntensity);

                }
            }

        }

        //high contrast image
        else
        {

            //for high contrast images gamma=  exp((1-(mean+std dev))/2) rest remains the same
            gamma=Math.exp((1-(meanVal+stdDevVal))/2);

            heaviside=heaviside(meanVal);

            for(int i=0;i<channels.get(2).rows();i++)
            {
                for(int j=0;j<channels.get(2).cols();j++)
                {
                    //access the pixel value and map it down to the range 0-1
                    double pixelIntensity=channels.get(2).get(i,j)[0]/255.0;

                    if(heaviside==1)
                    {
                        k=Math.pow(pixelIntensity,gamma)+(1-Math.pow(pixelIntensity,gamma))*Math.pow(meanVal,gamma);
                    }

                    c=1/(1+heaviside*(k-1));
                    //find new pixel intensity, map it to 0-255 scale and put it back in the Matrix
                    newPixelIntensity=255*c*Math.pow(pixelIntensity,gamma);
                    channels.get(2).put(i,j,newPixelIntensity);
                }
            }

        }



    //Merge the updated channels to form the image and convert image back to RGB form
    Core.merge(channels,mRgba);
    Imgproc.cvtColor(mRgba,mRgba,Imgproc.COLOR_HSV2BGR);

实施重质药物功能 -

public static double heaviside(double sigma) {
        if(0.5-sigma>0)
            return 1;
        else
            return 0;
    }

}

0 个答案:

没有答案