您的位置:首页 > 编程语言 > Java开发

Atitit.遍历图像像素点rgb java attilax总结

2015-05-07 22:50 411 查看
Atitit.遍历图像像素点rgb java attilax总结



1. 遍历像素点 1

2. 提取一行 1

3. Rgb分量提取 2

4. 其他读取像素 3

5. --code 5

6. 参考 6



1. 遍历像素点

ImgxPicPhotoSplitor.java atibrow prj



public static boolean containsWhiteLine(BufferedImage image) {

int heit=image.getHeight();

for(int i=0;i<heit;i++)

{

PixLine pl=getPixLine(image, i);

if(isWhiteLine(pl))

return true;

}

return false;

}



2. 提取一行

这 个过程的下一步是用 Java 2D 绘制图像。首先取得它的 Graphics2D 上下文。可以用方法 createGraphics2D() 或者调用 getGraphics() 做到这一点。在这个上下文上绘制将会自动修改图像的像素数据。在绘制完成后,可以用方法 getRGB(int startX, int startY, int w, int h, int rgbArray, int offset, int scansize) 容易且高效地提取图像的像素值。这个方法可以将图像中矩形区域的像素数据传输到一个整数数组中。getRGB() 方法的参数如下:

startX, startY 是要提取的区域左上角图像的坐标

w, h 是要提取的区域的宽度和高度

rgbArray 是接收像素值的整数数组

offset 是数组中接收第一个像素值的位置的索引。

scansize 是图像中相邻两行中具有相同行索引的像素的索引偏移值。如果这个值与要提取的区域的宽度相同,那么一行的第一个像素就会存储在数组中前一行最后一个像素后 面的索引位置。如果这个值大于提取区域的宽度,那么数组中,在一行最后和下一行开始之间就会有一些未使用的索引。



走势这个getRGB 好像有问题,不会调用,查找资料也不行。自豪嘎子写蓝。。

public static PixLine getPixLine(BufferedImage image, int lineIndex) {

int[] pxs=new int[image.getWidth()];

for(int i=0;i<image.getWidth();i++)

{

pxs[i]=image.getRGB(i, lineIndex);

}

PixLine pl=new PixLine();

pl.pxs=pxs;

return pl;

}

作者:: 老哇的爪子 Attilax 艾龙, EMAIL:1466519819@qq.com

转载请注明来源: http://blog.csdn.net/attilax






3. Rgb分量提取

多谢sqcl的回答,还有一个问题,就是用BufferedImage.getRGB()返回的像素值是32位颜色值,要自己移位才能得到RBGA的各个分量值,有没有什么类可以配合BufferedImage直接取出某个像素的某个独立的分量值?



1

2

3

4

5

int pixel = 0xFF0000;

Color pixelColor = new Color(pixel);

int r = pixelColor.getRed();

int g = pixelColor.getGreen();

int b = pixelColor.getBlue();

不过,这样效率太低。用移位最好。如果觉得不方便,可以自己写个Helper类简单封装一下。







我们知道通过bufferedimage对象的getRGB(x,y)方法可以返回指定坐标的颜色int值 他可以通过

int R =(rgb & 0xff0000 ) >> 16 ;

int G= (rgb & 0xff00 ) >> 8 ;

int B= (rgb & 0xff );

转换成三个颜色分量





4. 其他读取像素

从BufferedImage对象中读取像素数据的代码如下:
[java] view plaincopy
1. int type= image.getType();
2. if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3. return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );
4. else
5. return image.getRGB( x, y, width, height, pixels, 0, width );
[java] view plaincopy
1. int type= image.getType();
2. if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3. return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );
4. else
5. return image.getRGB( x, y, width, height, pixels, 0, width );
首先获取图像类型,如果不是32位的INT型数据,直接读写RGB值即可,否则需要从Raster
对象中读取。

往BufferedImage对象中写入像素数据同样遵守上面的规则。代码如下:
[java] view plaincopy
1. int type= image.getType();
2. if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3. image.getRaster().setDataElements(x, y, width, height, pixels );
4. else
5. image.setRGB(x, y, width, height, pixels, 0, width );
[java] view plaincopy
1. int type= image.getType();
2. if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3. image.getRaster().setDataElements(x, y, width, height, pixels );
4. else
5. image.setRGB(x, y, width, height, pixels, 0, width );


读取图像可能因为图像文件比较大,需要一定时间的等待才可以,Java Advance Image
Processor API提供了MediaTracker对象来跟踪图像的加载,同步其它操作,使用方法如下:
[java] view plaincopy
1. MediaTracker tracker = new MediaTracker(this); //初始化对象
2. tracker.addImage(image_01, 1); // 加入要跟踪的BufferedImage对象image_001
3. tracker.waitForID(1, 10000) // 等待10秒,让iamge_01图像加载
[java] view plaincopy
1. MediaTracker tracker = new MediaTracker(this); //初始化对象
2. tracker.addImage(image_01, 1); // 加入要跟踪的BufferedImage对象image_001
3. tracker.waitForID(1, 10000) // 等待10秒,让iamge_01图像加载


从一个32位int型数据cARGB中读取图像RGB颜色值的代码如下:
[java] view plaincopy
1. int alpha = (cARGB >> 24)& 0xff; //透明度通道
2. int red = (cARGB >> 16) &0xff;
3. int green = (cARGB >> 8) &0xff;
4. int blue = cARGB & 0xff;
[java] view plaincopy
1. int alpha = (cARGB >> 24)& 0xff; //透明度通道
2. int red = (cARGB >> 16) &0xff;
3. int green = (cARGB >> 8) &0xff;
4. int blue = cARGB & 0xff;


将RGB颜色值写入成一个INT型数据cRGB的代码如下:
[java] view plaincopy
1. cRGB = (alpha << 24) | (red<< 16) | (green << 8) | blue;
[java] view plaincopy
1. cRGB = (alpha << 24) | (red<< 16) | (green << 8) | blue;


创建一个BufferedImage对象的代码如下:
[java] view plaincopy
1. BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
[java] view plaincopy
1. BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);


5. --code

ImgxPicPhotoSplitor.java atibrow prj





6. 参考

Java数字图像处理基础知识 - 必读 - 流浪的鱼 - 博客频道 - CSDN.NET.htm

Java数字图像处理基础知识 - 必读 - 流浪的鱼 - 博客频道 - CSDN.NET.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