Atitit.遍历图像像素点rgb java attilax总结
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
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Atitit.遍历图像像素点rgb java attilax总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV2:等间隔采样和局部均值的图
- 下一篇: 盒子模型与DOCTYPE