全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3038
推到 Plurk!
推到 Facebook!

如何PixelGrabber中取得Color實體

答題得分者是:neoart
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-03-17 09:14:05 IP:61.229.xxx.xxx 未訂閱
大家好,又有問題請教各位: 底下的程式碼可以顯示圖片並抓取每個pixel的R、G、B值,我現在想取每個pixel的Color實體,並用getRGB()抓取其RGB值。 有本書提到說必須要用Color(int)建構函式: Color pixelColor=new Color(pixel[index]); 我試了好久,還是沒成功,麻煩大家幫我看看....謝謝^__^" 程式碼: import javax.swing.JFrame; import java.awt.*; import java.io.*; import java.applet.*; import java.awt.Graphics; import java.awt.image.*; import java.net.URL; public class testFrame5 extends Component { static Image _image; static JFrame jf1; static int imw,imh, pixels[]; //construct with a specific image public testFrame5(Image im) { this._image = im; prepareImage(_image,this); } public void paint(Graphics g) { g.drawImage(_image,0,0,this); imw = _image.getWidth(this); imh = _image.getHeight(this); jf1.setSize(new Dimension(imw 5,imh 30)); } static void handlesinglepixel(int x, int y, int pixel) { int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; System.out.print("[" red "," green "," blue "]"); // Deal with the pixel as necessary... } static void handlepixels(Image img, int x, int y, int w, int h) { int[] pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return; } System.out.print("imw = " w ", imh = " h "\n"); for (int j = 0; j < h; j ) { System.out.println("第" j "列"); for (int i = 0; i < w; i ) { handlesinglepixel(x i, y j, pixels[j * w i]); System.out.print("\t"); } System.out.println(); } } public static void main(String args[]){ jf1 = new JFrame(); jf1.setTitle("*** Image Frame ***"); jf1.setBounds(0,0,1000,1000); jf1.setVisible(true); Image _image = Toolkit.getDefaultToolkit().getImage("grape.gif"); //29*25 testFrame5 cube = new testFrame5(_image); jf1.getContentPane().add(cube); jf1.show(); jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); handlepixels(_image, 0, 0, imw, imh); } } 發表人 - vespa 於 2004/03/17 09:31:18 發表人 - vespa 於 2004/03/17 09:33:28
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-03-17 17:58:52 IP:61.64.xxx.xxx 未訂閱
你是RBG值沒有找到嗎? 以下是我實驗的code =====================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;    public class Frame1 extends JFrame {
  JPanel contentPane;
  JScrollPane jScrollPane1 = new JScrollPane();
  JLabel jLabel1 = new JLabel();
  java.awt.Image  iconimg;
  JButton btnGetPixel = new JButton();
  //Construct the frame      public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }      //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    jScrollPane1.setBounds(new Rectangle(61, 58, 277, 171));
    jLabel1.setText("");
    btnGetPixel.setBounds(new Rectangle(67, 259, 94, 30));
    btnGetPixel.setText("get color");
    btnGetPixel.addActionListener(new Frame1_btnGetPixel_actionAdapter(this));
    contentPane.add(jScrollPane1, null);
    contentPane.add(btnGetPixel, null);
    jScrollPane1.getViewport().add(jLabel1, null);
    ImageIcon tempIcon=new ImageIcon("d:/你的A圖.jpg");
    iconimg=tempIcon.getImage();
    jLabel1.setIcon(tempIcon);
  }      //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
  public void handlesinglepixel(int x, int y, int pixel) {
         int alpha = (pixel >> 24) & 0xff;
         int red   = (pixel >> 16) & 0xff;
         int green = (pixel >>  8) & 0xff;
         int blue  = (pixel      ) & 0xff;
         // Deal with the pixel as necessary...
         System.out.println("Alpah:" alpha ",red:" red ",green:" green ",bule:" blue);
  }      public void handlepixels(Image img, int x, int y, int w, int h) {
         int[] pixels = new int[w * h];
         PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
         try {
             pg.grabPixels();
         } catch (InterruptedException e) {
             System.err.println("interrupted waiting for pixels!");
             return;
         }
         if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
             System.err.println("image fetch aborted or errored");
             return;
         }
         for (int j = 0; j < h; j  ) {
             for (int i = 0; i < w; i  ) {
                 handlesinglepixel(x i, y j, pixels[j * w   i]);
             }
         }
  }      void btnGetPixel_actionPerformed(ActionEvent e) {
     this.handlepixels(iconimg,0,0,
                       jLabel1.getIcon().getIconWidth(),
                       jLabel1.getIcon().getIconHeight());
  }    }    class Frame1_btnGetPixel_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;      Frame1_btnGetPixel_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.btnGetPixel_actionPerformed(e);
  }
}    
========================================================= main()請自己加上去.按了按鍵就可以看到你的console列出該圖素的alpha &RGB 值了. 希望是你想到的功能. 發表人 - neoart 於 2004/03/17 18:02:40 發表人 - neoart 於 2004/03/17 18:06:08
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-03-17 20:42:04 IP:61.229.xxx.xxx 未訂閱
謝謝你,neoart... 我的問題可能問得不好,您有點誤解我的意思。前面貼的程式碼是可以顯示一張圖片並show出每個pixel的R、G、B值(三個數值),我們現在只想在每個pixel中抓出一個數值,打算用getRGB()這個函式。 其實,您貼的程式碼我還看不太懂,連main都有點寫不出來,不過還是非常謝謝你撥空寫了那近百行的程式。 我今天上網查了資料,修改我的程式碼如下... 不過還有些bug需要除錯,如:有時候圖片抓不到,或是圖片抓到卻不能印出RGB數值,更誇張的是,在有些電腦上執行成功率比較高,而有些電腦卻得試好幾次才能勉強把RGB印出。 這是我們今年系統實作的主題,我們想做個影像比對的系統,希望最終能做到以靜態圖片搜尋相關的動態影像檔(該圖片非取自影像檔)。比如:有段一小時的新聞影片,我們想以報紙上的新聞圖片搜尋圖片在影片中的相關位置,當然圖片跟影片中的資料有部分相關,但不是完全相同,不過在那之前,我們可能要先完成圖片是取自影像檔的比對。 我們目前想先做兩張圖片的靜態比對,先抓取每個pixel的RGB值,放入矩陣當中,然後將兩張圖片的矩陣數值作比對,套用有本"影像尋取"論文中提到的門檻值K,決定其相似比例。我們現在是假設兩張圖片的解析度相同,之後再做解析度不同的比對,然後是圖片相似但或許角度不太一樣的比對(如兩張小叮噹,一張是正面圖,一張是側面圖),之後....我不太敢想,因為想到的都是有待解決的問題...。其實,我不知道我們目前的邏輯正不正確,說不定我們錯得一踏糊塗,或許正在閱讀的您可以修正我們的方向... 我們現在資源不多,圖書館有關影像比對的書並不豐富,網路上的資源雖然充沛,不過不知道我們需要的在哪個角落。想請問大家不知道有沒有相關的書籍或是網路上的資料可以推薦,還是哪些java的函式是我們可能會派上用場的...再次謝謝您閱讀我的文章,每當看到瀏覽的人數多了一點,自己就覺得問題解決的機會又多了一些... import javax.swing.JFrame; import java.awt.*; import java.awt.Graphics; import java.awt.image.*; //用getRGB()抓取pixel的RGB //three possiblities: //1.image fetch aborted or errored //2.圖片有show出來,可是寬、高抓不到 //3.正常狀態:show出圖片,印出RGB public class testFrame6 extends Component { static Image _image; static JFrame jf1; static int imw,imh, pixels[]; //construct with a specific image public testFrame6(Image im) { this._image = im; prepareImage(_image,this); } public void paint(Graphics g) { g.drawImage(_image,0,0,this); imw = _image.getWidth(this); imh = _image.getHeight(this); jf1.setSize(new Dimension(imw 5,imh 30)); //根據寬高設定邊框,5,30是經過調整之後加入的值 } //not use in this example static void handlesinglepixel(int x, int y, int pixel) { int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; System.out.print("[" red "," green "," blue "]"); // Deal with the pixel as necessary... } // get a Color object from pixel(x,y) static void getPixelColor( int x, int y,int pixel) { Color pixelColor = new Color((pixel>>16)&0xff, (pixel>>8)&0xff, (pixel)&0xff); int RGB = pixelColor.getRGB(); System.out.print("[" RGB "]"); } static void handlepixels(Image img, int x, int y, int w, int h) { int[] pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return; } System.out.print("imw = " w ", imh = " h "\n"); for (int j = 0; j < h; j ) { System.out.println("第" j "列"); for (int i = 0; i < w; i ) { //handlesinglepixel(x i, y j, pixels[j * w i]); getPixelColor(x i,y j,pixels[j*w i]); System.out.print("\t"); } System.out.println(); } } public static void main(String args[]){ jf1 = new JFrame(); jf1.setTitle("*** Image Frame ***"); jf1.setBounds(0,0,1000,1000); //先預設比較大的邊框 jf1.setVisible(true); Image _image = Toolkit.getDefaultToolkit().getImage("grape.gif"); //29*25 //Image _image = Toolkit.getDefaultToolkit().getImage("window.gif"); //400*250 testFrame6 cube = new testFrame6(_image); jf1.getContentPane().add(cube); jf1.show(); //呼叫JFrame jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); handlepixels(_image, 0, 0, imw, imh); } }
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-03-18 09:54:43 IP:61.64.xxx.xxx 未訂閱
看樣子不加上main()是有點為德不卒了.
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      Frame1 frame = new Frame1();
      //Validate frames that have preset sizes
      //Pack frames that have useful preferred size info, e.g. from their layout
        frame.validate();
      //Center the window
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = frame.getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
      frame.setVisible(true);
      
    }
    catch(Exception e) {
      e.printStackTrace();
    }      }
