如何使用 JAVA 模糊图像的一部分

How to blur a portion of an image with JAVA

如何模糊图像的一部分,隐藏信用卡信息等隐私部分。

我尝试使用 ConvolveOp.class 比如 :

float[] matrix = new float[400];
for (int i = 0; i < 400; i++)
    matrix[i] = 1.0f/500.0f;

BufferedImage sourceImage =  (BufferedImage) image; ;
BufferedImage destImage = null ;
BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null );
BufferedImage blurredImage = op.filter(sourceImage, destImage);

它似乎有效,只是图像完全模糊了。

我不知道这是否可以通过更改矩阵值来完成,但这绝对可以通过过滤子图像来实现,因为根据 BufferedImage.getSubimage() 文档:

The returned BufferedImage shares the same data array as the original image.

所以原来的BufferedImage应该用这样的代码来改变:

BufferedImage image = /* ... */;
BufferedImage subImage = image.getSubimage(10, 20, 30, 40); // x, y, width, height
new ConvolveOp(new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null).filter(subImage, subImage);

虽然我没有测试这个,但我可以想象如果 sourcedestination 相同,filter 不会按预期工作,在这种情况下你可以使用子图像的副本,使用 this question:

中的解决方案
BufferedImage image = /* ... */;
BufferedImage dest = image.getSubimage(10, 20, 30, 40);  // x, y, width, height
ColorModel cm = dest.getColorModel();
BufferedImage src = new BufferedImage(cm, dest.copyData(dest.getRaster().createCompatibleWritableRaster()), cm.isAlphaPremultiplied(), null).getSubimage(0, 0, dest.getWidth(), dest.getHeight());
new ConvolveOp(new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null).filter(src, dest);

之后,继续使用 image不是 subImagesrcdest!)

如果您想专注于应用程序而不是图像处理的细节,您可以使用像 Marvin 这样的图像处理框架。因此,您可以用更少的代码做更多的事情。

输入图像:

输出图像:

源代码:

import static marvin.MarvinPluginCollection.*;

public class PortionBlur {
    public PortionBlur(){
        // 1. Load image
        MarvinImage image = MarvinImageIO.loadImage("./res/credit_card.jpg");

        // 2. Create masks for each blurred region
        MarvinImageMask mask1 = new MarvinImageMask(image.getWidth(), image.getHeight(), 38,170,345,24);
        MarvinImageMask mask2 = new MarvinImageMask(image.getWidth(), image.getHeight(), 52,212,65,24);
        MarvinImageMask mask3 = new MarvinImageMask(image.getWidth(), image.getHeight(), 196,212,65,20);
        MarvinImageMask mask4 = new MarvinImageMask(image.getWidth(), image.getHeight(), 38,240,200,20);

        // 3. Process Image with each mask
        GaussianBlur gaussianBlur = new GaussianBlur();
        gaussianBlur.load();
        gaussianBlur.attributes.set("radius",15);

        gaussianBlur.process(image.clone(), image, mask1);
        gaussianBlur.process(image.clone(), image, mask2);
        gaussianBlur.process(image.clone(), image, mask3);
        gaussianBlur.process(image.clone(), image, mask4);

        // 4. Save the final image
        MarvinImageIO.saveImage(image, "./res/credit_card_out.jpg");
    }
    public static void main(String[] args) {
        new PortionBlur();
        System.exit(0);
    }
}

高斯模糊算法源码:

https://github.com/gabrielarchanjo/marvinproject/blob/master/marvinproject/dev/MarvinPlugins/src/org/marvinproject/image/blur/gaussianBlur/GaussianBlur.java