Java 對二值化圖片識別連通域

來源:互聯網
上載者:User

標籤:十進位   ati   public   連通   except   getwidth   cep   imageio   src   

用Java 對 已經 二值化了的圖片 標記連通域

每塊的連通域都標記不一樣的數字

 1     public static void main(String [] args) throws IOException { 2         //二值化 3         BufferedImage image = ImageIO.read(new File("F:/MyCode/LianTongYu/specialGray.jpg")); 4         int w = image.getWidth(); 5         int h = image.getHeight(); 6  7         int rgb = image.getRGB(0, 0); 8         int arr[][] = new int[h][w]; 9                  // 擷取圖片每一像素點的灰階值10                  for (int i = 0; i < h; i++) {11                          for (int j = 0; j < w; j++) {12                                  // getRGB()返回預設的RGB顏色模型(十進位)13                               //  arr[i][j] = image.getRGB(i, j) == -1 ?0 : 1 ;//該點的灰階值14                              int tmp= image.getRGB(j, i);15                              arr[i][j] =tmp==-1? 0:1;16                              }17                     }18         int res=getCount(arr);19         System.out.println(res);20     }21 22     public static int getCount(int[][] A) {23         int result = 0;24         for (int i = 0; i < A.length; i++) {25             for (int j = 0; j < A[0].length; j++) {26                 if (A[i][j] == 1) {27                     result++;28                     erase(A, i, j,result+1);29                 }30             }31         }32 33         // 統計數值34         int arrsum[] = new int [result+2];35         // 讀取矩陣36         for (int i = 0; i < A.length; i++) {37             for (int j = 0; j < A[0].length; j++) {38                 if(A[i][j]!=0)39                 arrsum[A[i][j]]+=1;40                System.out.print(A[i][j]+" ");41                 }42                 System.out.println();43             }44  45             //輸出統計的數值46         for (int i = 0; i < arrsum.length; i++) {47             System.out.println(arrsum[i]);48         }49 50         return result;51     }52 53     public static void erase(int[][] A, int i, int j,int res1) {54         A[i][j] = res1;55         while (i - 1 >= 0 && A[i - 1][j] == 1) {56             erase(A, i - 1, j,res1);57         }58         while (i + 1 < A.length && A[i + 1][j] == 1) {59             erase(A, i + 1, j,res1);60         }61         while (j - 1 >= 0 && A[i][j - 1] == 1) {62             erase(A, i, j - 1,res1);63         }64         while (j + 1 < A[0].length && A[i][j + 1] == 1) {65             erase(A, i, j + 1,res1);66         }67 68     }69 }

 

 

圖片:

 

結果:

 

 

 @[email protected]

 

Java 對二值化圖片識別連通域

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.