成都网站建设设计

将想法与焦点和您一起共享

java代码实现图像识别 图片识别代码java

求java识别三角形,圆形,方形的具体算法和原理。

首先图片的背景和图形的颜色肯定是不一样的,图片是由像素组成的(这个概念很重要),,第一步区分背景和图形的颜色,保存背景的颜色,,第二步创建一个二维数组,这个二维数组对应于这个图片,你比如说,我这个图片是10*10大小的,然后我就把我这个数组保存是100*100的,即每隔0.1我取一下图片的像素值,判断这个像素值和背景是否一样,如果一样,那么数组的对应位置就存储0,否则存储1,,,第三步,通过Java代码控制鼠标遍历图片,一行一行的遍历,取像素值,与背景的像素对比,存入数组,遍历之后二维数组就只是存储的0和1(0代表背景,1代表图形),,第四步,把所有为1的二维数组元素对应的坐标取出来,写个方法判断一下,相当于数轴知道X和Y了,你判断一下图形的形状,应该不难。。。而且图形就三个,,不难实现,,楼主可以试试

在章贡等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都网站建设 网站设计制作按需网站策划,公司网站建设,企业网站建设,成都品牌网站建设,营销型网站建设,成都外贸网站建设,章贡网站建设费用合理。

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 图像识别 数字图像处理 从一张JPG图片中识别出若干黑色小方块

你需要关注的主要是这个类:java.awt.image.BufferedImage

可以查阅相关的API。

java图像处理技术在《java核心技术8 下卷》中有比较详细的介绍。

相关技术要求和注意事项:RGB标准、ICC配置特性、

建议如果进行像素识别的话可以选取关键点的识别方式、而且确认像素是否符合要求使用RGB的范围识别而非精确识别。

至于具体的识别操作过程,需要你详细定义开始识别的位置标准(规定的或者识别图像获取)、边界标准、大小(识别块得SIZE)、分组(给识别块确定属性)等

JAVA识别图片验证码

package com.he;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

import java.awt.*;

import java.awt.image.*;

import javax.imageio.*;

public class CodeFact

extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

//设置页面不缓存

response.setHeader("Pragma", "No-cache");

response.setHeader("Cache-Control", "no-cache");

response.setDateHeader("Expires", 0);

// 在内存中创建图象

int width = 60, height = 20;

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

// 获取图形上下文

Graphics g = image.getGraphics();

//生成随机类

Random random = new Random();

// 设定背景色

g.setColor(getRandColor(200, 250));

g.fillRect(0, 0, width, height);

//设定字体

g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

//画边框

g.setColor(new Color(33,66,99));

g.drawRect(0,0,width-1,height-1);

// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到

g.setColor(getRandColor(160, 200));

for (int i = 0; i 155; i++) {

int x = random.nextInt(width);

int y = random.nextInt(height);

int xl = random.nextInt(12);

int yl = random.nextInt(12);

g.drawLine(x, y, x + xl, y + yl);

}

// 取随机产生的认证码(4位数字)

String sRand = "";

for (int i = 0; i 4; i++) {

String rand = String.valueOf(random.nextInt(10));

sRand += rand;

// 将认证码显示到图象中

g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110),

20 + random.nextInt(110))); //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成

g.drawString(rand, 13 * i + 6, 16);

}

// 将认证码存入SESSION

HttpSession session = request.getSession();

session.setAttribute("rand", sRand);

// 图象生效

g.dispose();

// 输出图象到页面

ImageIO.write(image, "JPEG", response.getOutputStream());

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

doGet(request, response);

}

//给定范围获得随机颜色

private Color getRandColor(int fc, int bc) {

Random random = new Random();

if (fc 255) {

fc = 255;

}

if (bc 255) {

bc = 255;

}

int r = fc + random.nextInt(bc - fc);

int g = fc + random.nextInt(bc - fc);

int b = fc + random.nextInt(bc - fc);

return new Color(r, g, b);

}

}

你试试!!

怎么用java实现图片里面的数字识别

图片是由点组成(或者是别的方法),记录点的位置、颜色,控制点就行了。至于ocr,有难度,首先要制作文字的变化范围及整个字各部分的联系,这还是简单的。然后,图像分解就行了。额,我不会编程,稍微会点c++,所以这个回答就是假设如果我做这种程序的思路。

