Progress on different distance algorithms (current broken due to sketchy python)

This commit is contained in:
Matt McWilliams 2023-10-26 23:37:27 -04:00
parent 2a6fc4147b
commit 3649e53850
3 changed files with 57 additions and 3 deletions

View File

@ -6,3 +6,19 @@ Includes Python application for prototyping and C++ application for
building a self-contained binary.
```bash
python pallete.py ~/Desktop/Ohuhu_50_pallete_card_printed_scaled.png
```
```bash
python separator.py ~/Desktop/look.png 6 ./palletes/printed_pallete.json ./
```
### TODO
* Fix (?) BGR and RGB closest color match
* Order colors before matching
* Match brightest color to white and skip
* Re-assemble image with patched colors
* Scripts for running `stipple_gen` on each layer

View File

@ -1,5 +1,6 @@
import cv2
import numpy as np
from math import sqrt
def convert_color (color, color_space_a, color_space_b) :
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
@ -62,6 +63,34 @@ def closest_color (colors, color):
smallest_distance = colors[index_of_smallest]
return smallest_distance[0]
# Works for RGB, BGR, LAB and HSV(?)
def closest_color_pythagorean (colors, color) :
mDist = float('inf')
mIdx = -1
for idx, comp in enumerate(colors) :
dist = pythagorean_distance(comp[0], comp[1], comp[2], color[0], color[1], color[2])
if dist < mDist :
mDist = dist
mIds = idx
return color[mIdx], mDist
def closest_color_weighted_pythagorean (colors, color, space) :
mDist = float('inf')
mIdx = -1
for idx, comp in enumerate(colors) :
if space == 'BGR' :
dist = weighed_pythagorean_distance(comp[2], comp[1], comp[0], color[2], color[1], color[1])
elif space == 'RGB' :
dist = weighed_pythagorean_distance(comp[0], comp[1], comp[2], color[0], color[1], color[2])
else :
raise Exception(f'closest_color_weighted_pythagorean does not support color space {space}')
break
if dist < mDist :
mDist = dist
mIds = idx
return color[mIdx], mDist
def create_colored_image (width, height, bgr_color):
image = np.zeros((height, width, 3), np.uint8)
image[:] = bgr_color
@ -78,4 +107,13 @@ def list_match (a, b) :
for i in range(len(a)) :
if a[i] != b[i] :
return False
return True
return True
def pythagorean_distance (r1, g1, b1, r2, g2, b2) :
return sqrt(pow(r1-r2, 2) + pow(g1-g2, 2) + pow(b1-b2, 2))
def weighted_pythagorean_distance (r1, g1, b1, r2, g2, b2) :
R = 0.30
G = 0.59
B = 0.11
return sqrt(pow((r1-r2) * R, 2) + pow((g1-g2) * G, 2) + pow((b1-b2) * B, 2))

View File

@ -1,7 +1,7 @@
import cv2
import numpy as np
from pallete_schema import PalleteSchema
from common import convert_color, closest_color, create_colored_image, remove_from_list
from common import convert_color, closest_color, create_colored_image, remove_from_list, closest_color_pythagorean, closest_color_weighted_pythagorean
class ComparisonComparison:
def __init__ (self) :
@ -39,7 +39,7 @@ class ComparisonComparison:
for cc in comp_colors :
cccompare = convert_color(cc, 'RGB', space)
print(cccompare)
closest = closest_color(colors, cccompare)
closest = closest_color_pythagorean(colors, cccompare)
print(closest)
colors = remove_from_list(colors, closest)