執行一下吧,圖素就可以看到RBG值了 至於百來行的程式....都嘛是用工具產生的.不用急著debug
Tzy
一般會員


發表:1
回覆:2
積分:0
註冊:2004-03-18

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-03-18 14:37:46 IP:163.13.xxx.xxx 未訂閱
你好 我是vespa的同學 關於你熱情的回應我們的問題 已透過vespa轉述 所以我特地上線加入會員 也想參與你們的討論 之前你所寫的Frame1.java,compile之後出現下面訊息: Frame1.java:99: 'class' or 'interface' expected public static void main(String args[]) { ^ 1 error 不知道是不是我有什麼疏忽,還是什麼地方弄錯了… <真糟…連訊息都看不懂> 至於debug的問題嘛… 這個地方不解決,就做不下去了 有時候出現"image fetch aborted or errored" 有時候圖片有show出來,可是寬、高抓不到 能夠成功run出來的機率太低了. 先感謝你 撥冗看這篇文章。
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-03-18 18:09:10 IP:61.64.xxx.xxx 未訂閱
//package 的所在請自行宣告....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;    public class Frame1 extends JFrame {
  JPanel contentPane;
  JScrollPane jScrollPane1 = new JScrollPane();
  JLabel jLabel1 = new JLabel();
  java.awt.Image  iconimg;
  JButton btnGetPixel = new JButton();      //Construct the frame      public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }      //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(400, 350));
    this.setTitle("Frame Title");
    jScrollPane1.setBounds(new Rectangle(61, 58, 277, 171));
    jLabel1.setText("");
    btnGetPixel.setBounds(new Rectangle(67, 259, 94, 30));
    btnGetPixel.setText("get color");
    btnGetPixel.addActionListener(new Frame1_btnGetPixel_actionAdapter(this));
    contentPane.add(jScrollPane1, null);
    contentPane.add(btnGetPixel, null);
    jScrollPane1.getViewport().add(jLabel1, null);
    ImageIcon tempIcon=new ImageIcon("d:/AgentSmith.jpg");
    iconimg=tempIcon.getImage();
    jLabel1.setIcon(tempIcon);
  }      //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
  public void handlesinglepixel(int x, int y, int pixel) {
         int alpha = (pixel >> 24) & 0xff;
         int red   = (pixel >> 16) & 0xff;
         int green = (pixel >>  8) & 0xff;
         int blue  = (pixel      ) & 0xff;
         // Deal with the pixel as necessary...
         System.out.println("Alpah:" alpha ",red:" red ",green:" green ",bule:" blue);
  }      public void handlepixels(Image img, int x, int y, int w, int h) {
         int[] pixels = new int[w * h];
         PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
         try {
             pg.grabPixels();
         } catch (InterruptedException e) {
             System.err.println("interrupted waiting for pixels!");
             return;
         }
         if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
             System.err.println("image fetch aborted or errored");
             return;
         }
         for (int j = 0; j < h; j  ) {
             for (int i = 0; i < w; i  ) {
                 handlesinglepixel(x i, y j, pixels[j * w   i]);
             }
         }
  }      void btnGetPixel_actionPerformed(ActionEvent e) {
     this.handlepixels(iconimg,0,0,
                       jLabel1.getIcon().getIconWidth(),
                       jLabel1.getIcon().getIconHeight());
  }
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      Frame1 frame = new Frame1();
      //Validate frames that have preset sizes
      //Pack frames that have useful preferred size info, e.g. from their layout
        frame.validate();
      //Center the window
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = frame.getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
      frame.setVisible(true);        }
    catch(Exception e) {
      e.printStackTrace();
    }      }
}    class Frame1_btnGetPixel_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;      Frame1_btnGetPixel_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.btnGetPixel_actionPerformed(e);
  }
}    
.....好久沒玩程式接龍了說.......
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-03-18 21:25:33 IP:61.229.xxx.xxx 未訂閱
謝謝你...neoart 您寫的程式碼我們已經測試出來了.... 我們自己也寫了一段程式碼(這個討論串的第三篇文章內的程式碼,我貼在下方),同樣可以show出圖片,不過我們取出的是RGB值,而不是像您寫的取出R、G、B三個數值還有透明度。這樣做的原因是因為可以將像素值簡單化(從三個數值變成一個數值),如此可減低比對的難度(如我之前所提,我們目前想做兩張靜態圖片的比對,而首要步驟就是取出每個像素的數值,與其取三個值倒不如只取出一個,因為同樣都可以代表那像素的顏色)。 不過下方的程式碼有點問題,要請大家再幫我看看: 執行有以下三種結果: 1.image fetch aborted or errored 2.圖片顯示卻沒有抓到圖片的寬高,以至於RGB數值印不出來 3.正常狀態,show出圖片,印出RGB 第1.2種不是我們預期的結果,我們現在只想讓程式乖乖的跑出第三種正常狀態,不過苦思不解,不知是哪段程式出了問題? 勞煩大家了...謝謝 程式碼: import javax.swing.JFrame; import java.awt.*; import java.awt.Graphics; import java.awt.image.*; //用getRGB()抓取pixel的RGB //three possiblities: //1.image fetch aborted or errored //2.圖片有show出來,可是寬、高抓不到 //3.正常狀態:show出圖片,印出RGB public class testFrame6 extends Component { static Image _image; static JFrame jf1; static int imw,imh, pixels[]; //construct with a specific image public testFrame6(Image im) { this._image = im; prepareImage(_image,this); } public void paint(Graphics g) { g.drawImage(_image,0,0,this); imw = _image.getWidth(this); imh = _image.getHeight(this); jf1.setSize(new Dimension(imw 5,imh 30)); //根據寬高設定邊框,5,30是經過調整之後加入的值 } //not use in this example static void handlesinglepixel(int x, int y, int pixel) { int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; System.out.print("[" red "," green "," blue "]"); // Deal with the pixel as necessary... } // get a Color object from pixel(x,y) static void getPixelColor( int x, int y,int pixel) { Color pixelColor = new Color((pixel>>16)&0xff, (pixel>>8)&0xff, (pixel)&0xff); int RGB = pixelColor.getRGB(); System.out.print("[" RGB "]"); } static void handlepixels(Image img, int x, int y, int w, int h) { int[] pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return; } System.out.print("imw = " w ", imh = " h "\n"); for (int j = 0; j < h; j ) { System.out.println("第" j "列"); for (int i = 0; i < w; i ) { //handlesinglepixel(x i, y j, pixels[j * w i]); getPixelColor(x i,y j,pixels[j*w i]); System.out.print("\t"); } System.out.println(); } } public static void main(String args[]){ jf1 = new JFrame(); jf1.setTitle("*** Image Frame ***"); jf1.setBounds(0,0,1000,1000); //先預設比較大的邊框 jf1.setVisible(true); Image _image = Toolkit.getDefaultToolkit().getImage("grape.gif"); //29*25 //Image _image = Toolkit.getDefaultToolkit().getImage("window.gif"); //400*250 testFrame6 cube = new testFrame6(_image); jf1.getContentPane().add(cube); jf1.show(); //呼叫JFrame jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); handlepixels(_image, 0, 0, imw, imh); } }
系統時間:2024-05-08 16:57:32
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!