我在使用Java Swing和JAI的NetBeans平臺上製作應用程序。在這個我想做圖像處理。我使用X射線槍捕獲黑白圖像。之後我想繪製黑白圖像的直方圖。因此,對於直方圖來說,首先我們必須得到灰度或黑白圖像的像素值。那麼我們可以使用這個像素值繪製直方圖。那麼,我怎樣才能得到這個黑白圖像的像素值呢?如何獲得黑白圖像的像素值?
4
A
回答
4
這應該工作,如果您使用java.awt.image.BufferedImage。
既然你想創建一個直方圖,我想你會遍歷所有的像素。有返回單個像素值的方法。
int getRGB(int x, int y)
然而,由於循環將發生,我想你想使用這一個:
int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
當你的陣列,使用:
int alpha = (pixels[i] >> 24) & 0x000000FF;
int red = (pixels[i] >> 16) & 0x000000FF;
int green = (pixels[i] >>8) & 0x000000FF;
int blue = pixels[i] & 0x000000FF;
要提取頻道數據。不知道是否變量可以聲明爲字節(我們只使用數組中的整數的一個字節,儘管字節是有符號的,並且發生了不同的算術 - 二的補碼形式),但是您可以聲明它們是短的。
然後瓶坯一些數學上的這些值,例如:
int average = (red + green + blue)/3;
這將返回的平均水平像素,給你一個點,你可以在一個簡單的亮度直方圖使用。
編輯:
關於直方圖製作,我已經使用這個類。它將您想要的直方圖作爲其參數的圖像採用setImage(BufferedImage image)
方法。使用updateHistogram()
進行數組填充。繪圖數據在paintComponent(Graphics g)
。我必須承認,這是渺茫的,特別是在計算偏移量時,但它可以很容易地簡化。
這是全班同學:
class HistogramCtrl extends JComponent
{
BufferedImage m_image;
int[] m_histogramArray = new int[256]; //What drives our histogram
int m_maximumPixels;
public HistogramCtrl(){
m_maximumPixels = 0;
for(short i = 0; i<256; i++){
m_histogramArray[i] = 0;
}
}
void setImage(BufferedImage image){
m_image = image;
updateHistogram();
repaint();
}
void updateHistogram(){
if(m_image == null) return;
int[] pixels = m_image.getRGB(0, 0, m_image.getWidth(), m_image.getHeight(), null, 0, m_image.getWidth());
short currentValue = 0;
int red,green,blue;
for(int i = 0; i<pixels.length; i++){
red = (pixels[i] >> 16) & 0x000000FF;
green = (pixels[i] >>8) & 0x000000FF;
blue = pixels[i] & 0x000000FF;
currentValue = (short)((red + green + blue)/3); //Current value gives the average //Disregard the alpha
assert(currentValue >= 0 && currentValue <= 255); //Something is awfully wrong if this goes off...
m_histogramArray[currentValue] += 1; //Increment the specific value of the array
}
m_maximumPixels = 0; //We need to have their number in order to scale the histogram properly
for(int i = 0; i < m_histogramArray.length;i++){ //Loop through the elements
if(m_histogramArray[i] > m_maximumPixels){ //And find the bigges value
m_maximumPixels = m_histogramArray[i];
}
}
}
protected void paintComponent(Graphics g){
assert(m_maximumPixels != 0);
Rectangle rect = g.getClipBounds();
Color oldColor = g.getColor();
g.setColor(new Color(210,210,210));
g.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
g.setColor(oldColor);
String zero = "0";
String thff = "255";
final short ctrlWidth = (short)rect.getWidth();
final short ctrlHeight = (short)rect.getHeight();
final short activeWidth = 256;
final short activeHeight = 200;
final short widthSpacing = (short)((ctrlWidth - activeWidth)/2);
final short heightSpacing = (short)((ctrlHeight - activeHeight)/2);
Point startingPoint = new Point();
final int substraction = -1;
startingPoint.x = widthSpacing-substraction;
startingPoint.y = heightSpacing+activeHeight-substraction;
g.drawString(zero,widthSpacing-substraction - 2,heightSpacing+activeHeight-substraction + 15);
g.drawString(thff,widthSpacing+activeWidth-substraction-12,heightSpacing+activeHeight-substraction + 15);
g.drawLine(startingPoint.x, startingPoint.y, widthSpacing+activeWidth-substraction, heightSpacing+activeHeight-substraction);
g.drawLine(startingPoint.x,startingPoint.y,startingPoint.x,heightSpacing-substraction);
double factorHeight = (double)activeHeight/m_maximumPixels; //The height divided by the number of pixels is the factor of multiplication for the other dots
Point usingPoint = new Point(startingPoint.x,startingPoint.y);
usingPoint.x+=2; //I want to move this two points in order to be able to draw the pixels with value 0 a bit away from the limit
Point tempPoint = new Point();
for(short i = 0; i<256; i++){
tempPoint.x = usingPoint.x;
tempPoint.y = (int)((heightSpacing+activeHeight-substraction) - (m_histogramArray[i] * factorHeight));
if((i!=0 && (i % 20 == 0)) || i == 255){
oldColor = g.getColor();
g.setColor(oldColor.brighter());
//Draw horizontal ruler sections
tempPoint.x = widthSpacing + i;
tempPoint.y = heightSpacing+activeHeight-substraction+4;
g.drawLine(tempPoint.x,tempPoint.y,widthSpacing + i,heightSpacing+activeHeight-substraction-4);
if(i <= 200){
//Draw vertical ruler sections
tempPoint.x = widthSpacing - substraction - 3;
tempPoint.y = heightSpacing+activeHeight-substraction-i;
g.drawLine(tempPoint.x,tempPoint.y,widthSpacing - substraction + 4, heightSpacing+activeHeight-substraction-i);
}
tempPoint.x = usingPoint.x;
tempPoint.y = usingPoint.y;
g.setColor(oldColor);
}
g.drawLine(usingPoint.x, usingPoint.y, tempPoint.x, tempPoint.y);
usingPoint.x++; //Set this to the next point
}
}
}
相關問題
- 1. 獲取灰度或黑白圖像的每個像素的像素值?
- 2. 查找白色/黑色像素數在二值圖像
- 3. 如何將現有的彩色圖像變成黑白圖像?
- 4. 黑白圖像處理
- 5. imageJ如何獲得像素RGB值?
- 6. 如何在PHP中將圖像轉換爲黑白圖像
- 7. 瞭解畫布如何將圖像轉換爲黑白圖像
- 8. 如何識別圖像是黑白還是彩色圖像?
- 9. 從黑色和白色1D陣列像素創建圖像
- 10. Python:灰度圖像:使一切變白,除了黑色像素
- 11. 如何獲得圖像的src值?
- 12. 確定圖像是黑白圖像還是彩色圖像
- 13. 如何獲得圖像的像素在win8的應用
- 14. 獲取位圖中的圖像,但只返回黑色像素
- 15. java如何從黑白圖像獲取布爾數組
- 16. 獲取圖像中的像素值
- 17. PROCESSING:獲得灰度圖像的像素值
- 18. 如何獲得墊圖像的像素區域
- 19. 如何獲得圖像源值
- 20. 黑白色(像素),請與Cocos2D中
- 21. 如何從純數據網絡攝像頭獲得像素值?
- 22. 黑白圖像直方圖(MatLab)
- 23. 黑白圖像在Matlab中的顏色
- 24. jQuery的 - 黑白圖像的彩色圖像(懸停)
- 25. 檢測圖像中的峯值黑色像素(Android)?
- 26. 如何將圖像的顏色更改爲android中的黑白圖像
- 27. 如何獲取灰度圖像中的像素值
- 28. 如何在PHP中獲取圖像的像素值?
- 29. OpenCV二進制圖像只得到白色像素不工作
- 30. 如何獲得輪廓的像素點?
我已經嘗試過,但它並沒有確切的答案。 – Jay 2012-04-12 12:43:14
你需要幫助創建/繪製直方圖嗎? – 2012-04-12 12:51:30
是的。我想繪製直方圖。 – Jay 2012-04-12 13:29:13