java图像识别算法

java 在屏幕给定区域内找图的实现!

根据问题来看,比历纳较梁烂圆复杂,但是可以提供一个思路,因为每个图都是有名称的

因此使用图像识别可能就不必了,橡塌但是可以通过相应手段在特定区域内查找某

一文件的名称,查找到后可以返回该文件所在坐标,你可以自己试一试,如果

有什么疑问,可以联系我,一起研究研究

java图像识别算法

java适合做图像处理吗?

Java图像处理技巧四则

下面代码中用到的sourceImage是一个已经存在的Image对象

图像剪雀租切

对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:

//import java.awt.*;

//import java.awt.image.*;

Image croppedImage;

ImageFilter cropFilter;

CropFilter =new CropImageFilter(25,30,75,75); //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API

CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));

如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProducer对象。

图像缩放

对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一个100X100的图像

Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。

//其它情况请参考API

灰度变换

下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个洞岁孝颜色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知纳稿道具体算法是什么样的,效果和上述不同。

/* GrayFilter.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class GrayFilter extends RGBImageFilter {

int modelStyle;

public GrayFilter() {

modelStyle=GrayModel.CS_MAX;

canFilterIndexColorModel=true;

}

public GrayFilter(int style) {

modelStyle=style;

canFilterIndexColorModel=true;

}

public void setColorModel(ColorModel cm) {

if (modelStyle==GrayModel

else if (modelStyle==GrayModel

}

public int filterRGB(int x,int y,int pixel) {

return pixel;

}

}

/* GrayModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class GrayModel extends ColorModel {

public static final int CS_MAX=0;

public static final int CS_FLOAT=1;

ColorModel sourceModel;

int modelStyle;

public GrayModel(ColorModel sourceModel) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=0;

}

public GrayModel(ColorModel sourceModel,int style) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=style;

}

public void setGrayStyle(int style) {

modelStyle=style;

}

protected int getGrayLevel(int pixel) {

if (modelStyle==CS_MAX) {

return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));

}

else if (modelStyle==CS_FLOAT){

return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);

}

else {

return 0;

}

}

public int getAlpha(int pixel) {

return sourceModel.getAlpha(pixel);

}

public int getRed(int pixel) {

return getGrayLevel(pixel);

}

public int getGreen(int pixel) {

return getGrayLevel(pixel);

}

public int getBlue(int pixel) {

return getGrayLevel(pixel);

}

public int getRGB(int pixel) {

int gray=getGrayLevel(pixel);

return (getAlpha(pixel)24)+(gray16)+(gray8)+gray;

}

}

如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。

色彩变换

根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class ReverseColorModel extends ColorModel {

ColorModel sourceModel;

public ReverseColorModel(ColorModel sourceModel) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

}

public int getAlpha(int pixel) {

return sourceModel.getAlpha(pixel);

}

public int getRed(int pixel) {

return ~sourceModel.getRed(pixel);

}

public int getGreen(int pixel) {

return ~sourceModel.getGreen(pixel);

}

public int getBlue(int pixel) {

return ~sourceModel.getBlue(pixel);

}

public int getRGB(int pixel) {

return (getAlpha(pixel)24)+(getRed(pixel)16)+(getGreen(pixel)8)+getBlue(pixel);

}

}

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class ReverseFilter extends RGBImageFilter {

public ReverseFilter() {

canFilterIndexColorModel=true;

}

public void setColorModel(ColorModel cm) {

substituteColorModel(cm,new ReverseColorModel(cm));

}

public int filterRGB(int x,int y,int pixel) {

return pixel;

}

}

要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。

下面是上面的效果的一个总的演示程序。

/*GrayImage.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.*;

import java.awt.image.*;

import javax.swing.*;

import java.awt.color.*;

public class GrayImage extends JFrame{

Image source,gray,gray3,clip,bigimg;

BufferedImage bimg,gray2;

GrayFilter filter,filter2;

ImageIcon ii;

ImageFilter cropFilter;

int iw,ih;

public GrayImage() {

ii=new ImageIcon(\”images/11.gif\”);

source=ii.getImage();

iw=source.getWidth(this);

ih=source.getHeight(this);

filter=new GrayFilter();

filter2=new GrayFilter(GrayModel.CS_FLOAT);

gray=createImage(new FilteredImageSource(source.getSource(),filter));

gray3=createImage(new FilteredImageSource(source.getSource(),filter2));

cropFilter=new CropImageFilter(5,5,iw-5,ih-5);

clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));

bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);

MediaTracker mt=new MediaTracker(this);

mt.addImage(gray,0);

try {

mt.waitForAll();

} catch (Exception e) {

}

java blob

java blob是什么,让我们一起了解一下?

Blob是计算机视觉图像中的一块连通区域,Blob分析的就是对前景或背景分离后的二值培磨图像,进行连通域提取和标记以及计算Blob的一些相关特征,而且通过Blob提取,还可以获得相关区域的信息。

Blob分析的重要一个步骤是连通区域的确定,那么它的优缺点是什么?

优点:

Blob在目消中谈标跟踪的优势有:

1、通拿碰过Blob提取,可以获得相关区域的信息,这些信息可以作为边缘监测器或者角点检测器的补充信息。在目标识别中,Blob可以提供局部的统计信息和外貌信息,这些信息能够为目标识别和跟踪提供依据。

2、可以利用Blob对直方图进行峰值检测。

3、Blob还可以作为纹理分析和纹理识别的基元。

4、通过Blob分析,可以得到目标的个数及其所在区域,在进行目标匹配时,不需要对全局图像进行搜索。

缺点:

1、速度过慢,要整个区域作逐点扫描。

2、Blob分析难度大。这是一纯几何学上的问题,一个不规则的形状,如何计算它的面积、大小没有简单易行的算法,太过复杂,运算时间就长,速度就更慢了。

3、实际应用。Blob算法在实际应用中,非常依赖光源。几乎可以说,Blob算法如果离开了一个可靠的光源设计,则完全不起作用。

那么java是怎样对Blob读写的?示例如下:

package com.you.sister;   import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties;   public class BlobTest {   public static Connection conn; public static Connection getConn() throws Exception { FileInputStream fis = new FileInputStream(new File(“jdbc.properties”)); Properties prop = new Properties(); prop.load(fis); String driver = prop.getProperty(“jdbc.driver”); String url = prop.getProperty(“jdbc.url”); String username = prop.getProperty(“jdbc.username”); String password = prop.getProperty(“jdbc.password”); Class.forName(driver); return  DriverManager.getConnection(url, username, password); } public static void main(String[] args) throws Exception { conn = getConn(); readBlob(); writeBlob(); conn.close(); } /** * 从数据库中读大对象出来 * 保存在本地 */ public static void readBlob() { try { String readSql = “select * from emp where empno = ?”; PreparedStatement ps = conn.prepareStatement(readSql); ps.setInt(1, 7369); ResultSet rs = ps.executeQuery(); while (rs.next()) { Blob image = rs.getBlob(“image”); DataOutputStream dos =  // 在FileOutputStream中指定文件输出路径 new DataOutputStream(new FileOutputStream(7369 + “_image.jpeg”)); InputStream fis = image.getBinaryStream(); int out; byte[] outByte = new byte [100]; // 将blob对象输入流写入本地输出流中 while ((out = fis.read(outByte)) != -1) { dos.write(outByte); } fis.close(); dos.flush(); dos.close(); } rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 将大对象文件保存进数据库中 */ public static void writeBlob() { try { BufferedInputStream fis =  new BufferedInputStream(new FileInputStream(new File(“D:\\Tulips.jpg”))); // 如果是新插入字段,则将大对象对应字段插入为empty_clob(); // 如果是修改,则可以先update 该行数据,将大对象对应字段设置为empty_clob(); String writeSql = “select * from emp where empno = ? for update”; PreparedStatement ps = conn.prepareStatement(writeSql); ps.setInt(1, 7499); conn.setAutoCommit(false); ResultSet rs = ps.executeQuery(); while (rs.next()) { oracle.sql.BLOB image = (oracle.sql.BLOB)rs.getBlob(“image”); BufferedOutputStream bos = new BufferedOutputStream(image.getBinaryOutputStream()); int c; // 将实际文件中的内容以二进制的形式来输出到blob对象对应的输出流中 while ((c = fis.read()) != -1) { bos.write(c); } fis.close(); bos.close(); } conn.commit(); rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); } } }

关于JAVA的图片处理问题

public static boolean write(RenderedImage im, String formatName, File output) throws IOException

使用支持给定格式的任意 ImageWriter 将一个图像写入 File。如蚂盯果已经有一个 File 存在,则丢弃其内容。

参数:im – 要写入的 RenderedImage。

formatName – 包含格式非正式名称的 String。

output – 将在其中写入数据的 File。

返回:如果没有找到合适的 writer,则返回 false。

抛出: IllegalArgumentException – 如果任何参数为 null。

IOException – 如闷仿和果在写入过程中发生错误。

说白了,就是按指定的formatName把图片存到file(或OutputStream)中。formatName是已注册的、可以保存图片的writer的非正式名称,比如“jpeg”,“tiff”。如果想知道到底有哪些writer在你的机器上被注册了,用ImageIO.getWriterFormatNames(),返回类型是String[] 。同样的,还有读取图片的reader,对应的是ImageIO.getReaderFormatNames()。

最后要说的是,这个方法是保存图片,和上传没有关系。你可能是要上传图大森片后再保存吧!

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月21日 22:15:03
下一篇 2024年3月21日 22:22:14

相关推荐

  • 深入java虚拟机pdf,深入java虚拟机 中村成洋 pdf

    在linux环境下,java怎么实现从word格式转换为pdf格式 //设置当前使用的打印机,我的Adobe Distiller打印机名字为 Adobe PDF wordCom.setProperty( ActivePrinter , new Variant( Adobe PDF ));//设置printout的参数,将word文档打印为postscript…

    2024年5月23日
    4300
  • java截取指定长度字符串,java截取指定字符串之后的

    java中如何截取字符串中的指定一部分 第一个参数是开始截取的字符位置。(从0开始)第二个参数是结束字符的位置+1。(从0开始)indexof函数的作用是查找该字符串中的某个字的位置,并且返回。 int end);截取s中从begin开始至end结束时的字符串,并将其赋值给s;split讲解:java.lang.string.split split 方法 将…

    2024年5月23日
    4100
  • java绑定一个端口,java使用端口

    java如何多个service共用一个端口 你如果有多个项目的话,你可以把多个项目放到一个tomcat里面,这样端口相同使用项目名称来进行区分项目。你如果非要使用同一个,你也可以配置不同的域名导向不同的项目。就是访问的域名不同转接到的项目不同。 如果需要同时启动多个程序,要么修改tomcat的配置文件中的监听端口。要么修改jar包程序的监听端口。不能在一台服…

    2024年5月23日
    3400
  • java多线程并发编程基础,Java多线程并发执行返回

    电脑培训分享Java并发编程:核心理论 电脑培训发现本系列会从线程间协调的方式(wait、notify、notifyAll)、Synchronized及Volatile的本质入手,详细解释JDK为我们提供的每种并发工具和底层实现机制。 人们开始意识到了继承的众多缺点,开始努力用聚合代替继承。软件工程解决扩展性的重要原则就是抽象描述,直接使用的工具就是接口。接…

    2024年5月23日
    4600
  • 自学java找工作,自学java找工作需要包装简历吗

    自学java学多久可以找到工作 1、自学Java至少需要一年以上的时间才能达到找工作的水平。报班培训四到六个月的时间就可以找到一份不错的工作。 2、自学Java至少需要一年以上的时间才能达到找工作的水平。 3、如果要想找到一份Java相关的工作,需要至少学习5-6个月时间才能就业。Java开发需要掌握一些基础的编程语言知识,比如掌握面向对象的编程思想、基本的…

    2024年5月23日
    4200
  • java左移右移,java 左移

    java位移问题 1、思路:直接用Integer类的bit运算操作。 2、移位操作:左移:向左移位,符号后面的数字是移了多少位,移的位用0补齐,例如2进制数01111111左移一位后变为11111110,移位是字节操作。 3、Java 位运算 Java 位运算[转]一,Java 位运算表示方法: 在Java语言中,二进制数使用补码表示,最高位为符号位,正数的…

    2024年5月23日
    4100
  • java技术规范,java规范性要求

    现在主流的JAVA技术是什么? java最流行开发技术程序员必看 1 、Git Git一直是世界上最受欢迎的Java工具之一,也是Java开发人员最杰出的工具之一。Git是一个开源工具,是-种出色的分布式版本控制解决方案。 (1).Java基础语法、数组、类与对象、继承与多态、异常、范型、集合、流与文件、反射、枚举、自动装箱和注解。(2).Java面向对象编…

    2024年5月23日
    3900
  • javasocket编程,Java socket编程中,禁用nagle算法的参数

    Java进行并发多连接socket编程 1、Java可利用ServerSocket类对外部客户端提供多个socket接口。基本的做法是先创建一个ServerSocket实例,并绑定一个指定的端口,然后在这个实例上调用accept()方法等待客户端的连接请求。 2、Socket socket=server.accept(0;Thread handleThrea…

    2024年5月23日
    4500
  • java死亡,java死代码是什么意思

    我的世界传送回死亡点指令是什么? 1、下面就让我们一起来了解一下吧:我的世界回到死的地方的指令是输入/back,就可以回到死亡地点了,当然也可以看信标,因为死亡后会有一道光集中在死亡点,只要循着光就可以找到目的地了。 2、在服务器中的指令 首先打开指令台,在指令行输入“/back”就可以回到自己的死亡地点了。在单人游戏中的指令 在单人游戏中,您无法直接返回到…

    2024年5月23日
    4700
  • myeclipse能部署java工程么,myeclipse支持jdk18

    myeclipse如何建java文件 1、点击【File】—【New】–【Class】在如下界面,输入Class的名字,如Test,点击【Finish】。Test.java文件创建成功。 2、点击【File】—【New】–【Class】 在如下界面,输入Class的名字,如Test,点击【Finish】。 Te…

    2024年5月23日
    3900

发表回复

登录后才能评论



关注微信