How do I convert a Color Image to Black and White using ImageMagick?
Asked Answered
E

3

9

Background: I performed an Indexed conversion in Gimp of a color image and the result was a nice B&W version of the source Image.

I have tried numerous options in ImageMagick to no avail. I can get close but never quite as clear and crisp as what gimp seems to do effortlessly.

Here is my source:

var bmp = new MagickImage(sourceImage);

bmp.Threshold(new ImageMagick.Percentage(60));

bmp.Resample(200, 200);
bmp.ColorType = ColorType.Bilevel;
bmp.BitDepth(1);
bmp.Settings.Compression = CompressionMethod.Group4;
bmp.Strip();

bmp.Format = MagickFormat.Tiff;

I have been adjusting the Threshold call and have tried various suggestions I have seen online with varying amounts of success.

magickimage has a feature called -monochrome but I have not found how that is achieved in the .net library.

I am sure this is possible but what is the best way to achieve a nice B&W conversion.

sample image

Einhorn answered 28/1, 2021 at 21:43 Comment(2)
Please post your input image or a link to it.Mephitic
edited to add a sample image. The light colors of this paper are designed to drop out in a B&W conversion. The pen and background should convert to black as should most words on the page.Einhorn
M
13

With ImageMagick 7, you can do Otsu thresholding.

Input:

enter image description here

magick check.png -alpha off -auto-threshold otsu x.png

Result:

enter image description here

There is no built-in equivalent in ImageMagick 6. However I have a script, otsuthresh that will do that At my web site

So in ImageMagick 6, you just have to do simple thresholding.

convert check.png -alpha off -threshold 50% y.png

Result:

enter image description here

I note that your input image has an opaque alpha channel which needs to be removed to get proper results.

Mephitic answered 1/2, 2021 at 20:7 Comment(7)
Any pointers for doing this exact same process in MagickImage.NET?Einhorn
Sorry, I do not know MagickImage.NET. Contact the magick.net creator. He might help you since it is coded in IM 7 or convert my IM 6 script. See github.com/dlemstra/Magick.NETMephitic
on windows using version 7.1.1 using like convert check.png -alpha off -threshold 50% y.png gave a mostly blank page but 80% gave the desired resultSandal
@Sandal The threshold % is image dependent and possibly version dependent if a bug in that version (yours or mine). But why are you using convert in Imagemagick 7. You should be using magick in place of convert.Mephitic
@Mephitic that's easy: using convert since it worked for me but magick with otsu didn't :)Sandal
Please show the image you tried to use for Otsu thresholding. Otsu thresholding only works if the image is basically two different brightness colors, i.e., two peaks in the histogram.Mephitic
What was the exact version 7.1.1.x that you are using?Mephitic
I
0

This is possibly the simplest way you can use Greyscale in Magick.NET.

MagickImage image = new MagickImage(imagePath);
image.Grayscale();

string fileName = image.FileName + "_grey.png";
image.Write(fileName);

Instead of image.FileName you can also directly use the image path if you have it. Optionally you can add a PixelIntensityMethod in Greyscale for eventually better results.

Don't forget to call image.RePage() when you want to crop the image.

Intolerant answered 12/4, 2022 at 8:45 Comment(0)
M
0

Otsu's method will perform better when dealing with texts over a background with slightly gradient colors:

How do I convert a Color Image to Black and White using ImageMagick?

According to @fmw42 's comparison on multiple threshold methods: the Local Adaptive(its equivalent in imagemagick is -lat) algorithm might work best on texts over background:

Also try to combine with connected components processing that was introduced in another answer made by @fmw42 to remove unnecessary edges/dots which might get confused by any further OCR process.

Meekins answered 27/1, 2023 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.