Skip to content

Added Burkes dithering algorithm. #1916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix burkes algorithm
  • Loading branch information
l3str4nge committed Apr 29, 2020
commit a157b4770952bd3a5c18304f57eac19465301367
21 changes: 11 additions & 10 deletions digital_image_processing/dithering/burkes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Implementation Burke's algorithm (dithering)
"""
from cv2 import imread, imshow, waitKey, destroyAllWindows, IMREAD_GRAYSCALE
from cv2 import imread, imshow, waitKey, destroyAllWindows
import numpy as np


Expand All @@ -24,25 +24,26 @@ def __init__(self, input_img, threshold: int):
self.threshold = threshold
self.width, self.height = self.input_img.shape[1], self.input_img.shape[0]

# error table size (+2 columns and +1 row) greater than input image because of lack of if statements
# error table size (+4 columns and +1 row) greater than input image because of lack of if statements
self.error_table = [
[0 for _ in range(self.width + 4)] for __ in range(self.height + 1)
[0 for _ in range(self.height + 4)] for __ in range(self.width + 1)
]
self.output_img = np.ones((self.height, self.width, 3), np.uint8) * 255
self.output_img = np.ones((self.width, self.height, 3), np.uint8) * 255

@staticmethod
def get_greyscale(red: int, green: int, blue: int):
def get_greyscale(blue: int, green: int, red: int):
return 0.2126 * red + 0.587 * green + 0.114 * blue
Copy link
Member

@cclauss cclauss Apr 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If line 38 is in blue, green, red order then line 43 should also be in blue, green, red order.

In most instances, these numbers are stored in red, green, blue (RGB) order.


def process(self):
for y in range(self.height):
for x in range(self.width):
if self.threshold > self.input_img[y][x] + self.error_table[y][x]:
greyscale = int(self.get_greyscale(*self.input_img[y][x]))
if self.threshold > greyscale + self.error_table[y][x]:
self.output_img[y][x] = (0, 0, 0)
current_error = self.input_img[y][x] + self.error_table[y][x]
current_error = greyscale + self.error_table[x][y]
else:
self.output_img[y][x] = (255, 255, 255)
current_error = self.input_img[y][x] + self.error_table[y][x] - 255
current_error = greyscale + self.error_table[x][y] - 255

"""
Burkes error propagation (`*` is current pixel):
Expand Down Expand Up @@ -76,8 +77,8 @@ def process(self):
if __name__ == "__main__":
# create Burke's instances with original images in greyscale
burkes_instances = [
Burkes(imread("image_data/lena.jpg", IMREAD_GRAYSCALE), threshold)
for threshold in (80, 100, 120, 200, 215)
Burkes(imread("image_data/lena.jpg", 1), threshold)
for threshold in (1, 126, 180, 200)
]

for burkes in burkes_instances:
Expand Down
3 changes: 1 addition & 2 deletions digital_image_processing/test_digital_image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def test_sepia():

def test_burkes():
burkes = bs.Burkes(
imread("digital_image_processing/image_data/lena_small.jpg", IMREAD_GRAYSCALE),
120,
imread("digital_image_processing/image_data/lena_small.jpg", 1), 120,
)
burkes.process()
assert burkes.output_img.any()