一些背景信息:该项目是一个简单的图像,在项目结束时将成为我计算机屏幕的背景。
我想模糊背景的一部分,以使文本的一部分更可见。我可以模糊图像的一部分,其中文本是其自身,但这是我的最后选择。我不想这样做,因为在将来的项目中,我想随着周围的事物而主动模糊背景(我还没有开始这个未来的项目,所以我无法更好地描述该项目)。
有人知道如何模糊背景的一部分吗?对于此项目,它需要大约为400x200像素,并模糊10%-15%
这里有个函数可以做到这一点。它是静态方法,它引用了PApplet,因此,如果您在PDE中工作,则可以从方法签名中删除此参数。另外,使用此方法,您可以指定区域的中心,而不是左上角(根据需要进行修改)。
public static void blur(applet p, int regionCentreX, int regionCentreY, int regionWidth, int regionHeight,
float blurStrength) {
PGraphics g;
g = p.createGraphics(regionWidth, regionHeight);
g.beginDraw();
g.loadPixels();
int cornerX = PApplet.constrain(regionCentreX - (regionWidth / 2), 0, p.width - regionWidth);
int cornerY = PApplet.constrain(regionCentreY - (regionHeight / 2), 0, p.height - regionHeight);
p.loadPixels();
int graphicsIndex = 0;
for (int y = 0; y < regionHeight; y++) {
for (int x = 0; x < regionWidth; x++) {
g.pixels[graphicsIndex] = p.pixels[(cornerY + y) * p.width + (cornerX + x)];
graphicsIndex++;
}
}
p.updatePixels();
g.updatePixels();
g.filter(PConstants.BLUR, blurStrength);
g.endDraw();
p.image(g, cornerX, cornerY);
}
Michael的回答很好地概括了您所追求的功能。
FWIW这是示例>基本>图像> LoadDisplayImage的修改版,说明了模糊滤镜:
/**
* Load and Display
*
* Images can be loaded and displayed to the screen at their actual size
* or any other size.
*/
PImage img; // Declare variable "a" of type PImage
void setup() {
size(640, 360);
// The image file must be in the data folder of the current sketch
// to load successfully
img = loadImage("moonwalk.jpg"); // Load the image into the program
}
void draw() {
// take an image subsection
PImage blurred = img.get(mouseX, mouseY, 210, 210);
// blur that section
blurred.filter(BLUR, 8);
// Displays the image at its actual size at point (0,0)
image(img, 0, 0);
// render blurred subsection (in the same spot)
image(blurred, mouseX, mouseY);
}
请注意,模糊量较大的大图像会变慢。
如果需要更快的方法,可以查看在处理中以PShader实现的片段着色器:示例> Topcs>着色器> BlurFilter应该是一个很好的起点。