public void applyMedianMask(int Rad)
{
  int sz=(Rad*2+1)*(Rad*2+1);
  double[] dis=new double[sz];
  Point3d[] col=new Point3d[sz];
  for (int i=0;i<sz;i++) col[i]=new Point3d(0,0,0);
  
  if (!im_done) return;
  Graphics gg=cv.getGraphics();
  for (int x=Rad;x<wi-Rad;x++)
   for (int y=Rad;y<he-Rad;y++)
    { 
   gg.setColor(Color.white);
   gg.drawLine(x,y,x+1,y);
     int k=0;
     for (int i=-Rad;i<=Rad;i++)
       for (int j=-Rad;j<=Rad;j++)
        {
          col[k].x=R[(y+i)*wi+(x+j)];
          col[k].y=G[(y+i)*wi+(x+j)];
          col[k].z=B[(y+i)*wi+(x+j)];
          //dis[k]=rgbdist0(col[k].x,col[k].y,col[k].z,0,0,0);
          dis[k]=hsvdist(col[k].x,col[k].y,col[k].z,0,col[k].y,col[k].z);
          k++;
       }

     int i=0;
     for ( i=0;i<(sz/2)+1;i++)
      for (int j=i+1;j<sz;j++)
       {
        if (dis[i]>dis[j])
         {double t=dis[j];
          dis[j]=dis[i];
          dis[i]=t;
          Point3d p=col[j];
          col[j]=col[i];
          col[i]=p;
         }
       }
     resR[y*wi+x]=(short)(0.5+col[i-1].x);
     resG[y*wi+x]=(short)(0.5+col[i-1].y);
     resB[y*wi+x]=(short)(0.5+col[i-1].z);

   gg.setColor(new Color(resR[y*wi+x], resG[y*wi+x], resB[y*wi+x]));
   gg.drawLine(x,y,x,y);
  }
}