Applying sobel filter on jpg picture with OpenCV 2.4.10 in Java

I'm trying to add sobel operator on a jpg picture with Java. I found example here: http://www.tutorialspoint.com/java_dip/applying_sobel_operator.htm but it doesn't work. Instead it prints black image. Could someone explain to me what I did wrong, please? Other imgproc functions works well.

Here is my code:

 Mat sourceImage = Highgui.imread(sourcePath,  Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    Mat destinationImage = new Mat(sourceImage.rows(), sourceImage.cols(), sourceImage.type());

    Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F){
        {
           put(0,0,-1);
           put(0,1,0);
           put(0,2,1);

           put(1,0-2);
           put(1,1,0);
           put(1,2,2);

           put(2,0,-1);
           put(2,1,0);
           put(2,2,1);
        }
     };      

    Imgproc.filter2D(sourceImage, destinationImage, -1, kernel);          
    Highgui.imwrite(destinationPath, destinationImage);

    //display
    new ShowImage(sourcePath, sourceImage);
    new ShowImage(destinationPath, destinationImage);

First up, is there a reason why you're not using

Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy);

I'm not sure of the Java syntax used here. When does the block of put s get executed? Is it assumed to part of a constructor for a subclass of Mat that you define ? Having said that, assuming that this does what it looks like it's intended to do then it's possible that your output file type is not correctly specified; what is the value of destinationPath ?

Have you tried opening the saved file in an alternative image viewer to determine if it's the ShowImage() code or the saved file that's at fault ?

Have you tried opening the saved file in a hex editor to see if it has 'sensible' looking values or is all zeros ?

链接地址: http://www.djcxy.com/p/72514.html

上一篇: 索贝尔操作符不适用于矩形图像

下一篇: 在Java中使用OpenCV 2.4.10在jpg图片上应用sobel过滤器