try different scoring approach and attempt an image construction based on the avg colors of the image

This commit is contained in:
Rusty Striker 2024-01-29 19:50:02 +02:00
parent 51a0efee01
commit b1b77c0f24
Signed by: RustyStriker
GPG Key ID: 9DBDBC7C48FC3C31
1 changed files with 21 additions and 3 deletions

View File

@ -112,6 +112,21 @@ def calc_score(img, mask, avg_color, var_mag):
score += mag * m / var_mag
return score
def generate_subimg(img, c_avg, nc_avg):
sx, sy, sw = img.shape
if sw != c_avg.shape[0]:
print('Image depth doesnt match color!')
# can be bool actually but float32 because why not
res = np.zeros([sx, sy], dtype=np.float32)
for x in range(sx):
for y in range(sy):
da = img[x, y] - c_avg
mag_a = da.dot(da)
dn = img[x, y] - nc_avg
mag_n = dn.dot(dn)
res[x, y] = 1.0 if mag_a < mag_n else 0.0
return res
def score_font(char_img, char, font_name):
'''
Takes a char_img, the wanted character and a font_name/path
@ -123,8 +138,11 @@ def score_font(char_img, char, font_name):
# resize font_img to match char_img dimensions
dim = [char_img.shape[1], char_img.shape[0]]
mask = cv.resize(font_img, dim, interpolation=cv.INTER_LINEAR)
rv = np.ones(mask.shape, dtype=np.float32) - mask
# get average color
ac = get_avg_color(char_img, mask)
var = get_color_variance(char_img, mask, ac)
var = np.sqrt(var.dot(var))
return calc_score(char_img, mask, ac, var)
rac = get_avg_color(char_img, rv)
diff = ac - rac
mag = diff.dot(diff)
return mag