验证码识别算法腐蚀和二值化处理算法

    xiaoxiao2025-09-07  664

    腐蚀算法实现代码 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;       }         
    转载请注明原文地址: https://ju.6miu.com/read-1302387.html
    最新回复(0)