public static BufferedImage blackWhite(BufferedImage input, int threshold) {
        double RED_FACTOR = 0.299;
        double BLUE_FACTOR = 0.114;
        double GREEN_FACTOR = 0.587;

        BufferedImage result = new BufferedImage(input.getWidth(), input.getHeight(), input.getType());


        for (int i = 0; i < result.getWidth(); i++)
            for (int j = 0; j < result.getHeight(); j++) {

                Color c = new Color(input.getRGB(i, j));
                double grey = RED_FACTOR * c.getRed() + GREEN_FACTOR * c.getGreen() + BLUE_FACTOR * c.getBlue();

                if (grey < threshold) {
                    result.setRGB(i, j, Color.BLACK.getRGB());
                } else {
                    result.setRGB(i, j, Color.WHITE.getRGB());
                }
            }
        return result;
    }