Java卷积

Java卷积,java,convolution,Java,Convolution,嗨,我需要一些帮助。我需要从头开始编写一个卷积方法,它接受以下输入:int[][]和BufferedImage-inputImage。我可以假设内核的大小为3x3 我的方法如下: convolve inner pixels convolve corner pixels convolve outer pixels 在我将在下面发布的程序中,我相信我卷积了内部像素,但我对如何卷积角点和外部像素有点迷茫。我知道角点像素位于(0,0)、(宽度-1,0)、(0,高度-1)和(宽度-1,高度-1)。我想我

嗨,我需要一些帮助。我需要从头开始编写一个卷积方法,它接受以下输入:
int[][]
BufferedImage-inputImage
。我可以假设内核的大小为3x3

我的方法如下:

convolve inner pixels
convolve corner pixels
convolve outer pixels
在我将在下面发布的程序中,我相信我卷积了内部像素,但我对如何卷积角点和外部像素有点迷茫。我知道角点像素位于
(0,0)
(宽度-1,0)
(0,高度-1)
(宽度-1,高度-1)
。我想我知道如何处理这个问题,但不知道如何以书面形式执行。请注意,我对编程非常陌生:/任何帮助都将对我非常有帮助

import java.awt.*;
import java.awt.image.BufferedImage;
import com.programwithjava.basic.DrawingKit;
import java.util.Scanner;

public class Problem28 {
     // maximum value of a sample
    private static final int MAX_VALUE = 255;
    //minimum value of a sample
    private static final int MIN_VALUE = 0; 

    public BufferedImage convolve(int[][] kernel, BufferedImage inputImage) {


    }

    public BufferedImage convolveInner(double center, BufferedImage inputImage) {
        int width = inputImage.getWidth();
        int height = inputImage.getHeight();

        BufferedImage inputImage1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        //inner pixels
        for (int x = 1; x < width - 1; x++) {
            for (int y = 1; y < height - 1; y ++) {
                //get pixels at x, y
                int colorValue = inputImage.getRGB(x, y);
                Color pixelColor = new Color(colorValue);
                int red =  pixelColor.getRed() ;
                int green =  pixelColor.getGreen() ;
                int blue =  pixelColor.getBlue();

              int innerred =  (int) center*red;
              int innergreen =  (int) center*green;
              int innerblue =  (int) center*blue;

              Color newPixelColor = new Color(innerred, innergreen, innerblue);
              int newRgbvalue = newPixelColor.getRGB();
              inputImage1.setRGB(x, y, newRgbvalue);    
            }
        }
        return inputImage1;
    }

    public BufferedImage convolveEdge(double edge, BufferedImage inputImage) {
        int width = inputImage.getWidth();
        int height = inputImage.getHeight();

        BufferedImage inputImage2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        //inner pixels
        for (int x = 0; x < width - 1; x++) {
            for (int y = 0; y < height - 1; y ++) {
                //get pixels at x, y
                int colorValue = inputImage.getRGB(x, y);
                Color pixelColor = new Color(colorValue);
                int red =  pixelColor.getRed() ;
                int green =  pixelColor.getGreen() ;
                int blue =  pixelColor.getBlue();

              int innerred =  (int) edge*red;
              int innergreen =  (int) edge*green;
              int innerblue =  (int) edge*blue;

              Color newPixelColor = new Color(innerred, innergreen, innerblue);
              int newRgbvalue = newPixelColor.getRGB();
              inputImage2.setRGB(x, y, newRgbvalue);    
            }
        }
        return inputImage2;
    }

    public BufferedImage convolveCorner(double corner, BufferedImage inputImage) {
        int width = inputImage.getWidth();
        int height = inputImage.getHeight();

        BufferedImage inputImage3 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        //inner pixels
        for (int x = 0; x < width - 1; x++) {
            for (int y = 0; y < height - 1; y ++) {
                //get pixels at x, y
                int colorValue = inputImage.getRGB(x, y);
                Color pixelColor = new Color(colorValue);
                int red =  pixelColor.getRed() ;
                int green =  pixelColor.getGreen() ;
                int blue =  pixelColor.getBlue();

              int innerred =  (int) corner*red;
              int innergreen =  (int) corner*green;
              int innerblue =  (int) corner*blue;

              Color newPixelColor = new Color(innerred, innergreen, innerblue);
              int newRgbvalue = newPixelColor.getRGB();
              inputImage3.setRGB(x, y, newRgbvalue);    
            }
        }
        return inputImage3;
    }


