问题描述
我想模糊图像
int radius = 11;
int size = radius * 2 + 1;
float weight = 1.0f / (size * size);
float[] data = new float[size * size];
for (int i = 0; i < data.length; i++) {
data[i] = weight;
}
Kernel kernel = new Kernel(size, size, data);
ConvolveOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
//tbi is BufferedImage
BufferedImage i = op.filter(tbi, null);
它会模糊图像,但不会模糊图像的所有部分。
It will blur the image but not all portion of the image.
我失踪的地方,它会模糊完整的图像。没有任何路径。
Where I am missing so that it will blur complete image. Without any path .
推荐答案
标准Java ConvolveOp
只有两个选项 EDGE_ZERO_FILL
和 EDGE_NO_OP
。你想要的是JAI等价物的选项(), EDGE_REFLECT
(或 EDGE_WRAP
,如果你想重复模式)。
The standard Java ConvolveOp
only has the two options EDGE_ZERO_FILL
and EDGE_NO_OP
. What you want is the options from the JAI equivalent (ConvolveDescriptor), which is EDGE_REFLECT
(or EDGE_WRAP
, if you want repeating patterns).
如果你不想使用JAI,你可以自己实现,通过将图像复制到更大的图像,拉伸或包裹边缘,应用卷积运算,然后切掉边缘(类似于,但根据该文章,您也可以将边缘保持透明。)
If you don't want to use JAI, you can implement this yourself, by copying your image to a larger image, stretching or wrapping the edges, apply the convolve op, then cut off the edges (similar to the technique described in the "Working on the Edge" section of the article posted by @halex in the comments section, but according to that article, you can also just leave the edges transparent).
为简单起见,您只需使用,它执行上述操作(BSD许可证)。
For simplicity, you can just use my implementation called ConvolveWithEdgeOp
which does the above (BSD license).
代码与您原来的代码类似:
The code will be similar to what you had originally:
// ...kernel setup as before...
Kernel kernel = new Kernel(size, size, data);
BufferedImageOp op = new ConvolveWithEdgeOp(kernel, ConvolveOp.EDGE_REFLECT, null);
BufferedImage blurred = op.filter(original, null);
过滤器应该像任何其他 BufferedImageOp
一样工作,并且应该适用于任何 BufferedImage
。
The filter should work like any other BufferedImageOp
, and should work with any BufferedImage
.
这篇关于Java模糊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!