Skip to content

Commit

Permalink
feat: add color blind correction algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
HokageM committed Apr 2, 2024
1 parent d767e8b commit 5daf271
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 23 deletions.
71 changes: 65 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

<img src="logo/logo.jpeg" width="200">

The ByakuganVisualizer repository hosts a Python tool designed to compare images and highlight their differences.
The ByakuganVisualizer repository hosts a Python tool designed to compare images and highlight their differences.
It simplifies the process of identifying disparities between images, making it ideal for tasks like testing and quality
assurance. Additionally, it offers options for customization, which can be helpful for color-blind users.
assurance.
Moreover, it offers a color filter that can be used to correct images for **color-blind users**.


## Installation
Expand All @@ -16,21 +17,79 @@ pip install byakuganvisualizer
## Usage

```
usage: byakugan_vision [-h] [--version] --diff DIFF [--filter {red,blue,green,yellow}] [--out_dir OUT_DIR]
usage: byakugan_vision [-h] [--version] [--diff DIFF] [--filter {red,blue,green,yellow}] [--images IMAGES] [--deuteranomaly DEUTERANOMALY]
[--protanomaly PROTANOMALY] [--out_dir OUT_DIR]
ByakuganVisualizer: Tool for comparing images and highlighting differences.
ByakuganVisualizer: Tool for correcting the color palett for color blind people and highlighting differences of images.
options:
-h, --help show this help message and exit
--version show program's version number and exit
--diff DIFF String containing a list of tuples "Path_To_Image1a,Path_To_Image2a;Path_To_Image1b,Path_To_Image2b...". Each tuple contains two paths to images to be compared.
--diff DIFF String containing a list of tuples "Path_To_Image1a,Path_To_Image2a;Path_To_Image1b,Path_To_Image2b...". Each tuple
contains two paths to images to be compared.
--filter {red,blue,green,yellow}
Filter type (red, blue, green, yellow)
--images IMAGES List of image names to be manipulated by a filter. E.g.: A,B,C,D
--deuteranomaly DEUTERANOMALY
Expresses your degree of deuteranomaly, which will be used to correct the image. Default is 1.
--protanomaly PROTANOMALY
Expresses your degree of protanomaly, which will be used to correct the image. Default is 1.
--out_dir OUT_DIR Output directory for the difference images
```

## Image Correction for Color Blind People

In the following examples the image is corrected for deuteranomaly and protanomaly.

**Note:** The float values for deuteranomaly and protanomaly are between 0 and 10. The default value is 1.
The used algorithm is based on the following paper: https://arxiv.org/abs/1711.10662.

The image used in the example is from the following source:
https://www.anime2you.de/news/606180/naruto-feiert-20-anime-jubilaeum/

<img src="tests/test_images/naruto.jpg" style="width: 200px">

### Deuteranomaly Correction

```bash
byakugan_vision --images "tests/test_images/naruto.jpg" --deuteranomaly 2
```

<img src="tests/test_images/filtered/Filtered_naruto_deuteranomaly_2_protanomaly_0.jpg" style="width: 200px">

### Protanomaly Correction

```bash
byakugan_vision --images "tests/test_images/naruto.jpg" --protanomaly 2
```

## Example
<img src="tests/test_images/filtered/Filtered_naruto_deuteranomaly_0_protanomaly_2.jpg" style="width: 200px">

### Deuteranomaly and Protanomaly Correction

```bash
byakugan_vision --images "tests/test_images/naruto.jpg" --deuteranomaly 2 --protanomaly 2
```

<img src="tests/test_images/filtered/Filtered_naruto_deuteranomaly_2_protanomaly_2.jpg" style="width: 200px">


```bash
byakugan_vision --images "tests/test_images/naruto.jpg" --deuteranomaly 0.5 --protanomaly 0.5
```

<img src="tests/test_images/filtered/Filtered_naruto_deuteranomaly_0.5_protanomaly_0.5.jpg" style="width: 200px">

### Filter an Image

```bash
byakugan_vision --images "tests/test_images/naruto.jpg" --filter red
```

<img src="tests/test_images/filtered/Filtered_naruto_red.jpg" style="width: 200px">


## Differences between images

