Android中的Sobel边缘检测

gre*_*nie 8 android image-processing edge-detection

作为我正在为Android开发的应用程序的一部分,我想向用户展示他们拍摄的图像的边缘检测版本(类似于下面的示例).

替代文字

为实现这一目标,我一直在研究Sobel运算符以及如何在Java中实现它.但是,我发现的许多示例都使用AWT中的对象和方法(如本例所示),而不是Android的一部分.

那么我的问题是,Android是否提供了上述示例中使用的AWT功能的替代方案?如果我们仅使用Android内置的库重写该示例,我们将如何进行呢?

Xia*_*ang 6

问题和答案是3年...... @ reflog的解决方案适用于像边缘检测这样的简单任务,但速度很慢.

我在iOS上使用GPUImage进行边缘检测任务.Android上有一个同等的库:https: //github.com/Cyber​​Agent/android-gpuimage/tree/master

它的硬件加速所以它应该非常快.以下是sobel边缘检测过滤器:https: //github.com/Cyber​​Agent/android-gpuimage/blob/master/library/src/jp/co/cyberagent/android/gpuimage/GPUImageSobelEdgeDetection.java

根据文档,你可以简单地这样做:

Uri imageUri = ...;
mGPUImage = new GPUImage(this);
mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
mGPUImage.setImage(imageUri); // this loads image on the current thread, should be run in a thread
mGPUImage.setFilter(new GPUImageSobelEdgeDetection());

// Later when image should be saved saved:
mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用RenderScript,您可以并行访问每个像素并随意执行任何操作.我没有看到任何使用它构建的图像处理库.


ref*_*log 3

由于Android中没有BufferedImage,因此您可以自己完成所有基本操作:

Bitmap b = ...
width = b.getWidth();
height = b.getHeight();
stride = b.getRowBytes();
for(int x=0;x<b.getWidth();x++)
  for(int y=0;y<b.getHeight();y++)
    {
       int pixel = b.getPixel(x, y);
       // you have the source pixel, now transform it and write to destination 
    }
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,这几乎涵盖了移植该 AWT 示例所需的所有内容。(只需更改“convolvePixel”函数)