驗證碼識別演算法腐蝕和二值化處理演算法
阿新 • • 發佈:2018-12-27
腐蝕演算法實現程式碼
private static int[][] erosion(int[][] source,int threshold){ int width = source[0].length; int height = source.length; int[][] result = new int[height][width]; for(int i = 0;i < height; i++){ for(int j = 0;j < width; j++){ ///邊緣不進行操作,邊緣內才操作 if(i > 0 && j > 0 && i < height-1 && j<width-1){ int max = 0; ///對結構元素進行遍歷 for(int k = 0;k < sElement.length; k++){ int x=k/3;///商表示x偏移量 int y=k%3;///餘數表示y偏移量 if(sElement[k]!=0){ ///不為0時,必須全部大於閾值,否則就設定為0並結束遍歷 if(source[i-1+x][j-1+y] >= threshold){ if(source[i-1+x][j-1+y] > max){ max = source[i-1+x][j-1+y] ; } }else{ ////與結構元素不匹配,賦值0,結束遍歷 max = 0; break; } } } ////此處可以設定閾值,當max小於閾值的時候就賦為0 result[i][j] = max; }else{ ///直接賦值 result[i][j] = source[i][j]; }///end of the most out if-else clause . } }///end of outer for clause return result; }
膨脹演算法
private static int[][] dilate(int[][] source,int threshold){ int width = source[0].length; int height = source.length; int[][] result = new int[height][width]; for(int i = 0;i < height; i++){ for(int j = 0;j < width; j++){ ///邊緣不進行操作 if(i>0 && j>0 && i<height-1 && j<width-1){ int max = 0; ///對結構元素進行遍歷 for(int k = 0; k < sElement.length; k++){ int x = k/3;///商表示x偏移量 int y = k%3;///餘數表示y偏移量 if(sElement[k]!=0){ ///當結構元素中不為0時,取出影象中對應各項的最大值賦給影象當前位置作為灰度值 if(source[i+x-1][j+y-1] > max){ max = source[i+x-1][j+y-1] ; } } } ////此處可以設定閾值,當max小於閾值的時候就賦為0 if(max < threshold){ result[i][j] = 0; }else{ result[i][j] = max; } // result[i][j]=max; }else{ ///直接賦值 result[i][j] = source[i][j]; } } } return result; }