Skip to content

Instantly share code, notes, and snippets.

@prakharaditya
Forked from MehdiNS/ordered_dithering.txt
Created December 30, 2021 18:51
Show Gist options
  • Save prakharaditya/e7606b075e23a90fb387f32dba5b18f9 to your computer and use it in GitHub Desktop.
Save prakharaditya/e7606b075e23a90fb387f32dba5b18f9 to your computer and use it in GitHub Desktop.

Revisions

  1. @MehdiNS MehdiNS revised this gist Sep 27, 2016. 1 changed file with 15 additions and 6 deletions.
    21 changes: 15 additions & 6 deletions ordered_dithering.txt
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,18 @@
    (Written as a way to stop forgetting these things. May be wrong sometimes, as it's just the result of my research here and there. If it helps you, give me a shout!)
    (Written as a way to stop forgetting these things.
    May be wrong sometimes, as it's just the result of my research here and there.
    If it helps you, give me a shout!)

    Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
    Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image

    ----------------------------------------------------------------
    INTRODUCTION

    The idea behind it is simple : given two available values a and b, let's say black and white, the value x between a and b - that should be grayish - is simulated by mixing pixels of colors a and b.
    The idea behind it is simple : given two available values a and b, let's say black and white,
    the value x between a and b - that should be grayish - is simulated by mixing pixels of colors a and b.
    To apply some ordered dithering on an image, we apply the same logic but in 2D by using a bayer matrix.
    By turning the pixel on in a very specified order, the matrix creates the perception of continuous variation of color.
    By turning the pixel on in a very specified order, the matrix creates the perception of
    continuous variation of color.

    Here an example of a variaton for a 2x2 matrix :

    @@ -19,15 +23,18 @@ Here an example of a variaton for a 2x2 matrix :
    BAYER MATRIX

    Dither matrix are power-of-2 matrix whose elements can be considered as threshold.
    In these matrices, consecutive threshold values are located far apart spatially, which gives the perception of a progressive variation. The values inside the matrix indicate how likely a pixel will be turned on.
    In these matrices, consecutive threshold values are located far apart spatially,
    which gives the perception of a progressive variation.
    The values inside the matrix indicate how likely a pixel will be turned on.
    You can rotate, mirror, transpose these matrices and it won't bring much change to the dithered image.

    ----------------------------------------------------------------
    HOW TO CONSTRUCT A DITHER MATRIX ?

    - By hand --> become quickly fastidious but is perfect to understand how the matrix works

    The simplest - yet useless - dither matrix is the zero matrix of dimension 1x1, this one doesn't apply any transformation. So yeah, useless.
    The simplest - yet useless - dither matrix is the zero matrix of dimension 1x1,
    this one doesn't apply any transformation. So yeah, useless.

    On the oher hand, the 2x2 matrix is fundamental :
    | 0 2 |
    @@ -73,7 +80,9 @@ HOW TO APPLY ORDERED DITHERING ON AN IMAGE ?
    Here we are gonna focus on black&white ordered dithering.
    The dither matrix is tiled across the image. The bigger the matrix is, the better it will look.
    For each pixel of the image to modify, look up the member of the matrix covering it.
    Then we use the matrix as a threshold mask : if the value of the pixel is superior to the one given by the matrix, the pixel is lit, if not it becomes black.
    Then we use the matrix as a threshold mask :
    if the value of the pixel is superior to the one given by the matrix, the pixel is lit, if not it becomes black.

    ----------------------------------------------------------------
    Done !

  2. @MehdiNS MehdiNS revised this gist Sep 27, 2016. 2 changed files with 79 additions and 44 deletions.
    44 changes: 0 additions & 44 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,44 +0,0 @@
    Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
    Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image

    INTRODUCTION

    The idea behind it is simple : given two available values a and b, let's say black and white, the value x - what should be grayish - between a and b is simulated by mixing pixels of colors a and b.
    To apply some ordered dithering on an image, we apply the same logic but in 2D by using a bayer matrix.

    By turning the pixel on in a very specified order, the matrix creates the perception of continuous variation of color.

    ■ ■ | □ ■ | □ ■ | □ □ | □ □
    ■ ■ | ■ ■ | ■ □ | ■ □ | □ □


    HOW TO CONSTRUCT A BAYER MATRIX ?

    A bayer matrix is a dither matrix
    Consecutive threshold are located far apart spatially, which gives the perception of smooth/continuous variation.
    The values inside the matrix indicate how likely a pixel will be turned on.
    0 --> most likely to turn on,
    3 --> least likely to turn on

    The whole idea behind ordered dithering is to distribute evenly the
    expected range of gray levels throughout a matrix
    The real trick is to come up with a way to design the matrix so that
    each gray level will produce an even pattern in the output. For example,
    a 50% gray level should come out as a checkerboard pattern. If we adopt
    a recursive definition of the matrix, we get this for free.

    Recursive

    - By hand --> become quickly fastidious but is perfect to understand how the matrix works
    - Using the


    HOW TO APPLY ORDERED DITHERING ON AN IMAGE ?

    Here we are gonna focus on black&white ordered dithering.
    The dither matrix is repeated across the image. The bigger it is, the better it will look.
    For each pixel of the image to modify, look up the member of the matrix covering it.
    Then we use the matrix as a threshold mask : if the value of the pixel is superior to the one given by the matrix, the pixel is lit, if not it becomes black.

    PSEUDO-CODE

    79 changes: 79 additions & 0 deletions ordered_dithering.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    (Written as a way to stop forgetting these things. May be wrong sometimes, as it's just the result of my research here and there. If it helps you, give me a shout!)

    Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
    Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image

    ----------------------------------------------------------------
    INTRODUCTION

    The idea behind it is simple : given two available values a and b, let's say black and white, the value x between a and b - that should be grayish - is simulated by mixing pixels of colors a and b.
    To apply some ordered dithering on an image, we apply the same logic but in 2D by using a bayer matrix.
    By turning the pixel on in a very specified order, the matrix creates the perception of continuous variation of color.

    Here an example of a variaton for a 2x2 matrix :

    ■ ■ | □ ■ | □ ■ | □ □ | □ □
    ■ ■ | ■ ■ | ■ □ | ■ □ | □ □

    ----------------------------------------------------------------
    BAYER MATRIX

    Dither matrix are power-of-2 matrix whose elements can be considered as threshold.
    In these matrices, consecutive threshold values are located far apart spatially, which gives the perception of a progressive variation. The values inside the matrix indicate how likely a pixel will be turned on.
    You can rotate, mirror, transpose these matrices and it won't bring much change to the dithered image.

    ----------------------------------------------------------------
    HOW TO CONSTRUCT A DITHER MATRIX ?

    - By hand --> become quickly fastidious but is perfect to understand how the matrix works

    The simplest - yet useless - dither matrix is the zero matrix of dimension 1x1, this one doesn't apply any transformation. So yeah, useless.

    On the oher hand, the 2x2 matrix is fundamental :
    | 0 2 |
    | 3 1 |
    Given that this matrix contains 4 differents values, we will see only 4 kinds of pattern in the dithered image.
    To reach better image quality, we need to use bigger matrices. Let's compute the 4x4 matrix recursively :

    Step1 4x4:
    | 0 - 2 - |
    | - - - - |
    | 3 - 1 - |
    | - - - - |

    Step2 4x4:
    | 0 - 2 - |
    | - 4 - 6 |
    | 3 - 1 - |
    | - 7 - 5 |

    ...and so on... we just follow the pattern on the 2x2 case.
    The first step to obtain the 8x8 is the following :

    Step1 8x8:
    | 0 - 8 - 2 - 10 - |
    | - - - - - - - - |
    |12 - 4 - 14 - 6 - |
    | - - - - - - - - |
    | 3 - 11 - 1 - 9 - |
    | - - - - - - - - |
    |15 - 7 - 13 - 5 - |

    - Using the recursive formula :

    Now that we done this little prep work, it is easy to deduce the induction relation defining these matrices

    M(2n) = | 4 * M(n) + 0 4 * M(n) + 2 |
    | 4 * M(n) + 3 4 * M(n) + 1 |
    for all n>=1

    ----------------------------------------------------------------
    HOW TO APPLY ORDERED DITHERING ON AN IMAGE ?

    Here we are gonna focus on black&white ordered dithering.
    The dither matrix is tiled across the image. The bigger the matrix is, the better it will look.
    For each pixel of the image to modify, look up the member of the matrix covering it.
    Then we use the matrix as a threshold mask : if the value of the pixel is superior to the one given by the matrix, the pixel is lit, if not it becomes black.

    Done !

  3. @MehdiNS MehdiNS revised this gist Sep 27, 2016. 1 changed file with 43 additions and 1 deletion.
    44 changes: 43 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,44 @@
    Dithering : Given two available values a and b, the missing value x between a et b is simulated by mixing pixels of colors a and b
    Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
    Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image

    INTRODUCTION

    The idea behind it is simple : given two available values a and b, let's say black and white, the value x - what should be grayish - between a and b is simulated by mixing pixels of colors a and b.
    To apply some ordered dithering on an image, we apply the same logic but in 2D by using a bayer matrix.

    By turning the pixel on in a very specified order, the matrix creates the perception of continuous variation of color.

    ■ ■ | □ ■ | □ ■ | □ □ | □ □
    ■ ■ | ■ ■ | ■ □ | ■ □ | □ □


    HOW TO CONSTRUCT A BAYER MATRIX ?

    A bayer matrix is a dither matrix
    Consecutive threshold are located far apart spatially, which gives the perception of smooth/continuous variation.
    The values inside the matrix indicate how likely a pixel will be turned on.
    0 --> most likely to turn on,
    3 --> least likely to turn on

    The whole idea behind ordered dithering is to distribute evenly the
    expected range of gray levels throughout a matrix
    The real trick is to come up with a way to design the matrix so that
    each gray level will produce an even pattern in the output. For example,
    a 50% gray level should come out as a checkerboard pattern. If we adopt
    a recursive definition of the matrix, we get this for free.

    Recursive

    - By hand --> become quickly fastidious but is perfect to understand how the matrix works
    - Using the


    HOW TO APPLY ORDERED DITHERING ON AN IMAGE ?

    Here we are gonna focus on black&white ordered dithering.
    The dither matrix is repeated across the image. The bigger it is, the better it will look.
    For each pixel of the image to modify, look up the member of the matrix covering it.
    Then we use the matrix as a threshold mask : if the value of the pixel is superior to the one given by the matrix, the pixel is lit, if not it becomes black.

    PSEUDO-CODE

  4. @MehdiNS MehdiNS created this gist Sep 27, 2016.
    2 changes: 2 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    Dithering : Given two available values a and b, the missing value x between a et b is simulated by mixing pixels of colors a and b