The left image used in the example is from the following source:
https://www.anime2you.de/news/606180/naruto-feiert-20-anime-jubilaeum/
Expand Down
29 changes: 20 additions & 9 deletions src/byakuganvisualizer/ImageFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,25 @@ def apply_yellow_filter(image_data):
return yellow_filtered

@staticmethod
def adjust_for_deuteranomaly(image_array):
# Define color transformation matrix for Deuteranomaly
correction_matrix = np.array([[1.0, 0.0, 0.0],
[0.494207, 0.0, 1.24827],
[0.0, 0.0, 1.0]])
def correction_for_colorblindness(image_array, degree_protanomaly, degree_deuteranomaly):
"""
Apply a colorblindness correction to the image data.
:param image_array:
:param degree_protanomaly:
:param degree_deuteranomaly:
:return:
"""
r = image_array[..., 0]
g = image_array[..., 1]
b = image_array[..., 2]

# Apply the color correction matrix
adjusted_array = np.dot(image_array[..., :3], correction_matrix.T)
adjusted_array = np.clip(adjusted_array, 0, 255).astype(np.uint8)
corrected = np.copy(image_array)
r_corrected = (1 - degree_deuteranomaly / 2) * r + (degree_deuteranomaly / 2) * g
g_corrected = (degree_protanomaly / 2) * r + (1 - degree_protanomaly / 2) * g
b_corrected = ((degree_protanomaly / 4) * r + (degree_deuteranomaly / 4) * g +
(1 - (degree_deuteranomaly + degree_protanomaly) / 4) * b)

return adjusted_array
corrected[..., 0] = r_corrected
corrected[..., 1] = g_corrected
corrected[..., 2] = b_corrected
return corrected
28 changes: 20 additions & 8 deletions src/byakuganvisualizer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def parse_args(args):
:obj:`argparse.Namespace`: command line parameters namespace
"""
parser = argparse.ArgumentParser(
description="ByakuganVisualizer: Tool for comparing images and highlighting differences."
description="ByakuganVisualizer: Tool for correcting the color palett for color blind people and highlighting "
"differences of images."
)
parser.add_argument(
"--version",
Expand All @@ -54,23 +55,34 @@ def parse_args(args):
help='String containing a list of tuples "Path_To_Image1a,Path_To_Image2a;Path_To_Image1b,Path_To_Image2b...". '
'Each tuple contains two paths to images to be compared.'
)
parser.add_argument(
'--filter',
choices=['red', 'blue', 'green', 'yellow'],
help='Filter type (red, blue, green, yellow)'
)
parser.add_argument(
'--images',
type=str,
help='List of image names to be manipulated by a filter. E.g.: A,B,C,D'
)
parser.add_argument(
'--filter',
choices=['red', 'blue', 'green', 'yellow', 'deuteranomaly'],
help='Filter type (red, blue, green, yellow, deuteranomaly)'
'--deuteranomaly',
type=float,
default=0,
help='Expresses your degree of deuteranomaly, which will be used to correct the image. Default is 1.'
)
parser.add_argument(
'--protanomaly',
type=float,
default=0,
help='Expresses your degree of protanomaly, which will be used to correct the image. Default is 1.'
)
parser.add_argument(
'--out_dir',
type=str,
default='.',
help='Output directory for the difference images'
)
# TODO: option for just filtering an image
return parser.parse_args(args)


Expand Down Expand Up @@ -130,9 +142,9 @@ def main(args):
if args.filter == 'yellow':
rgb_array = ImageFilter.apply_yellow_filter(rgb_array)
image_name += '_yellow'
if args.filter == 'deuteranomaly':
rgb_array = ImageFilter.adjust_for_deuteranomaly(rgb_array)
image_name += '_deuteranomaly'
if args.protanomaly > 0 or args.deuteranomaly > 0:
rgb_array = ImageFilter.correction_for_colorblindness(rgb_array, args.protanomaly, args.deuteranomaly)
image_name += f'_deuteranomaly_{args.deuteranomaly}_protanomaly_{args.protanomaly}'

filtered_image = Image.fromarray(rgb_array.astype('uint8'))
filtered_image.save(f'{args.out_dir}/Filtered_{image_name}.jpg')
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5daf271

Please sign in to comment.