JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 灰階圖片轉黑白的問題?變全黑了
justwait





發文: 13
積分: 0
於 2007-03-08 00:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
><不知為什麼 跑出來的圖片變成全黑
讀值出來 全是0
可以跟我說為什麼會這樣嗎? 謝謝!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BufferedImage bi=ImageIO.read(new File("graypicture.jpg"));
   width=bi.getWidth();
   height=bi.getHeight();
   
   int[] pixels = new int[width*height];
   bi.getRGB(0, 0, width, height, pixels, 0, width); 
   int[] gray=new int[width*height];
   
   for(int i=0;i<(width*height);i++){
             gray[i] = pixels[i] ;
     }           
   int[] bw=new int[width*height]; 
   for(int i=0;i<width*height;i++){
     if (bw[i] > 128)  
     {bw[i] = 255;}  
     else 
     {bw[i] = 0  ;}    }


reply to postreply to post
作者 Re:灰階圖片轉黑白的問題?變全黑了 [Re:justwait]
Shen





發文: 167
積分: 3
於 2007-03-08 01:47 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你少做一個RGB2Gray的動作!

reply to postreply to post
作者 Re:灰階圖片轉黑白的問題?變全黑了 [Re:justwait]
justwait





發文: 13
積分: 0
於 2007-03-09 00:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我把程式加進去了,出來的是紫色和黑色,而不是黑白。。。
是哪裡錯了?可以跟我講說一下嗎? 謝謝!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
width=bi.getWidth();
   height=bi.getHeight();
   int[] pixels = new int[width*height];
   bi.getRGB(0, 0, width, height, pixels, 0, width); 
   for (int y = 0; y < height; y++) {
      int i = y * width;
      for (int x = 0; x < width; x++) {
        int rgb = pixels[i];
        int r = (rgb & 0xff0000) >> 16;
        int g = (rgb & 0x00ff00) >> 8;
        int b = rgb & 0x0000ff;
 
       int va=(int)(0.299*r  + 0.587*g + 0.114*b);       if (va > 128)  
     {va = 255;}  
       else 
     {va = 0  ;}   
     pixels[i++]  = va;       }
    }


reply to postreply to post
作者 Re:灰階圖片轉黑白的問題?變全黑了 [Re:justwait]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2007-03-09 00:10 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
justwait wrote:
我把程式加進去了,出來的是紫色和黑色,而不是黑白。。。
是哪裡錯了?可以跟我講說一下嗎? 謝謝!


請先把基本的觀念弄懂,比如什麼是 RGB。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
width=bi.getWidth();
   height=bi.getHeight();
   int[] pixels = new int[width*height];
   bi.getRGB(0, 0, width, height, pixels, 0, width); 
   for (int y = 0; y < height; y++) {
      int i = y * width;
      for (int x = 0; x < width; x++) {
        int rgb = pixels[i];
        int r = (rgb & 0xff0000) >> 16;
        int g = (rgb & 0x00ff00) >> 8;
        int b = rgb & 0x0000ff;
 
       int va=(int)(0.299*r  + 0.587*g + 0.114*b);       if (va > 128)  
     {va = 0xFFFFFF;}  
       else 
     {va = 0  ;}   
     pixels[i++]  = va;       }
    }


reply to postreply to post

給我
辣味豆腐 其餘免談
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8