implement avg color function

This commit is contained in:
Rusty Striker 2024-01-19 15:51:35 +02:00
parent 5670b821b8
commit d5a4b46c7a
Signed by: RustyStriker
GPG Key ID: 9DBDBC7C48FC3C31
1 changed files with 50 additions and 0 deletions

View File

@ -2,8 +2,22 @@ import cv2 as cv
import numpy as np
import h5py as h5
db = None
def init(path):
''' initializes the database, must be called before any use '''
global db
db = h5.File(path, 'r')
def needs_init():
''' checks if the database has been initialized '''
if db is None:
print('db is none, please use init(path_to_db) first!')
return db is None
# Extract letter from a bounding box
def extract_bb(img, bb):
''' extracts a bounding box/letter from the given image '''
# Get the bounding box
rect = cv.minAreaRect(bb.astype(np.float32).transpose())
# will be useful later, map center and size to ints
@ -16,3 +30,39 @@ def extract_bb(img, bb):
cropped = cv.getRectSubPix(rot_img, size, center)
return cropped
def get_img(index):
''' gets image from database '''
if needs_init():
return None
names = list(db['data'].keys())
im = names[index]
return db['data'][im][:]
def get_attrs(index):
''' gets attribute dict from the database '''
if needs_init():
return None
names = list(db['data'].keys())
im = names[index]
return db['data'][im].attrs
def get_avg_color(img, mask):
'''
gets avg color from an image that is underneath a mask,
img and mask needs to be of same size(in x,y) but img can
have any third dimension size it want(usually 3 for rgb or 1 for grayscale)
'''
sx, sy, sw = img.shape
mx, my = mask.shape
if sx != mx or sy != my:
print('Image and mask shape doesnt match!')
return None
avg = np.array(sw, dtype=np.float32)
count = 0.0
for x in range(sx):
for y in range(sy):
m = mas[x, y]
avg += img[x, y] * m
count += m
avg /= count
return avg