使用Java中的OpenCV 2.4.10在jpg图片上应用sobel滤镜

时间:2015-05-17 23:23:35

标签: java opencv sobel

我正在尝试使用Java在jpg图片上添加sobel运算符。我在这里找到了示例:http://www.tutorialspoint.com/java_dip/applying_sobel_operator.htm但它不起作用。相反,它打印黑色图像。有人可以向我解释我做错了吗?其他imgproc函数效果很好。

这是我的代码:

 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);

1 个答案:

答案 0 :(得分:4)

首先,你有没有使用

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

我不确定这里使用的Java语法。什么时候put的块被执行了?它是否假定为您定义的Mat的子类的构造函数的一部分?话虽如此,假设这样做的目的是它的意图,那么你的输出文件类型可能没有被正确指定; destinationPath的价值是什么?

您是否尝试在备用图片查看器中打开已保存的文件,以确定它是ShowImage()代码还是保存的文件?

您是否尝试在十六进制编辑器中打开已保存的文件以查看它是否具有“合理”的值或全部为零?