- 浏览: 3629 次
-
文章分类
最新评论
几个java的图片方法
之前学校有个图片处理研究性学习,正好学了java高级,自己找了些资料写了几个图像处理的方法,在这里分享给大家
1、获取指定路径的图片
/** * 读取指定路径的图片 * * @param path * 图片路径 * @return 缓冲图片 */ public static BufferedImage readPicture(String path) { try { // 使用Io流读取指定路径的图片将其存入图片流 抛出 FuleNotFoundException 以及IoException BufferedImage image = ImageIO.read(new FileInputStream(path)); bImage = image; System.out.println("图片读取成功"); } catch (FileNotFoundException e) { System.out.println("没有找到指定的图片"); } catch (IOException e) { System.out.println(e.getMessage()); } return bImage; }
2、读取网络图片
/** * 读取网络图片 * * @param url * 地址 * @return 缓冲图片 */ public static BufferedImage readWebImage(String url) { BufferedImage bf = null; HttpURLConnection conn; InputStream inStream = null; // 以流的方式获取网络数据 try { URL u = new URL(url); conn = (HttpURLConnection) u.openConnection(); // 设置请求方式为"GET" conn.setRequestMethod("GET"); // 超时响应时间为5秒 conn.setConnectTimeout(5 * 1000); // 通过输入流获取图片数据 inStream = conn.getInputStream(); bf = ImageIO.read(inStream); } catch (Exception e) { e.printStackTrace(); return null; } finally { // 关闭输入流释放连接 if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } conn = null; } return bf; }
3、使用BufferedImage创建一张图片
/** * 使用图片流创建一张图片 * * @param bf * 图片流 * @param path * 路径 * @param name * 文件名 * @param format * 格式 */ public static void creatPicture(BufferedImage bf, String path, String name, String format) { try { // 新建文件 File file = new File(path + "/" + name + "." + format); // 通过图片io流将图片写入file文件 ImageIO.write(bf, format, file); System.out.println("图片创建成功,保存在" + path + "/" + name + "目录下"); } catch (Exception e) { e.printStackTrace(); System.out.println("创建失败"); } }
4、获取色差,即颜色在直方图中的距离
/** * 获取两个颜色的距离 * * @param r1 * 颜色1 * @param r2 * 颜色2 * @return 颜色距 */ public static float getDistance(RGB r1, RGB r2) { // 结果 float result = 0; // 使用空间两点间距离公式求解颜色距离 result = (float) Math.sqrt(Math.pow(r1.getRed() - r2.getRed(), 2) + Math.pow(r1.getGreen() - r2.getGreen(), 2) + Math.pow(r1.getBlue() - r2.getBlue(), 2)); return result; }
5、将一张彩色图片转换为灰度图
/** * 全灰度图转换 * * @param path * 图片路径 * @param type * 0:hsv取亮度 1:RGB取平均值 2:移位算法 3。取红色 4。 取绿色 5.取蓝色 其他:RGB心理算法 * @param threshold * 误差 */ public static BufferedImage changeToGray(BufferedImage pixes, int type, int threshold) { BufferedImage bi = pixes; if (pixes.getHeight() < 1) { return bi; } else { Graphics2D g2 = (Graphics2D) bi.getGraphics(); // i是纵坐标 switch (type) { case 0: for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 RGB color = new RGB(pixes.getRGB(j, i)); HSV hsv = color.toHSV(); hsv.setSaturation(0); hsv.setHue(0); g2.setColor(new Color(color.toRGBInt())); if (getDistance(color, hsv.toRGB()) <= threshold) { g2.setColor(new Color(hsv.toRGB().toRGBInt())); } // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } break; case 1: for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 RGB color = new RGB(pixes.getRGB(j, i)); int ave = (color.getBlue() + color.getRed() + color .getGreen()) / 3; g2.setColor(new Color(color.toRGBInt())); if (getDistance(color, new RGB(ave, ave, ave)) <= threshold) { g2.setColor(new Color(ave, ave, ave)); } // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } break; case 2: for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 RGB color = new RGB(pixes.getRGB(j, i)); int ave = (color.getBlue() * 28 + color.getRed() * 76 + color .getGreen() * 151) >> 8; g2.setColor(new Color(color.toRGBInt())); if (getDistance(color, new RGB(ave, ave, ave)) <= threshold) { g2.setColor(new Color(ave, ave, ave)); } // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } break; case 3: for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 RGB color = new RGB(pixes.getRGB(j, i)); g2.setColor(new Color(color.toRGBInt())); if (getDistance( color, new RGB(color.getRed(), color.getRed(), color .getRed())) <= threshold) { g2.setColor(new Color(color.getRed(), color .getRed(), color.getRed())); } // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } break; case 4: for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 RGB color = new RGB(pixes.getRGB(j, i)); g2.setColor(new Color(color.toRGBInt())); if (getDistance(color, new RGB(color.getGreen(), color.getGreen(), color.getGreen())) <= threshold) { g2.setColor(new Color(color.getGreen(), color .getGreen(), color.getGreen())); } // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } break; case 5: for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 RGB color = new RGB(pixes.getRGB(j, i)); g2.setColor(new Color(color.toRGBInt())); if (getDistance( color, new RGB(color.getBlue(), color.getBlue(), color .getBlue())) <= threshold) { g2.setColor(new Color(color.getBlue(), color .getBlue(), color.getBlue())); } // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } break; default: for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 RGB color = new RGB(pixes.getRGB(j, i)); int gray = (color.getBlue() * 114 + color.getRed() * 299 + color.getGreen() * 587) / 1000; g2.setColor(new Color(color.toRGBInt())); if (getDistance(color, new RGB(gray, gray, gray)) <= threshold) { g2.setColor(new Color(gray, gray, gray)); } // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } break; } return bi; } }其中出现的RGB类是色彩的RGB格式,HSV是色彩的HSV格式
RGB.java
public class RGB { // 红色 private int red; // 绿色 private int green; // 蓝色 private int blue; /** * 默认生成白色 */ public RGB() { red = 255; green = 255; blue = 255; } /** * 用RGB值初始化一个颜色 * * @param red * 红色值 0-255 * @param green * 绿色值 0-255 * @param blue * 蓝色值 0-255 */ public RGB(int red, int green, int blue) { if (red < 256 && green >= 0 && green < 256 && green >= 0 && blue < 256 && blue >= 0) { this.red = red; this.green = green; this.blue = blue; } else { this.red = 0; this.green = 0; this.blue = 0; } } /** * 使用32位整型值初始化一个颜色 * * @param color * 例如 0xffffff -1 */ public RGB(int color) { // Color类 可以使用整形值初始化一个类,由于 Color c = new Color(color); red = c.getRed(); green = c.getGreen(); blue = c.getBlue(); } public int getRed() { return red; } public void setRed(int red) { this.red = red; } public int getGreen() { return green; } public void setGreen(int green) { this.green = green; } public int getBlue() { return blue; } public void setBlue(int blue) { this.blue = blue; } /** * 转换为32位整型值 用于初始化一个Color对象 * * @return 32位整型 */ public int toRGBInt() { int rgb = 0xff000000 | (red << 16) | green << 8 | blue; return rgb; } public float getDistance(RGB rgb) { return (float) Math.sqrt(Math.pow(this.getRed() - rgb.getRed(), 2) + Math.pow(this.getGreen() - rgb.getGreen(), 2) + Math.pow(this.getBlue() - rgb.getBlue(), 2)); } /** * RGB转HSV算法 * * @return */ public HSV toHSV() { int hue = 0;// 色调 int saturation = 0;// 饱和度 int value = 0;// 亮度 int max = Math.max(Math.max(red, blue), green); int min = Math.min(Math.min(red, blue), green); // 色调的计算 if (max == min) { hue = 0; } else { if (max == red && green >= blue) { hue = (int) ((green - blue) / (float) (max - min) * 60); } else if (max == red && green < blue) { hue = (int) ((green - blue) / (float) (max - min) * 60 + 360); } else if (max == green) { hue = (int) ((blue - red) / (float) (max - min) * 60 + 120); } else if (max == blue) { hue = (int) ((red - green) / (float) (max - min) * 60 + 240); } } value = max;// 亮度的值为颜色最大值 if (max == 0) { saturation = 0; } else { saturation = (int) ((float) (max - min) / max * 100);// 饱和度计算 } return new HSV(hue, saturation, value); } public String toString() { return "[" + red + "," + green + "," + blue + "]"; }
HSV.java
public class HSV { int hue;// 色调0-360 int saturation;// 饱和度0-100 int value;// 亮度0-255 public HSV() { this.hue = 0; this.saturation = 0; this.value = 0; } /** * 使用参数初始化HSV信息 * @param hue 色调 * @param saturation 饱和度 * @param value 亮度 */ public HSV(int hue, int saturation, int value) { if (hue <= 360 && saturation >= 0 && saturation <= 100 && value >= 0 && value <= 255) { this.hue = hue; this.saturation = saturation; this.value = value; } else { this.hue = 0; this.saturation = 0; this.value = 0; } } public int getHue() { return hue; } public void setHue(int hue) { this.hue = hue; } public int getSaturation() { return saturation; } public void setSaturation(int saturation) { this.saturation = saturation; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public String toString(){ return "["+hue+","+saturation+","+value+"]"; } /** * HSV转RGB算法 * @return */ public RGB toRGB() { int red = 0; int green = 0; int blue = 0; float a, b, c, h;// if (saturation == 0) { red = value; green = value; blue = value; } else { h = hue / (float) 60; int i = hue / 60; a = (float) (value * (100 - saturation)/100.0); b = (float) (value * (100 - saturation * (h-i))/100.0); c = (float) (value * (100 - saturation* (1 - h+i))/100.0); switch (i) { case 0: red=value; green=Math.round(c); blue=Math.round(a); break; case 1: red=Math.round(b); green=value; blue=Math.round(a); break; case 2: red=Math.round(a); green=value; blue=Math.round(c); break; case 3: red=Math.round(a); green=Math.round(b); blue=value; break; case 4: red=Math.round(c); green=Math.round(a); blue=value; break; case 5: red=value; green=Math.round(a); blue=Math.round(b); break; } } return new RGB(red, green, blue); } }
6、在一张图片上的指定位置用指定颜色画一个矩形
/** * 在一张图片上的指定位置用指定颜色画一个矩形 * * @param pix * 图片流 * @param x * 起点 横坐标 * @param y * 起点 纵坐标 * @param width * 矩形宽度 * @param height * 矩形高度 * @param rgb * 画笔颜色 * @return 画完方后的矩形 */ public static BufferedImage drawRectOnMip(BufferedImage pix, int x, int y, int width, int height, RGB rgb) { BufferedImage pixes = pix; if (x > pixes.getWidth() || y > pixes.getHeight() || width > pixes.getWidth() - x || height > pixes.getHeight() - y) { System.out.println("无法绘制"); return pix; } // pic_drawRect Graphics2D g2 = (Graphics2D) pixes.getGraphics(); for (int i = 0; i < pixes.getHeight(); i++) { // j是横坐标 for (int j = 0; j < pixes.getWidth(); j++) { // 获取颜色集合中一个颜色的int值 Color color = new Color(pixes.getRGB(j, i)); // System.out.println(pixes.get(i).get(j).toRGBInt()); // System.out.println(color); g2.setColor(color); // System.out.println(g2.getColor()); // 画一个像素 g2.drawLine(j, i, j, i); } } g2.setColor(new Color(rgb.toRGBInt())); g2.drawRect(x, y, width, height); return pixes; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关推荐
实训商业源码-付费进群自动定位版本-毕业设计.zip
单级热电制冷器件,全球前20强生产商排名及市场份额(by QYResearch).pdf
实训商业源码-Turbo Website Reviewer SEO分析报告工具源码-毕业设计.zip
内容概要:本文详细介绍了利用COMSOL进行铌酸锂波导倍频(PPLN)仿真的方法和技术难点。首先讨论了材料设置中非线性系数d33的空间调制方式,推荐使用tanh函数代替sign函数以提高收敛性。接着阐述了波导结构的选择和模式分析的关键步骤,强调了正确设置边界条件的重要性。对于网格划分提出了在极化周期交界处局部加密的方法,并解释了分步求解策略以节省内存。最后,作者提醒注意相位匹配条件以及考虑实际器件制造中的工艺误差对转换效率的影响。 适合人群:从事非线性光学研究、光子学器件设计的研究人员和工程师。 使用场景及目标:帮助读者掌握COMSOL软件中针对PPLN结构的仿真技巧,优化仿真流程,提升仿真准确性,解决实际项目中可能遇到的问题。 阅读建议:由于文中涉及大量具体的操作细节和技术要点,建议读者结合自己的项目背景仔细研读每个部分的内容,并尝试将所学应用到实践中去。
PLOT2222222222
内容概要:本文介绍了“19永磁直驱风机+混合储能+PQ逆变并网”系统,这是一种集成永磁直驱风机、混合储能设备和PQ逆变器的综合性解决方案,旨在实现可再生能源的高效利用和电网的稳定并网。文中详细阐述了各组件的工作原理及其协同效应,强调了该系统在提高能量转换效率、增强电网稳定性和改善供电质量方面的优势。通过对实际应用效果的分析,展示了该系统在低风速环境下的稳定输出能力、混合储能系统的削峰填谷作用以及PQ逆变器的智能调控和保护功能。 适合人群:从事新能源研究和技术开发的专业人士,关注绿色能源发展的科研工作者和政策制定者。 使用场景及目标:适用于风电场建设、分布式能源系统规划等领域,旨在推动可再生能源的广泛应用,促进电网的智能化和稳定性。 其他说明:随着可再生能源的发展,该系统有望在全球范围内获得更广泛的应用,成为未来能源领域的重要组成部分。
内容概要:本文详细介绍了商用车P2并联混合动力系统的HCU(整车控制器)控制策略及其建模方法。首先探讨了模式切换策略,针对不同工况如车辆速度、电池电量等因素进行模式选择。接着深入讲解了扭矩分配策略,考虑到了温度变化以及坡道情况对扭矩分配的影响。此外,还讨论了能量回收策略,利用预测性制动提高能量利用率。最后提及了故障降级策略,确保系统在出现故障时能够快速响应。文中提供了多个具体代码片段来辅助理解和实施这些策略。 适合人群:从事汽车电子控制系统开发的技术人员,尤其是专注于混合动力系统的研究人员和工程师。 使用场景及目标:帮助开发者将理论性的功能规范转化为实际可用的控制模型,适用于商用车P2并联混合动力系统的开发过程中,旨在提升系统的效率和平顺性。 其他说明:建议读者在实践中不断调整和完善模型参数,以适应不同的应用场景和技术要求。同时,在构建模型时应注意保持良好的可追溯性和验证性,以便后续维护和改进。
OpenAI Agents SDK
干式无油螺杆空压机,2024年前13大企业占据全球78%的市场份额.pdf
实训商业源码-多功能水果外卖电子商务手机模板-毕业设计.zip
.NET Framework 3.5(Windows server系统)
电子设计竞赛相关资源
电子设计竞赛相关资源
膜用聚砜,全球前9强生产商排名及市场份额(by QYResearch).pdf
信號完整性小技巧 #2 EYE CONTOUR.pdf
常规S参数与共模差模S参数转换
实训商业源码-UNIAPP开发的软件市场多端源码-毕业设计.zip
内容概要:本文详细介绍了LMS(LabVIEW Multifunctional System)测试系统在模态分析和锤击实验中的应用。首先解释了LMS系统及其核心功能——模态分析,这是一种用于确定结构振动特性的关键技术,可以获取固有频率、阻尼比和模态形状等参数。接着阐述了锤击实验的具体步骤,包括实验准备、数据采集、激励与响应记录、数据分析和结果解读。文中还简要介绍了LMS系统中使用的软件工具,涵盖数据导入、滤波与去噪、频域分析、模态识别与提取等功能。最后强调了模态分析和锤击实验在结构设计、优化和故障诊断中的重要作用。 适合人群:从事机械工程、振动与噪声分析的技术人员,尤其是需要掌握模态分析和锤击实验的应用工程师。 使用场景及目标:适用于希望深入了解LMS系统在模态分析和锤击实验中的具体应用,提升结构设计和故障诊断能力的专业人士。目标是在实际工作中更好地利用LMS系统进行振动与噪声分析。 其他说明:本文不仅提供了理论知识,还详细描述了实验操作流程,帮助读者更好地理解和应用相关技术。
内容概要:本文详细介绍了使用MATLAB 2021b进行手写数字图像识别的深度学习入门教程。主要内容涵盖从加载MNIST数据集到构建并训练卷积神经网络(CNN)的全过程。首先展示了如何读取和预览数据集,接着逐步讲解了CNN各层的设计思路及其功能,如卷积层、批规范化层、ReLU激活函数以及池化层的作用。随后,演示了如何将数据集划分为训练集和验证集,并设置训练选项来启动模型训练。此外,还提供了提取和展示中间层特征图的方法,帮助理解CNN的工作机制。最后,评估了模型性能并通过混淆矩阵直观地展示了分类效果。 适合人群:初学者和希望快速掌握MATLAB环境下深度学习应用的研究人员或学生。 使用场景及目标:适用于希望通过实际操作加深对深度学习理论和技术的理解,特别是对于想要了解CNN架构及其在图像识别任务中具体应用的人群。 其他说明:文中提供的代码片段可以直接在MATLAB环境中执行,便于读者跟随教程动手实践。同时,强调了模型训练过程中需要注意的问题,如过拟合现象的监控等。
内容概要:本文详细介绍了博世汽车电驱仿真模型的技术细节,涵盖同步电机和异步电机的相电流波形优化、自动弱磁FOC控制、正反转切换电流稳定性和铁损计算等方面。文中展示了MATLAB和C语言编写的自动化控制脚本,以及Python编写的高效铁损计算方法。特别强调了在高转速条件下保持电流波形的稳定性,以及通过动态磁滞模型提高铁损计算精度。 适合人群:从事电动汽车驱动系统研究的专业人士、电机控制系统工程师和技术研究人员。 使用场景及目标:适用于需要深入了解和应用先进电机控制技术和仿真的场合,如电动汽车动力系统的开发和测试。目标是掌握先进的FOC控制算法和优化技术,提高电机性能和效率。 其他说明:文章不仅提供了理论背景,还包括具体的实现代码片段,便于读者理解和实际操作。