如何解决卷积产生非常暗的图像
编辑: 我包括了一个 k 值的例子。另外要清楚的是,我从 RGB 图像生成三个单独的数组。我还包括加载图像的代码
public static final int[][] SHARPEN = { { -1,-2,-1 },{ 0,0 },{ 1,2,1 } };
加载图片
BufferedImage inputImage = ImageIO.read(new File("bridge-rgb.png")); // load the image from this current folder
当我使用 3*3 内核在 java 中对图像进行卷积时,生成的图像具有您期望从给定内核中获得的一些属性,但非常暗,黑色是主色。如果我使用标识内核处理图像,则返回标识,所以我想这意味着我选择了正确的设置来创建 bufferedImage,因此问题一定出在我的卷积算法上,但是我确实用测试数组测试了卷积算法它似乎确实产生了准确的输出。我想知道有人可以对我所拥有的东西发表任何评论或指出我正确的方向吗?
for (int j = 0; j < kernelWidth; ++j) {
try {
output+=(input[y-1][x-1+j] * k[0][j]);
counter++;
}catch(Exception e) {
continue;
}
}
for (int j = 0; j < kernelWidth;++j) {
try {
output+=(input[y][x-1+j] * k[1][j]);
counter1++;
}catch(Exception e) {
continue;
}
}
for (int j = 0; j < kernelWidth;++j) {
try {
output+=(input[y+1][x-1+j] * k[2][j]);
counter2++;
}catch(Exception e) {
continue;
}
}
if((output>>bitshiftValue)>255) {
return ((255& 0xff)<<bitshiftValue);
}
else if ((output>>bitshiftValue)<0) {
return 0;
}else {
return output;
} }
我使用以下方法对数组进行了卷积
private static int[][] convertTo2DWithoutUsingGetRGBgreen(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
int[][] result = new int[height][width];
final int pixelLength = 4;
for (int pixel = 0,row = 0,col = 0; pixel + 3 < pixels.length; pixel += pixelLength) {
result[row][col] = ((int) ((pixels[pixel + 2] & 0xff) )<<8);
col++;
if (col == width) {
col = 0;
row++;
}
}
return result;
}
和后卷积我像这样把它们加在一起
int[][] finalConv = new int[convRedArray.length][convRedArray[0].length];
for(int c =0; c<convRedArray.length;c++) {
for(int p =0;p<convRedArray[0].length;p++) {
finalConv[c][p]=(convBlueArray[c][p])+(convGreenArray[c[p])+(convRedArray[c][p]);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。