有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java卷积内核映像作为镜像出现

所以我有一些卷积灰度图像的代码,在Java中使用卷积内核。它似乎运行得相当好。然而,这幅图像显示为镜像。好像是从行的末尾而不是开始复制。我想知道有谁能帮我理解这里发生了什么

问题似乎出在convertToArrayLocation()方法中,就好像我试图从该方法生成的数组中重新创建一个图像一样,该图像是镜像的

public class GetTwoDimensionalPixelArray {
    public static BufferedImage inputImage, output;

    public static final int[][] IDENTITY = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
    public static final int[][] EDGE_DETECTION_1 = {{-1, -1, -1}, {-1, 8, -1}, {-1, -1, -1}};

    public static int[][] SHARPEN = {{0, -1, 0}, {-1, 5, -1}, {0, -1, 0}};
    public static int WIDTH, HEIGHT;
    public static int order = SHARPEN.length;

    public static void main(String[] args) throws IOException {
        System.out.println(WIDTH);
        BufferedImage inputImage = ImageIO.read(new File("it-gs.png")); // load the image from this current folder
        WIDTH = inputImage.getWidth();
        HEIGHT = inputImage.getHeight();

        int[][] result = convertToArrayLocation(inputImage); // pass buffered image to the method and get back the
        // result
        System.out.println("height" + result.length + "width" + result[0].length);

        int[][] outputarray = convolution2D(result, WIDTH, HEIGHT, EDGE_DETECTION_1, EDGE_DETECTION_1.length,
                EDGE_DETECTION_1.length);

        int opwidth = outputarray[0].length;
        int opheight = outputarray.length;
        System.out.println("W" + opwidth + "H" + opheight);

        BufferedImage img = new BufferedImage(opheight, opwidth, BufferedImage.TYPE_BYTE_GRAY);
        for (int r = 0; r < opheight; r++) {
            for (int t = 0; t < opwidth; t++) {
                img.setRGB(r, t, outputarray[r][t]);
            }
        }
        try {
            File imageFile = new File("C:\\Users\\ciara\\eclipse-workspace\\it.png");
            ImageIO.write(img, "png", imageFile);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private static int[][] convertToArrayLocation(BufferedImage inputImage) {
        final byte[] pixels = ((DataBufferByte) inputImage.getRaster().getDataBuffer()).getData();
        // get pixel value as single array from buffered Image
        final int width = inputImage.getWidth(); // get image width value
        final int height = inputImage.getHeight(); // get image height value
        System.out.println("height" + height + "width");
        int[][] result = new int[height][width]; // Initialize the array with height and width
        // this loop allocates pixels value to two dimensional array
        for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel++) {
            int argb = 0;
            argb = (int) pixels[pixel];
            // if pixel value is negative, change to positive //still weird to me
            if (argb < 0) {
                argb += 256;
            }
            result[row][col] = argb;
            col++;
            if (col == width) {
                col = 0;
                row++;
            }
        }
        return result;
    }

    public static int[][] convolution2D(int[][] input, int width, int height,
                                        int[][] kernel, int kernelWidth, int kernelHeight) {
        int smallWidth = width - kernelWidth + 1;
        int smallHeight = height - kernelHeight + 1;
        int[][] output = new int[smallHeight][smallWidth];
        for (int i = 0; i < smallHeight; ++i) {
            for (int j = 0; j < smallWidth; ++j) {
                output[i][j] = 0;
            }
        }
        for (int i = 0; i < smallHeight; ++i) {
            for (int j = 0; j < smallWidth; ++j) {
                output[i][j] = singlePixelConvolution(input, i, j, kernel, kernelWidth, kernelHeight);
            }
        }
        return output;
    }

    public static int singlePixelConvolution(int[][] input, int x, int y, int[][] k,
                                             int kernelWidth, int kernelHeight) {
        int output = 0;
        for (int i = 0; i < kernelHeight; ++i) {
            for (int j = 0; j < kernelWidth; ++j) {
                try {
                    output = output + (input[x + i][y + j] * k[i][j]);
                } catch (Exception e) {
                    continue;
                }
            }
        }
        return output;
    }
}

共 (0) 个答案