Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

how to implement imgradient() function of matlab in opencv android java

I want to use the imgradient() function of matlab in my android application using opencv. how can i do so and which function of opencv is equivalent to to Matlab imgradient() function.

i m using below mentioned function is it right ?

public Mat imgradient(Mat grayScaleImage)
    {
        Mat grad_x=new Mat();
        Mat grad_y = new Mat();
        Mat abs_grad_x=new Mat();
        Mat abs_grad_y=new Mat();            
        Mat gradientImag = new Mat(grayScaleImage.rows(),grayScaleImage.cols(),CvType.CV_8UC1);

         Imgproc.Sobel(grayScaleImage, grad_x, CvType.CV_16S, 1, 0,3,1,0,Imgproc.BORDER_DEFAULT );
         Core.convertScaleAbs( grad_x, abs_grad_x );             
         Imgproc.Sobel( grayScaleImage, grad_y, CvType.CV_16S, 0, 1, 3, 1,0,Imgproc.BORDER_DEFAULT );
         Core.convertScaleAbs( grad_y, abs_grad_y );                
         double[] buff_grad = new double[1];
         for(int i = 0; i < abs_grad_y.cols(); i++)
            {
                for(int j =0 ; j<abs_grad_y.rows() ; j++)
                {
                    double[] buff_x = abs_grad_x.get(j, i);
                    double[] buff_y = abs_grad_y.get(j, i);
                    double x =  buff_x[0];
                    double y =  buff_y[0];
                    double ans=0;
                    try
                    {
                         ans = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
                    }catch(NullPointerException e)
                    {
                        ans = 0;

                    }
                    buff_grad[0] =  ans;                        
                    gradientImag.put(j, i, buff_grad);   
                }
            }           
        return gradientImag;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried using something like sobel or canny operators?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...