java 抠取红色印章(透明背景)

本文详细描述了如何通过Java代码利用Graphics2D和BufferedImage对照片中的红色印章进行抠图,提供了一个示例方法并提到了阈值调整的重要性。
摘要由CSDN通过智能技术生成

一个亲戚让我帮他把照片里的红色印章抠出来,,,记录下处理过程,代码如下,可直接用:

public static void signatureProcess(String sourceImagePath, String targetImagePath) {
		Graphics2D graphics2D = null;
		try {
			File imageFile = new File(sourceImagePath);
			BufferedImage sourceBufferIamge = ImageIO.read(imageFile);
			//新图大小与原图保持一致
			int targetImageWidth = sourceBufferIamge.getWidth();
			int targetImageHeight = sourceBufferIamge.getHeight();

			BufferedImage targetBufferImage = new BufferedImage(targetImageWidth, targetImageHeight, BufferedImage.TYPE_4BYTE_ABGR);
			graphics2D = targetBufferImage.createGraphics();
			graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

			int targetW = targetBufferImage.getWidth();
			int targetH = targetBufferImage.getHeight();
			int targetMinX = targetBufferImage.getMinX();
			int targetMinY = targetBufferImage.getMinY();

			for (int i = targetMinX; i < targetW; i++) {
				for (int j = targetMinY; j < targetH; j++) {
					// 得到原图像素(i,j)上的RGB值
					int pixel = sourceBufferIamge.getRGB(i, j);
					Color color = new Color(pixel);
					float[] hsbvals=new float[3];
					float[] floats = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsbvals);

					int red = (pixel >> 16) & 0xFF;

					// 判断是否为红色印章
					if (red > 200 && colorInRange(floats)) { //双重判断,精细一点 可以根据需要调整阈值
						targetBufferImage.setRGB(i, j, pixel);
					} else {
						// 不是红色,设置为透明
						targetBufferImage.setRGB(i, j, 0x00000000);
					}
				}
			}
			// 保存到新文件
			FileOutputStream ops = new FileOutputStream(new File(targetImagePath));
			ImageIO.write(targetBufferImage, "png", ops);
			ops.flush();
			ops.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (graphics2D != null) {
				graphics2D.dispose();
			}
		}
	}

	private static Boolean colorInRange( float[] floats){
		if (0.90< floats[0]) {
			return true;
		}
		return false;
	}

运行效果(章名打了马赛克),左变为抠出的章,右边为原图,如果抠图效果不好,调整代码判断阈值,最好选清晰的色差大的抠图:

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值