convert RGB colorspace to Homochromy(Black/whit)

时间:2021-12-24 00:21:05
convert RGB colorspace to Homochromy(Black/whit)// convert RGB colorspace to Homochromy(Black/whit) 
convert RGB colorspace to Homochromy(Black/whit)

convert RGB colorspace to Homochromy(Black/whit)  
static   public   int  ConvertRGBtoHomochromy( int  x, int  bw)
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)  
{
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        String rgbString 
= Integer.toHexString(x).toUpperCase();
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        
if (rgbString.length()!=6)
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)        
{
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)              
return Integer.parseInt("000000"16);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        }

convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        String strR,strG,strB;
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        strR 
= rgbString.substring(02);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        strG 
= rgbString.substring(24);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        strB 
= rgbString.substring(46);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        
int intR,intG,intB;
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        intR 
= Integer.parseInt(strR, 16);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        intG 
= Integer.parseInt(strG, 16);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        intB 
= Integer.parseInt(strB, 16);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        
//get lum value from RGB
convert RGB colorspace to Homochromy(Black/whit)

convert RGB colorspace to Homochromy(Black/whit)        
int y = (int)(0.299 * intR + 0.587 * intG + 0.114 * intB);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)      
if(y>bw) 
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)      
{
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        y 
= 255;
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)      }

convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)      
else
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)      
{
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        y 
= 0;
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)      }

convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        String result 
= Integer.toHexString(y).toUpperCase()+ Integer.toHexString(y).toUpperCase()
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        
+ Integer.toHexString(y).toUpperCase();
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        
return Integer.parseInt(result, 16);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)  }

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

      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.

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

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

convert RGB colorspace to Homochromy(Black/whit)       }

convert RGB colorspace to Homochromy(Black/whit)       
// set up everything
convert RGB colorspace to Homochromy(Black/whit)
       sum = csum = 0.0;
convert RGB colorspace to Homochromy(Black/whit)       n 
= 0;
convert RGB colorspace to Homochromy(Black/whit)       
for (k = 0; k <= 255; k++
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)       
{
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)           sum 
+= (double) k * (double) ihist[k]; /* x*f(x) */
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)           n 
+= ihist[k]; /* f(x) */
convert RGB colorspace to Homochromy(Black/whit)       }

convert RGB colorspace to Homochromy(Black/whit)       
if (n==0
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)       
{
convert RGB colorspace to Homochromy(Black/whit)        
// if n has no value, there is problemsconvert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)
        return (60);
convert RGB colorspace to Homochromy(Black/whit)       }

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

convert RGB colorspace to Homochromy(Black/whit)       }

convert RGB colorspace to Homochromy(Black/whit)       
// at this point we have our thresholding value
convert RGB colorspace to Homochromy(Black/whit)
       return thresholdValue;      
convert RGB colorspace to Homochromy(Black/whit)  }

and the  orginal codes be changed:

convert RGB colorspace to Homochromy(Black/whit)   int  thresholdValue  =  getThreshold(image);
convert RGB colorspace to Homochromy(Black/whit)         
for ( int  i = 0 ; i < imgdata.length;i ++ )
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)         
{
convert RGB colorspace to Homochromy(Black/whit)             
//imgdata[i] = ConvertRGBtoGray(imgdata[i]);
convert RGB colorspace to Homochromy(Black/whit)
             imgdata[i] = ConvertRGBtoHomochromy(imgdata[i],thresholdValue);             
convert RGB colorspace to Homochromy(Black/whit)             
//i=0;
convert RGB colorspace to Homochromy(Black/whit)
         }

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

convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)static   public  BufferedImage edgeDetect(BufferedImage img) {
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)        
int w = img.getWidth(null);
convert RGB colorspace to Homochromy(Black/whit)        
int h = img.getHeight(null);
convert RGB colorspace to Homochromy(Black/whit)        BufferedImage newImage 
= new BufferedImage(w,h,1); 
convert RGB colorspace to Homochromy(Black/whit)        
for (int y=0;y<h;y++)
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)            
for (int x=0;x<w;x++){
convert RGB colorspace to Homochromy(Black/whit)                
int c=img.getRGB(x,y);
convert RGB colorspace to Homochromy(Black/whit)                newImage.setRGB(x,y,c);
convert RGB colorspace to Homochromy(Black/whit)            }

convert RGB colorspace to Homochromy(Black/whit)        
int c,red,green,blue,avg,d=3;
convert RGB colorspace to Homochromy(Black/whit)         
for (int y=0;y<h;y++)
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)             
for (int x=0;x<w;x++){
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)               c
=img.getRGB(x,y);
convert RGB colorspace to Homochromy(Black/whit)               red
=getRed(c);
convert RGB colorspace to Homochromy(Black/whit)               green
=getGreen(c);
convert RGB colorspace to Homochromy(Black/whit)               blue
=getBlue(c);
convert RGB colorspace to Homochromy(Black/whit)               avg
=(red+green+blue)/d;
convert RGB colorspace to Homochromy(Black/whit)               newImage.setRGB(x,y,createRGB(avg,avg,avg));
convert RGB colorspace to Homochromy(Black/whit)             }

convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)         Kernel k 
= new Kernel(33new float[] 0.0f1.0f0.0f
convert RGB colorspace to Homochromy(Black/whit)                                                          
1.0f-4.0f1.0f
convert RGB colorspace to Homochromy(Black/whit)                                                          
0.0f1.0f0.0f }
);
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)               ConvolveOp op 
= new ConvolveOp(k);
convert RGB colorspace to Homochromy(Black/whit)               BufferedImage img2 
= op.filter(newImage, null);
convert RGB colorspace to Homochromy(Black/whit)               
return img2;     
convert RGB colorspace to Homochromy(Black/whit)    }

convert RGB colorspace to Homochromy(Black/whit)



添加去杂点的处理方法:

convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)static   private   int  getMiddleValue( int [] arrayValue) {
convert RGB colorspace to Homochromy(Black/whit)      
int v;
convert RGB colorspace to Homochromy(Black/whit)      
for(int i=0;i<=7;i++)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)      
{
convert RGB colorspace to Homochromy(Black/whit)          
for(int j=0;j<=7;j++)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)          
{
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)              
if (arrayValue[j]<arrayValue[j+1]) {
convert RGB colorspace to Homochromy(Black/whit)                 v 
= arrayValue[j];
convert RGB colorspace to Homochromy(Black/whit)                 arrayValue[j] 
= arrayValue[j+1];
convert RGB colorspace to Homochromy(Black/whit)                 arrayValue[j
+1= v;
convert RGB colorspace to Homochromy(Black/whit)              }
 
convert RGB colorspace to Homochromy(Black/whit)          }

convert RGB colorspace to Homochromy(Black/whit)      }

convert RGB colorspace to Homochromy(Black/whit)    
return arrayValue[5];
convert RGB colorspace to Homochromy(Black/whit)      
convert RGB colorspace to Homochromy(Black/whit)  }

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

 

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

convert RGB colorspace to Homochromy(Black/whit)      }

convert RGB colorspace to Homochromy(Black/whit)      
return image;
convert RGB colorspace to Homochromy(Black/whit)  }

更新edge detect 算法

convert RGB colorspace to Homochromy(Black/whit)static   public   int  [] apply_sobel( int  [] src_1d,  int  width,  int  height,  double  sobscale,
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)            
float  offsetval)  {
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)      
int i_w = width;
convert RGB colorspace to Homochromy(Black/whit)      
int i_h = height;
convert RGB colorspace to Homochromy(Black/whit)      
int d_w = width;
convert RGB colorspace to Homochromy(Black/whit)      
int d_h = height;
convert RGB colorspace to Homochromy(Black/whit)      
int[] dest_1d = new int[d_w * d_h];
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)      
for(int i=0;i<src_1d.length;i++){
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)          
try {
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)              
int a = src_1d[i] & 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int b = src_1d[i+ 1& 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int c = src_1d[i+ 2& 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int d = src_1d[i + i_w] & 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int e = src_1d[i + i_w + 2& 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int f = src_1d[i + 2*i_w ] & 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int g = src_1d[i + 2*i_w + 1& 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int h = src_1d[i + 2*i_w + 2& 0x000000ff;
convert RGB colorspace to Homochromy(Black/whit)              
int hor = (a+d+f) - (c+e+h);
convert RGB colorspace to Homochromy(Black/whit)              
if (hor < 0) hor = -hor;
convert RGB colorspace to Homochromy(Black/whit)              
int vert = (a+b+c) - (f+g+h);
convert RGB colorspace to Homochromy(Black/whit)              
if (vert < 0) vert = -vert;
convert RGB colorspace to Homochromy(Black/whit)              
short gc = (short) (sobscale * (hor + vert));
convert RGB colorspace to Homochromy(Black/whit)              gc 
= (short) (gc + offsetval);
convert RGB colorspace to Homochromy(Black/whit)              
if (gc > 255) gc = 255;
convert RGB colorspace to Homochromy(Black/whit)              dest_1d[i] 
= 0xff000000 | gc<<16 | gc<<8 | gc;
convert RGB colorspace to Homochromy(Black/whit)
convert RGB colorspace to Homochromy(Black/whit)  
//reached borders of image so goto next row
convert RGB colorspace to Homochromy(Black/whit)  
//(see Convolution.java)
convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)
              if (((i+3)%i_w)==0)  {
convert RGB colorspace to Homochromy(Black/whit)                  dest_1d[i] 
= 0;
convert RGB colorspace to Homochromy(Black/whit)                  dest_1d[i
+1= 0;
convert RGB colorspace to Homochromy(Black/whit)                  dest_1d[i
+2= 0;
convert RGB colorspace to Homochromy(Black/whit)                  i
+=3;
convert RGB colorspace to Homochromy(Black/whit)                  }

convert RGB colorspace to Homochromy(Black/whit)convert RGB colorspace to Homochromy(Black/whit)          }
 catch (ArrayIndexOutOfBoundsException e) {
convert RGB colorspace to Homochromy(Black/whit)
//if reached row boudary of image return.
convert RGB colorspace to Homochromy(Black/whit)
              i = src_1d.length;
convert RGB colorspace to Homochromy(Black/whit)          }

convert RGB colorspace to Homochromy(Black/whit)      }

convert RGB colorspace to Homochromy(Black/whit)      
return dest_1d;
convert RGB colorspace to Homochromy(Black/whit)  }

convert RGB colorspace to Homochromy(Black/whit)