  public static void main(String[] args) {
    DrawingKit dk = new DrawingKit("Compositor", 1000, 1000);
    BufferedImage p1 = dk.loadPicture("image/pattern1.jpg");
    Problem28 c = new Problem28();
    BufferedImage p5 = c.convolve();
    dk.drawPicture(p5, 0, 100);
  }
}
import java.awt.*;
导入java.awt.image.buffereImage;
导入com.programwithjava.basic.DrawingKit;
导入java.util.Scanner;
公共类问题28{
//样本的最大值
私有静态最终整数最大值=255;
//样本的最小值
私有静态最终整数最小值=0;
公共BuffereImage卷积(int[][]内核,BuffereImage inputImage){
}
公共BuffereImage convolveInner(双中心,BuffereImage输入图像){
int width=inputImage.getWidth();
int height=inputImage.getHeight();
BuffereImage inputImage1=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_ARGB);
//内像素
对于(int x=1;x
我对代码做了一些修改,但输出结果显示为黑色。我做错了什么:

import java.awt.*;
import java.awt.image.BufferedImage;
import com.programwithjava.basic.DrawingKit;
import java.util.Scanner;

public class Problem28 {
    // maximum value of a sample
    private static final int MAX_VALUE = 255;
    //minimum value of a sample
    private static final int MIN_VALUE = 0; 


    public BufferedImage convolve(int[][] kernel, BufferedImage inputImage) {


        int width = inputImage.getWidth();
        int height = inputImage.getHeight();

        BufferedImage inputImage1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        //for every pixel
        for (int x = 0; x < width; x ++) {
            for (int y = 0; y < height; y ++) {

                int colorValue = inputImage.getRGB(x,y);
                Color pixelColor = new Color(colorValue);
                int red = pixelColor.getRed();
                int green = pixelColor.getGreen();
                int blue = pixelColor.getBlue();

                double gray = 0;

                //multiply every value of kernel with corresponding image pixel
                for (int i = 0; i < 3; i ++) {
                    for (int j = 0; j < 3; j ++) {

                        int imageX = (x - 3/2 + i + width) % width;
                        int imageY = (x -3/2 + j + height) % height;


                        int RGB = inputImage.getRGB(imageX, imageY);
                        int GRAY = (RGB) & 0xff;

                        gray += (GRAY*kernel[i][j]);

                    }
                }
                int out;
                out = (int) Math.min(Math.max(gray * 1, 0), 255);
                inputImage1.setRGB(x, y, new Color(out,out,out).getRGB());
            }
        }
        return inputImage1;
    }


public static void main(String[] args) {
    int[][] newArray = {{1/9, 1/9, 1/9}, {1/9, 1/9, 1/9}, {1/9, 1/9, 1/9}};
    DrawingKit dk = new DrawingKit("Problem28", 1000, 1000);
    BufferedImage p1 = dk.loadPicture("image/pattern1.jpg");
    Problem28 c = new Problem28();
    BufferedImage p2 = c.convolve(newArray, p1);
    dk.drawPicture(p2, 0, 100);
  }
}
import java.awt.*;
导入java.awt.image.buffereImage;
导入com.programwithjava.basic.DrawingKit;
导入java.util.Scanner;
公共类问题28{
//样本的最大值
私有静态最终整数最大值=255;
//样本的最小值
私有静态最终整数最小值=0;
公共BuffereImage卷积(int[][]内核,BuffereImage inputImage){
int width=inputImage.getWidth();
int height=inputImage.getHeight();
BuffereImage inputImage1=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_ARGB);
//每像素
对于(int x=0;xconvolve(kernel, inputImage)
   for each pixel in the image
      convolve the single pixel and check edge cases
   endfor
end
private void convolvePixel(int x, int y, int[][] kernel, BufferedImage input, BufferedImage output)
convolvePixel(x, y, kernel, input, output)
   accumulation = 0
   for each row of kernel applicable pixels
      for each column of kernel applicable pixels
         if this neighboring pixel location is within the image boundaries then
            input color = get the color at this neighboring pixel
            adjusted value = input color * relative kernel mask value
            accumulation += adjusted value
         else
            //handle this somehow, mentioned below
         endif
      endfor
   endfor
   set output pixel as accumulation, assuming this convolution method does not require normalization
end