title: How to threshold any image (for black and white color seperation) in java?
body: <p><strong>SOLVED</strong> : Issue was "jpeg compression". Saving as ".png" worked.</p>
  
  <p>I had detected edges of an image using a canny filter program in java.
  After applying filter ... </p>
  
  <p><a href="https://i.stack.imgur.com/zY9Rr.jpg" rel="nofollow noreferrer">This is my image</a>
  If zoomed in ... 
  <a href="https://i.stack.imgur.com/98g7v.jpg" rel="nofollow noreferrer">Zoomed</a></p>
  
  <p>All have different shades of black and white.<br/>
  I want all my edge pixels as pure white(#FFFFFF) and the remaining portion black.</p>
  
  <p>Note: Different pixels may have different shades apart from the one above(#F7F7F7). The zoomed image above is just an example.  </p>
  
  <p>Edit:
  I had written this code to take effect on image ...</p>
  
  <pre><code>public void convert(){
      try{
          BufferedImage img = ImageIO.read(new File("input.jpg"));
          int rgb;
          int height = img.getHeight();
          int width = img.getWidth();
          File f = new File("newThreshold.jpg");
          Color white = new Color(255,255,255);
          int wh = white.getRGB();
  
          for (int h = 0; h&lt;height; h++){
              for (int w = 0; w&lt;width; w++){  
  
                  rgb = img.getRGB(w, h);
                  red = (rgb &amp; 0x00ff0000) &gt;&gt; 16;
                  green = (rgb &amp; 0x0000ff00) &gt;&gt; 8;
                  blue  =  rgb &amp; 0x000000ff;
                  if(red &gt;= 200 || blue &gt;= 200 || green &gt;= 200){
                       img.setRGB(w,h,wh);
                  }
              }
          }
  
          ImageIO.write(img,"jpg",f);
      }
      catch(Exception e){
      }
  }
  </code></pre>
  
  <p>Even after running the code, there is no change in my image.<br/>
  Even if the red, green and blue values are above 200, my image is not changing.
  <br/></p>
  
  <p><strong>UPDATE</strong>: Saving the image as ".png" rather than ".jpg" worked! <br/></p>