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;
}
}
解决方案
推荐阅读
- python - 解压二进制数据得到需要字符串参数错误
- python - 如何将 python3 manage.py runserver 放入 Bash 脚本中
- html - HTML 视频标签仅显示背景
- selenium - 如何在 Katalon Studio 中获取元素的所有子元素
- kendo-ui - Kendo Grid - 使每行的单元格可动态编辑/不可编辑
- javascript - Cypress TestRunner 通过“cypress.open”在本地工作,但在 Windows Server 2008 上不工作
- java - 如何避免 Java 类的这个帮助方法中的代码重复?
- postgresql - 将 ssl 证书路径传递给 PostgreSQL ODBC 驱动程序
- javascript - Vue 生命钩子中的先前和当前数据
- javascript - 如何从虚拟内存在 node.js 中提供 html+js?