android图像边缘提取(Rober算子)

效果展示:

android图像边缘提取(Rober算子)_第1张图片android图像边缘提取(Rober算子)_第2张图片


下面帖主要的图片计算类:

图片灰度化:

public class BitmapUtil {
    public static Bitmap bitmap2Gray(Bitmap bmSrc) {
        // 得到图片的长和宽
        int width = bmSrc.getWidth();
        int height = bmSrc.getHeight();
        // 创建目标灰度图像
        Bitmap bmpGray = null;
        bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        // 创建画布
        Canvas c = new Canvas(bmpGray);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmSrc, 0, 0, paint);
        return bmpGray;
    }

}

Rober算子边缘提取计算类:

public class RobertsEdgeDetect {
    int width;//图像宽
    int height;//图像高
    int[] grayData;//图像灰度值
    int size;  //图像大小
    int gradientThreshold = -1;//判断时用到的阈值
    //BufferedImage outBinary;//输出的边缘图像
    public RobertsEdgeDetect(int threshold) {
        gradientThreshold = threshold;
    }


    public void readImage(Bitmap bitmap) throws IOException {
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        size = width * height;

        //获取图像像素值
        int imageData[] = new int[width * height];
        int count = 0;
        for (int i = 0;i> 16;// 由于读的是灰度图,故只考虑一个分量(三分量值相同)
        }
    }

    public Bitmap createEdgeImage() {
        int[] colors = new int[width * height];
        float[] gradient = gradientM();// 计算图像各像素点的梯度值
        float maxGradient = gradient[0];
        for (int i = 1; i < gradient.length; ++i)
            if (gradient[i] > maxGradient)
                maxGradient = gradient[i];// 获取梯度最大值

        float scaleFactor = 255.0f / maxGradient;// 比例因子用于调整梯度大小

        int[][] cc = new int[width][height];
        if (gradientThreshold >= 0) {
            for (int y = 1; y < height - 1; ++y)
                for (int x = 1; x < width - 1; ++x)
                    if (Math.round(scaleFactor * gradient[y * width + x]) >= gradientThreshold){
                        cc[x][y] = Color.BLUE;
                    }else {
                        cc[x][y] = Color.parseColor("#00000000");
                    }
        }// 对梯度大小进行阈值处理
        int count = 0;
        for (int i = 0;i

参考博客:http://blog.csdn.net/qq_20925635/article/details/53115883


你可能感兴趣的:(android图像边缘提取(Rober算子))