Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

.net core - How do I convert a Color Image to Black and White using ImageMagick?

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

question from:https://stackoverflow.com/questions/65945662/how-do-i-convert-a-color-image-to-black-and-white-using-imagemagick

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...