convert RGB colorspace to Homochromy(Black/whit)

//convert RGB colorspace to Homochromy(Black/whit) 

  
static public int ConvertRGBtoHomochromy(int x,int bw)

  
{

        String rgbString 
= Integer.toHexString(x).toUpperCase();

        
if (rgbString.length()!=6)

        
{

              
return Integer.parseInt("000000"16);

        }


        String strR,strG,strB;

        strR 
= rgbString.substring(02);

        strG 
= rgbString.substring(24);

        strB 
= rgbString.substring(46);

        

        
int intR,intG,intB;

        intR 
= Integer.parseInt(strR, 16);

        intG 
= Integer.parseInt(strG, 16);

        intB 
= Integer.parseInt(strB, 16);

        
//get lum value from RGB

        
int y = (int)(0.299 * intR + 0.587 * intG + 0.114 * intB);

      
if(y>bw) 

      
{

        y 
= 255;

      }


      
else

      
{

        y 
= 0;

      }


        String result 
= Integer.toHexString(y).toUpperCase()+ Integer.toHexString(y).toUpperCase()

        
+ Integer.toHexString(y).toUpperCase();

        
return Integer.parseInt(result, 16);

  }


      ColorModel    cm    =image.getColorModel();

      if((image.getType()!=BufferedImage.TYPE_BYTE_BINARY)||(cm.getPixelSize()!=1)){

      //BufferedImage grayImage=new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_BYTE_BINARY);

        

      //grayImage.getGraphics().drawImage(image,0,0,null);

       WritableRaster   raster =image.getRaster();

         //image = grayImage;

         DataBufferInt   buffer=(DataBufferInt)raster.getDataBuffer();

         int[]           imgdata=(int[])buffer.getData();

        

       

         for(int i=0; i<imgdata.length;i++)

         {

             imgdata[i] = ConvertRGBtoHomochromy(imgdata[i],190);              

              //i=0;

         }

         image.setData(raster);

          //image = convert(image,BufferedImage.TYPE_BYTE_BINARY);

         File jpgFile = new File("C:""Documents and Settings""ar""My Documents","afterbw.jpg");jpgFile.delete();

         ImageIO.write(image, "jpg", jpgFile);

below code is how to get threshold value from image for convertor.

static public int getThreshold(BufferedImage image)
  
{
      
int thresholdValue=1// threshold
      int[] ihist = new int[256]; //
      int i, j, k; // various counters
      int n, n1, n2, gmin, gmax;
      
double m1, m2, sum, csum, fmax, sb;
       
// reset
      for(i=0;i<256;i++){
        ihist[i] 
= 0;
      }

      gmin
=255; gmax=0;
      
for (i = 1; i < image.getWidth()-1 ; i++
      
{
          
for (j = 1; j < image.getHeight()-1 ; j++
          
{
               
//get gray value base on RGB value
              int cn = getGrayValue(image.getRGB(i, j));
               ihist[cn]
++;
              
if(cn > gmax) gmax=cn;
              
if(cn < gmin) gmin=cn;
              }

       }

       
// set up everything
       sum = csum = 0.0;
       n 
= 0;
       
for (k = 0; k <= 255; k++
       
{
           sum 
+= (double) k * (double) ihist[k]; /* x*f(x) */
           n 
+= ihist[k]; /* f(x) */
       }

       
if (n==0
       
{
        
// if n has no value, there is problems
        return (60);
       }

       
// do the otsu global thresholding method
       fmax = -1.0;
       n1 
= 0;
       
for (k = 0; k < 255; k++
       
{
           n1 
+= ihist[k];
           
if (n1==0continue; }
           n2 
= n - n1;
           
if (n2 == 0break; }
           csum 
+= (double) k *ihist[k];
           m1 
= csum / n1;
           m2 
= (sum - csum) / n2;
           sb 
= (double) n1 *(double) n2 *(m1 - m2) * (m1 - m2);
           
/* bbg: note: can be optimized. */
           
if (sb > fmax) 
           
{
               fmax 
= sb;
               thresholdValue 
= k;
           }

       }

       
// at this point we have our thresholding value
       return thresholdValue;      
  }

and the  orginal codes be changed:

  int thresholdValue = getThreshold(image);
         
for(int i=0; i<imgdata.length;i++)
         
{
             
//imgdata[i] = ConvertRGBtoGray(imgdata[i]);
             imgdata[i] = ConvertRGBtoHomochromy(imgdata[i],thresholdValue);             
             
//i=0;
         }

添加 edge detect ,用来找到边界去掉背景块

static public BufferedImage edgeDetect(BufferedImage img){

        
int w = img.getWidth(null);
        
int h = img.getHeight(null);
        BufferedImage newImage 
= new BufferedImage(w,h,1); 
        
for (int y=0;y<h;y++)

            
for (int x=0;x<w;x++){
                
int c=img.getRGB(x,y);
                newImage.setRGB(x,y,c);
            }

        
int c,red,green,blue,avg,d=3;
         
for (int y=0;y<h;y++)

             
for (int x=0;x<w;x++){

               c
=img.getRGB(x,y);
               red
=getRed(c);
               green
=getGreen(c);
               blue
=getBlue(c);
               avg
=(red+green+blue)/d;
               newImage.setRGB(x,y,createRGB(avg,avg,avg));
             }

         Kernel k 
= new Kernel(33new float[] 0.0f1.0f0.0f
                                                          
1.0f-4.0f1.0f
                                                          
0.0f1.0f0.0f }
);

               ConvolveOp op 
= new ConvolveOp(k);
               BufferedImage img2 
= op.filter(newImage, null);
               
return img2;     
    }



添加去杂点的处理方法:

static private int getMiddleValue(int[] arrayValue){
      
int v;
      
for(int i=0;i<=7;i++)
      
{
          
for(int j=0;j<=7;j++)
          
{
              
if (arrayValue[j]<arrayValue[j+1]) {
                 v 
= arrayValue[j];
                 arrayValue[j] 
= arrayValue[j+1];
                 arrayValue[j
+1= v;
              }
 
          }

      }

    
return arrayValue[5];
      
  }

  
  
static private BufferedImage Opt(BufferedImage image)
  
{   
      
int width = image.getWidth();
      
int height = image.getHeight();
      
      
int[] c = new int[9];
      
for(int i=1;i<width-1;i++)
      
{
          
for(int j=1;j<height-1;j++){
             
int cc = image.getRGB(i, j);
             c[
0= image.getRGB(i-1, j-1);
             c[
1= image.getRGB(i, j-1);
             c[
2= image.getRGB(i+1, j-1);
             c[
3= image.getRGB(i-1, j);
             c[
4= image.getRGB(i, j);
             c[
5= image.getRGB(i+1, j);
             c[
6= image.getRGB(i-1, j+1);
             c[
7= image.getRGB(i, j+1);
             c[
8= image.getRGB(i+1, j+1);
             cc 
= getMiddleValue(c);
             image.setRGB(i,j,cc);    
          }

      }

      
return image;
  }

更新edge detect 算法

static public int [] apply_sobel(int [] src_1d, int width, int height, double sobscale,
            
float offsetval) {

      
int i_w = width;
      
int i_h = height;
      
int d_w = width;
      
int d_h = height;
      
int[] dest_1d = new int[d_w * d_h];

      
for(int i=0;i<src_1d.length;i++){
          
try {

              
int a = src_1d[i] & 0x000000ff;
              
int b = src_1d[i+ 1& 0x000000ff;
              
int c = src_1d[i+ 2& 0x000000ff;
              
int d = src_1d[i + i_w] & 0x000000ff;
              
int e = src_1d[i + i_w + 2& 0x000000ff;
              
int f = src_1d[i + 2*i_w ] & 0x000000ff;
              
int g = src_1d[i + 2*i_w + 1& 0x000000ff;
              
int h = src_1d[i + 2*i_w + 2& 0x000000ff;
              
int hor = (a+d+f) - (c+e+h);
              
if (hor < 0) hor = -hor;
              
int vert = (a+b+c) - (f+g+h);
              
if (vert < 0) vert = -vert;
              
short gc = (short) (sobscale * (hor + vert));
              gc 
= (short) (gc + offsetval);
              
if (gc > 255) gc = 255;
              dest_1d[i] 
= 0xff000000 | gc<<16 | gc<<8 | gc;

  
//reached borders of image so goto next row
  
//(see Convolution.java)
              if (((i+3)%i_w)==0)  {
                  dest_1d[i] 
= 0;
                  dest_1d[i
+1= 0;
                  dest_1d[i
+2= 0;
                  i
+=3;
                  }

          }
 catch (ArrayIndexOutOfBoundsException e) {
//if reached row boudary of image return.
              i = src_1d.length;
          }

      }

      
return dest_1d;
  }


 

原文地址:https://www.cnblogs.com/umlchina/p/1125412.html