如何使用Java实现屏幕找图功能

测试代码

[java] view plain copy

public static void main(String[] args) throws Exception {

findImage4FullScreen(ImageCognition.SIM_ACCURATE_VERY);

}

public static void findImage4FullScreen(int sim) throws Exception {

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

int w = (int) screenSize.getWidth();

int h = 200;

Robot robot = new Robot();

BufferedImage screenImg = robot.createScreenCapture(new Rectangle(0, 0,

w, h));//对屏幕指定范围进行截图,保存到BufferedImage中

OutputStream out = new FileOutputStream("data/images/screen.png");

ImageIO.write(screenImg, "png", out);//将截到的BufferedImage写到本地

InputStream in = new FileInputStream("data/images/search.png");

BufferedImage searchImg = ImageIO.read(in);//将要查找的本地图读到BufferedImage

//图片识别工具类

ImageCognition ic = new ImageCognition();

ListCoordBean list = ic.imageSearch(screenImg, searchImg, sim);

for (CoordBean coordBean : list) {

System.out.println("找到图片,坐标是" + coordBean.getX() + ","

+ coordBean.getY());

//标注找到的图的位置

Graphics g = screenImg.getGraphics();

g.setColor(Color.BLACK);

g.drawRect(coordBean.getX(), coordBean.getY(),

searchImg.getWidth(), searchImg.getHeight());

g.setFont(new Font(null, Font.BOLD, 20));

g.drawString("←找到的图片在这里",

coordBean.getX() + searchImg.getWidth() + 5,

coordBean.getY() + 10 + searchImg.getHeight() / 2);

out = new FileOutputStream("data/images/result.png");

ImageIO.write(screenImg, "png", out);

}

}

额外的类

CoordBean

package cn.xt.imgCongnition;

public class CoordBean {

private int x;

private int y;

/**

* 获取x坐标

*

* @return x坐标

*/

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

/**

* 获取y坐标

*

* @return

*/

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

}

RgbImageComparerBean

package cn.xt.imgCongnition;

/**

* RGB 相关,图片相似度计算时使用

*

*/

public class RgbImageComparerBean {

/****** 颜色值数组,第一纬度为x坐标,第二纬度为y坐标 ******/

private int colorArray[][];

/*** 是否忽略此点,若为true,则不纳入比较的像素行列。 ***/

private boolean ignorePx[][];

/**** 图片的宽高 ****/

private int imgWidth;

private int imgHeight;

// 图片的像素总数

private int pxCount;

/**

* 获取图像的二维数组组成

*

* @return 图像的二维数组

*/

public int[][] getColorArray() {

return colorArray;

}

/**

* 要对比的像素总数,会自动筛选掉不对比的颜色

*

* @param pxCount

*/

public void setPxCount(int pxCount) {

this.pxCount = pxCount;

}

/**

* 设置颜色二维数组

*

* @param colorArray

* 颜色二维数组,一维为x轴,二维为y轴

*/

public void setColorArray(int[][] colorArray) {

this.colorArray = colorArray;

this.imgWidth = this.colorArray.length;

this.imgHeight = this.colorArray[0].length;

this.pxCount = this.imgWidth * this.imgHeight;

}

/**

* 是否忽略此点,若为true,则不纳入像素比较行列。

*

* @return 具体x,y坐标的那个像素点

*/

public boolean[][] getIgnorePx() {

return ignorePx;

}

/**

* 是否忽略此点,若为true,则不纳入像素比较行列。

*

* @param ignorePx

* 具体x,y坐标的那个像素点

*/

public void setIgnorePx(boolean[][] ignorePx) {

this.ignorePx = ignorePx;

}

/**

* 获取图像的宽度

*

* @return 图像宽度

*/

public int getImgWidth() {

return imgWidth;

}

/**

* 获取图像的高度

*

* @return 图像高度

*/

public int getImgHeight() {

return imgHeight;

}

/**

* 获取图像里像素的总数

*

* @return

*/

public int getPxCount() {

return pxCount;

}

}


文章名称:java代码实现图像识别 图片识别代码java
分享网址:http://chengdu.cdxwcx.cn/article/hgsies.html