private void colorThresh(Mat src,int threshold){
  Size srcSize=src.size();
  int size=(int)(srcSize.height * srcSize.width) * 3;
  byte[] d=new byte[size];
  src.get(0,0,d);
  for (int i=0; i < size; i+=3) {
    if ((double)(d[i] & 0xff) == 255) {
      continue;
    }
    double max=Math.max(Math.max((double)(d[i] & 0xff),(double)(d[i + 1] & 0xff)),(double)(d[i + 2] & 0xff));
    double mean=((double)(d[i] & 0xff) + (double)(d[i + 1] & 0xff) + (double)(d[i + 2] & 0xff)) / 3;
    if (max > threshold && mean < max * 0.8) {
      d[i]=(byte)((double)(d[i] & 0xff) * 255 / max);
      d[i + 1]=(byte)((double)(d[i + 1] & 0xff) * 255 / max);
      d[i + 2]=(byte)((double)(d[i + 2] & 0xff) * 255 / max);
    }
 else {
      d[i]=d[i + 1]=d[i + 2]=0;
    }
  }
  src.put(0,0,d);
}

private void colorThresh ( Mat src , int threshold ) { Size srcSize = src . size ( ) ; int size = ( int ) ( srcSize . height * srcSize . width ) * 3 ; byte [ ] d = new byte [ size ] ; src . get ( 0 , 0 , d ) ; for ( int i = 0 ; i < size ; i += 3 ) { if ( ( double ) ( d [ i ] & 0xff ) == 255 ) { continue ; } double max = Math . max ( Math . max ( ( double ) ( d [ i ] & 0xff ) , ( double ) ( d [ i + 1 ] & 0xff ) ) , ( double ) ( d [ i + 2 ] & 0xff ) ) ; double mean = ( ( double ) ( d [ i ] & 0xff ) + ( double ) ( d [ i + 1 ] & 0xff ) + ( double ) ( d [ i + 2 ] & 0xff ) ) / 3 ; if ( max > threshold && mean < max * 0.8 ) { d [ i ] = ( byte ) ( ( double ) ( d [ i ] & 0xff ) * 255 / max ) ; d [ i + 1 ] = ( byte ) ( ( double ) ( d [ i + 1 ] & 0xff ) * 255 / max ) ; d [ i + 2 ] = ( byte ) ( ( double ) ( d [ i + 2 ] & 0xff ) * 255 / max ) ; } else { d [ i ] = d [ i + 1 ] = d [ i + 2 ] = 0 ; } } src . put ( 0 , 0 , d ) ; }
when a pixel have any of its three elements above the threshold value and the average of the three values are less than 80 % of the higher one , brings all three values to the max possible keeping the relation between them , any absolute white keeps the value , all others go to absolute black . src must be a 3 channel image with 8 bits per channel
