How to convert image to black and white using Java

前端 未结 4 615
醉酒成梦
醉酒成梦 2020-12-03 04:27

I convert an image to black&white in imagemagick with a command like this:

convert myimg.png -monochrome  out3.png

I\'m wondering whet

4条回答
  •  情话喂你
    2020-12-03 04:32

    The effect you are achieving is not through a binarization by a pre-defined threshold, instead it is done by a technique called dithering. Many dithering methods works by propagation the error (the intensity in the present image - the binary output at a given point) and thus adjusting the next outputs. This is done to create a visual effect such that it might seem like the resulting image is not black & white -- if you don't look too closely at it.

    One such simple and well-known method goes by Floyd-Steinberg, a pseudo-code for it is:

    for y := 1 to image height
        for x := 1 to image width
            v := im(y, x)
            if v < 128 then
                result(y, x) := 0
            else
                result(y, x) := 255
            error := v - result(y, x)
            propagate_error(im, y, x, error)
    

    Where propagate_error for this method can be given as (without taking care of border cases):

        im(y,   x+1) := im(y,   x+1) + (7/16) * error
        im(y+1, x+1) := im(y+1, x+1) + (1/16) * error
        im(y+1, x  ) := im(y+1, x  ) + (5/16) * error
        im(y+1, x-1) := im(y+1, x-1) + (3/16) * error
    

    Considering the direct implementation of the pseudocode given, the following image at right is the binary version of the one at its left. There is in fact only black and white colors at the image at right, this is a trivial matter for those that know about this method but for those unaware this might seem like impossible. The patterns created give the impression that there are several gray tones, depending from far you look at the image.

    enter image description here enter image description here

提交回复
热议问题