图像卷积不根据实际算法工作

我已经在 java 中实现了一个卷积过滤器。我在 ap cs 中做过一段时间,但现在我确实需要它来做某事,所以我重新实现了它以确保我仍然知道如何去做。不幸的是,我丢失了我的工作副本,所以我无法将当前代码与我以前的工作代码进行比较。我很确定我正确地实现了算法,但代码仍然无法正常工作。有经验的程序员能解释一下我做错了什么吗?


这是卷积类:


import java.awt.*;

import java.util.Arrays;


public class ConvolutionFilter {


    private int[][] image;

    private int[][] weights;

    private double[][] doubleWeights;


    private int[][] convolved;


    public ConvolutionFilter(int[][] image, int[][] weights) {


        this.image = image;

        this.weights = weights;


        convolve();

    }


    public void convolve() {


        int sum;

        int[][] convolved = new int[image.length][image[0].length];


        for (int r = 0; r < convolved.length - weights.length - 1; r++) {

            for (int c = 0; c < convolved[r].length - weights.length - 1; c++) {

                sum = 0;

                for (int i = 0; i < weights.length; i++) {

                    for (int j = 0; j < weights[i].length; j++) {

                        sum += image[r + i][c + j] * weights[i][j];

                    }

                }

                convolved[r][c] = sum / weight();

            }

        }


        this.convolved = convolved;

    }


    public int numWeights() {

        return weights.length * weights[0].length;

    }


    public int weight() {

        int sum = 0;

        for (int r = 0; r < weights.length; r++) {

            for (int c = 0; c < weights[r].length; c++) {

                sum += weights[r][c];

            }

        }

        if (sum == 0) return 1; else return sum;

    }


    public int[][] getConvolved() {

        return convolved;

    }


}

任何帮助表示赞赏!


慕工程0101907
浏览 200回答 1
1回答

慕盖茨4494581

为了使它适应 RGB,算法应该在每个通道上完成,而不是在打包表示上完成,例如(未测试)public void convolve() {&nbsp; &nbsp; int[][] convolved = new int[image.length][image[0].length];&nbsp; &nbsp; double invScale = 1.0 / weight();&nbsp; &nbsp; for (int r = 0; r < convolved.length - weights.length - 1; r++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int c = 0; c < convolved[r].length - weights.length - 1; c++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int rsum = 0, gsum = 0, bsum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < weights.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < weights[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pixel = image[r + i][c + j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int w = weights[i][j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsum += ((pixel >> 16) & 0xFF) * w;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gsum += ((pixel >> 8) & 0xFF) * w;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bsum += (pixel & 0xFF) * w;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsum = (int)(rsum * invScale);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gsum = (int)(gsum * invScale);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bsum = (int)(bsum * invScale);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; convolved[r][c] = bsum | (gsum << 8) | (rsum << 16) | (0xFF << 24);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; this.convolved = convolved;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java