当前位置: 首页 > news >正文

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

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

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;}

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

http://www.lryc.cn/news/327772.html

相关文章:

  • CSS及javascript
  • LeetCode 1997.访问完所有房间的第一天:动态规划(DP)——4行主要代码(不需要什么前缀和)
  • BootsJS上新!一个库解决大部分难题!
  • 智慧公厕,让数据和技术更好服务社会生活
  • Spark基于DPU Snappy压缩算法的异构加速方案
  • 如何使用python链表
  • ADB的主要操作命令及详解
  • 傻瓜式启动关闭重启docker容器的脚本
  • R语言使用dietaryindex包计算NHANES数据多种营养指数(2)
  • Elasticsearch 索引模板、生命周期策略、节点角色
  • buy me a btc 使用数字货币进行打赏赞助
  • Solidity Uniswap V2 Router swapTokensForExactTokens
  • 网络安全渗透测试工具
  • springcloud+nacos服务注册与发现
  • 【C++程序员的自我修炼】基础语法篇(一)
  • 小狐狸JSON-RPC:钱包连接,断开连接,监听地址改变
  • union在c语言中什么用途
  • 2024年华为OD机试真题- 寻找最优的路测线路-Java-OD统一考试(C卷)
  • WPF 多路绑定、值转换器ValueConvert、数据校验
  • 【Linux多线程】线程的同步与互斥
  • Linux网卡bond的七种模式详解
  • 【学习笔记】java项目—苍穹外卖day01
  • C++之STL整理(2)之vector超详用法整理
  • 机器学习作业二之KNN算法
  • 笔记81:在服务器中运行 Carla 报错 “Disabling core dumps.”
  • ensp中pc机访问不同网络的服务器
  • CSGO赛事管理系统的设计与实现|Springboot+ Mysql+Java+ B/S结构(可运行源码+数据库+设计文档)
  • win10微软拼音输入法 - bug - 在PATH变量为空的情况下,无法输入中文
  • Java安全篇-Fastjson漏洞
  • Flink系列之:Flink SQL Gateway