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

Hub*_*ada 0 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);
Run Code Online (Sandbox Code Playgroud)

Dav*_*bin 5

首先,有没有您不使用的原因

Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy);
Run Code Online (Sandbox Code Playgroud)

我不确定这里使用的Java语法。的puts 块何时执行?是否假设您定义的Mat子类的构造函数的一部分?话虽如此,假设这样做的目的与预期相符,则可能未正确指定输出文件类型。的值是destinationPath多少?

您是否尝试过在备用图像查看器中打开保存的文件以确定是ShowImage()代码还是错误的保存文件?

您是否尝试过在十六进制编辑器中打开保存的文件以查看其外观值是否“合理”或全为零?