1

我正在做一项任务,要求我们开发一个程序,对具有不同过滤器尺寸 3x3、5x5...11x11 的灰度图像执行平均过滤器

首先我用Java开发了一个矩阵类:

final public class Matrix {
    private final int M;             // number of rows
    private final int N;             // number of columns
    private final double[][] data;   // M-by-N array

    // create M-by-N matrix of 0's
    public Matrix(int M, int N) {
        this.M = M;
        this.N = N;
        data = new double[M][N];
    }

    // create matrix based on 2d array
    public Matrix(double [][] data) {
        M = data.length;
        N = data[0].length;
        this.data = new double[M][N];
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                    this.data[i][j] = data[i][j];
    }

     public static Matrix filter(int M, int N) {
        Matrix A = new Matrix(M, N);
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                A.data[i][j] = (1.0/9.0);
        return A;
    }

    // copy constructor
    private Matrix(Matrix A) { this(A.data); }

    // return C = A + B
    public Matrix plus(Matrix B) {
        Matrix A = this;
        if (B.M != A.M || B.N != A.N) throw new RuntimeException("Illegal matrix dimensions.");
        Matrix C = new Matrix(M, N);
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                C.data[i][j] = A.data[i][j] + B.data[i][j];
        return C;
    }

// return C = A - B
    public Matrix minus(Matrix B) {
        Matrix A = this;
        if (B.M != A.M || B.N != A.N) throw new RuntimeException("Illegal matrix dimensions.");
        Matrix C = new Matrix(M, N);
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                C.data[i][j] = A.data[i][j] - B.data[i][j];
        return C;
    }
    public boolean eq(Matrix B) {
        Matrix A = this;
        if (B.M != A.M || B.N != A.N) throw new RuntimeException("Illegal matrix dimensions.");
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                if (A.data[i][j] != B.data[i][j]) return false;
        return true;
    }

    // return C = A * B
    public Matrix multiply(Matrix B) {
        Matrix A = this;
        if (A.N != B.M) throw new RuntimeException("Illegal matrix dimensions.");
        Matrix C = new Matrix(A.M, B.N);
        for (int i = 0; i < C.M; i++)
            for (int j = 0; j < C.N; j++)
                for (int k = 0; k < A.N; k++)
                    C.data[i][j] +=  (A.data[i][k] * B.data[k][j]);
        return C;
    }
    public double average (){
        double sum=0.0;
        for (int i = 0; i < M; i++) {
                        for (int j = 0; j < N; j++) {
                                sum = sum + data[i][j];
                        }
        }
        return sum;


    }
    public void show() {
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) 
                System.out.printf("%9.4f ", data[i][j]);
            System.out.println();
        }
    }



}

然后我开发了我的图像过滤应用程序,如下所示:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Yousra
 */
public class ImgfilterApplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



        System.out.println("Please Enter Your Image Path Here ...");
        Scanner myscanner = new Scanner(System.in);
        String path = myscanner.next();
        BufferedImage img = getImage(path);
       int filtersize = 3;


       BufferedImage outimg = TDfilter(img, filtersize);
       JFrame frame = new JFrame();
       JLabel image = new JLabel(new ImageIcon("imageName.png"));
       frame.getContentPane().setLayout(new FlowLayout());
       frame.getContentPane().add(new JLabel(new ImageIcon(img)));
       frame.getContentPane().add(new JLabel(new ImageIcon(outimg)));
       frame.pack();
       frame.setVisible(true);




    }

    public static BufferedImage TDfilter (BufferedImage img, int filtersize){

        int w = img.getWidth();
        int h = img.getHeight();
        WritableRaster cr=img.getRaster();
        WritableRaster wr=img.copyData(null);

        double[][] imgarray = Img2D(img);
        double[][] x = new double[filtersize][filtersize];
        Matrix filter = Matrix.filter(filtersize, filtersize);
        filter.show();
        Matrix imgm = new Matrix(w,h);;
        Matrix result;

        for (int ii = 0; ii < w; ii++)
                for (int jj = 0; jj < h; jj++) {
                    for (int i = ii; i < filtersize + ii; i++) {
                        for (int j = jj; j < filtersize + jj; j++) {
                                if (i - filtersize / 2 < 0 || i - filtersize / 2  >= w || j- filtersize / 2  < 0 || j- filtersize / 2  >= h) {
                                    x[i-ii][j-jj] = 0;
                                   // imgm = new Matrix(x);
                                } else {
                                    x[i-ii][j-jj] = imgarray[i - filtersize / 2][j - filtersize / 2];
                                };


                        }

                    }
                    imgm = new Matrix(x);
                    result = imgm.multiply(filter);
                    double value = result.average();
                    wr.setSample(ii, jj, 0, value);
                }



        BufferedImage img2= new BufferedImage(w, h, img.getType());
        img2.setData(wr);
        return img2;    

    }

    public static double [][] Img2D(BufferedImage img) {


        int w = img.getWidth();
        int h = img.getHeight();
        double[][] imgarray = new double[w][h] ;
        Raster raster = img.getData();
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                imgarray[i][j] = raster.getSample(i, j, 0);
            }
        }
        return imgarray;
    }
     public static BufferedImage getImage(String imageName) {
        try {
            File input = new File(imageName);
            BufferedImage image = ImageIO.read(input);
            return image;
        } catch (IOException ie) {
            System.out.println("Error:" + ie.getMessage());
        }
        return null;
    }
}

这假设会使图像更加模糊,但它会使其部分模糊,而其他部分则以随机模式呈现负片。你能帮忙吗:(

4

2 回答 2

1

我没有费心去挖掘你的代码。更好地发布算法的伪表示。

为了解决图像负面部分的问题,当出现数字溢出时会发生这种情况;例如,您试图256根据使用的数据结构和语言来制作具有值的像素,可能会发生字节翻转256而像素得到值 0。

对于 8 位灰度,您需要检查分配给图像的所有值是否在 <0-255> 范围内。

于 2013-10-25T08:08:01.020 回答
1

我发现出了什么问题。我将过滤器设为矩阵,然后将过滤器矩阵乘以图像过滤器,然后得到结果矩阵的平均值。纠正这个问题的方法是将过滤器传递给整个图像,并将每个像素乘以过滤器内的数字,然后将它们全部相加。

于 2013-10-25T15:38:33.597 回答