使用Java在android中进行sobel边缘检测

时间:2018-08-22 13:04:49

标签: java android

我比较新鲜,我正在尝试使用sobel运算符来检测图像中的边缘,但不幸的是,我被卡住了,并且得到了错误的结果。 我已经更加努力地尝试并在互联网上进行搜索,但是没有找到问题的解决方案,所以我正在寻求帮助。这是我使用的代码

  bitmap2Set = biit.grayScale(bitmap2Decode);
            bitmapWidth = bitmap2Set.getWidth();
            bitmapHeight = bitmap2Set.getHeight();
            loc=new int[bitmapWidth*bitmapHeight];
            bitmap2Set.getPixels(loc,0,bitmapWidth,0,0,bitmapWidth,bitmapHeight);
            Bitmap  bitmap2= Bitmap.createBitmap(bitmapWidth,bitmapHeight, Bitmap.Config.ARGB_8888);
            for (int x = 1; x < bitmapWidth - 1; x++) {
                for (int y = 1; y < bitmapHeight - 1; y++) {
                    int a10 = Color.rgb((x - 1) + (y - 1) * bitmapWidth, (x - 1) + (y - 1) * bitmapWidth, (x - 1) + (y - 1) * bitmapWidth);
                    int a11 = Color.rgb((x - 1) + y * bitmapWidth, (x - 1) + y * bitmapWidth, (x - 1) + y * bitmapWidth);
                    int a13 = Color.rgb((x - 1) + (y + 1) * bitmapWidth, (x - 1) + (y + 1) * bitmapWidth, (x - 1) + (y + 1) * bitmapWidth);
                    int b10 = Color.rgb(x + (y - 1) * bitmapWidth, x + (y - 1) * bitmapWidth, x + (y - 1) * bitmapWidth);
                    int b11 = Color.rgb(x + y * bitmapWidth, x + y * bitmapWidth, x + y * bitmapWidth);
                    int b13 = Color.rgb(x + (y + 1) * bitmapWidth, x + (y + 1) * bitmapWidth, x + (y + 1) * bitmapWidth);
                    int c10 = Color.rgb((x + 1) + (y - 1) * bitmapWidth, (x + 1) + (y - 1) * bitmapWidth, (x + 1) + (y - 1) * bitmapWidth);
                    int c11 = Color.rgb((x + 1) + y * bitmapWidth, (x + 1) + y * bitmapWidth, (x + 1) + y * bitmapWidth);
                    int c13 = Color.rgb((x + 1) + (y + 1) * bitmapWidth, (x + 1) + (y + 1) * bitmapWidth, (x + 1) + (y + 1) * bitmapWidth);
                    int gx = ((-1 * a10) + (0 * a11) + (1 * a13) + (-2 * b10) + (0 * b11) + (2 * b13) + (-1 * c10) + (0 * c11) + (1 * c13));
                    int gy = ((-1 * a10) + (2 * a11) + (-1 * a13) + (0 * b10) + (0 * b11) + (0 * b13) + (1 * c10) + (2 * c11) + (1 * c13));
                    double gradient = Math.sqrt((gx * gx) + (gy * gy));
                    int magnitude = (int) gradient;
                    if (threshold < magnitude) {
                        threshold = magnitude;
                        loc[x + y * bitmapWidth] = magnitude;
                    }
                    int edgeCol=loc[x+y*bitmapWidth];
                    edgeCol=(int)(edgeCol*scale);
                    edgeCol= 0xff000000 | (edgeCol >> 16)  | (edgeCol >> 8) | edgeCol ;
                    loc[x+y*bitmapWidth]=edgeCol;
                }
            }
                bitmap2.setPixels(loc,0,bitmapWidth,0,0,bitmapWidth,bitmapHeight);
                imageView = (ImageView) findViewById(R.id.img);
                imageView.setImageBitmap(bitmap2Set);

当我尝试设置新像素时,我已将图像转换为灰度图像,它再次返回相同的灰度图像,而不是带有边缘的图像 这是灰度代码

 bitmap.getPixels(pixels,0,width,0,0,width,height);
       Bitmap  bitmap2= Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                location = x + y * width;
                r = Color.red(pixels[location]);
                g = Color.green(pixels[location]);
                b = Color.blue(pixels[location]);
                r = (r + g + b) / 3;
                g = r;
                b = r;
                pixels[location] = Color.rgb(r, g, b);
            }
        }
        bitmap2.setPixels(pixels,0,width,0,0,width,height);
       return bitmap2;

0 个答案:

没有答案