Aurigma Forums
 » 
Graphics Mill
 » 
Discussions – Graphics Mill
 » 
Convert an image to black and white with some thresholding
 
		
        
            
            
	
    | 
Rank: Newbie
 Groups: Guest
Joined: 7/3/2017(UTC)
 Posts: 1
 
 Thanks: 1 times
 | 
            
	      
                I have an indexed bitmap that I need to convert to a black and white (monochrome, not grayscale). How can I apply some custom threshold to it? For example, i want all pixels that are less than some thresholds to be white, others black? | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
         
		   
        
            
            
	
    | 
Rank: Advanced Member
 Groups: Guest
Joined: 8/19/2016(UTC)
 Posts: 33
 
 Was thanked: 1 time(s) in 1 post(s)
 | 
            
	      
                Hi Wolszhang, You can implement this as follows: Code:
//Setting desired threshold
int threshold = 170;
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(@"input.jpg");
for(int i = 0; i < bitmap.Palette.Count; i++)
{
     // RGB-to-brightness calculation taken from https://stackoverflow.com/a/596243
     var brightness = (0.299 * bitmap.Palette[i].R + 0.587 * bitmap.Palette[i].G + 0.114 * bitmap.Palette[i].B);
     if (brightness > threshold)
     {
          bitmap.Palette[i] = Aurigma.GraphicsMill.RgbColor.White;
     }
     else
     {
          bitmap.Palette[i] = Aurigma.GraphicsMill.RgbColor.Black;
     }
}
bitmap.ColorManagement.Convert(PixelFormat.Format1bppIndexed);
bitmap.Save(@"output.tif"); 
 Best regards, Julia | 
	
    | 
              1 user thanked Julia Khoptyan for this useful post. |  | 
    |  | 
        
        
        
    
		
        
            
            
	
    | 
Rank: Newbie
 Groups: Guest
Joined: 7/29/2017(UTC)
 Posts: 1
 
 | 
            
	      
                Hi, I also indexed bitmap that I need to convert to a black and white and want all pixels that are less than some thresholds to be black.I implemented the code as shown below,but it does not work well.What can I do for this and how can I fix the issue? Thanks! Edited by moderator Sunday, July 30, 2017 9:28:19 PM(UTC)
 | Reason: no external site links are allowed here! | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
         
		   
        
            
            
	
    | 
Rank: Advanced Member
 Groups: Guest
Joined: 7/4/2017(UTC)
 Posts: 43
 
 Was thanked: 12 time(s) in 12 post(s)
 | 
            
	      
                Hi daniel12, Please post your code that doesn't work and we will take a look. Could you also add some details on the issue? Regards, Andrew Edited by user Sunday, July 30, 2017 9:47:55 PM(UTC)
 | Reason: Not specified | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
    
		
        
            
            
	
    | 
Rank: Advanced Member
 Groups: Guest
Joined: 7/28/2003(UTC)
 Posts: 1,660
 
 Thanks: 5 timesWas thanked: 76 time(s) in 74 post(s)
 
 | 
            
	      
                Could you post an image you would like to convert? | 
| Best regards, Fedor Skvortsov
 | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
    
Aurigma Forums
 » 
Graphics Mill
 » 
Discussions – Graphics Mill
 » 
Convert an image to black and white with some thresholding
 
    
        Forum Jump
         
    
    You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.