You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
3.1 KiB
78 lines
3.1 KiB
import numpy as np
|
|
|
|
class MaskedImage:
|
|
DSCALE = 10_000 # valeur arbitraire qui est le nombre max de la function de distance
|
|
base = [1.0, 0.99, 0.96, 0.83, 0.38, 0.11, 0.02, 0.005, 0.0006, 0.0001, 0]
|
|
similarity = np.interp(np.linspace(0, 1, DSCALE), np.linspace(0, 1, len(base)), base)
|
|
|
|
def __init__(self,image=None,mask=None,width=None,height=None):
|
|
if image is not None:
|
|
self.image = image
|
|
self.height, self.width = image.shape[:2]
|
|
self.mask = mask
|
|
return
|
|
self.width = width
|
|
self.height = height
|
|
self.image = np.zeros((self.height, self.width, 3), dtype=np.uint8)
|
|
self.mask = np.zeros((self.height, self.width), dtype=bool)
|
|
|
|
def containsMask(self,x,y,patchSize):
|
|
for dy in range(-patchSize,patchSize):
|
|
if (y+dy < 0 or self.height <= y+dy):
|
|
continue
|
|
for dx in range(-patchSize,patchSize):
|
|
if (x+dx < 0 or self.width <= x+dx):
|
|
continue
|
|
if self.mask[y+dy,x+dx]:
|
|
return True
|
|
return False
|
|
|
|
def copy(self):
|
|
return MaskedImage(image=self.image.copy(), mask=self.mask.copy())
|
|
|
|
def downsample(self):
|
|
newW, newH = self.width // 2, self.height // 2
|
|
kernel = np.array([1, 5, 10, 10, 5, 1])
|
|
newimage = np.zeros((newH, newW, 3), dtype=np.uint8)
|
|
newmask = np.zeros((newH, newW), dtype=bool)
|
|
|
|
for y in range(0, self.height - 1, 2):
|
|
for x in range(0, self.width - 1, 2):
|
|
r, g, b, ksum, m = 0, 0, 0, 0, 0
|
|
|
|
for dy in range(-2, 4):
|
|
yk = y + dy
|
|
if (yk<0 or yk >= self.height):
|
|
continue
|
|
ky = kernel[2 + dy]
|
|
|
|
for dx in range(-2, 4):
|
|
xk = x + dx
|
|
if (xk<0 or xk >= self.width):
|
|
continue
|
|
if self.mask[yk, xk]:
|
|
continue
|
|
k = kernel[2 + dx] * ky
|
|
r += k * self.image[yk, xk, 0]
|
|
g += k * self.image[yk, xk, 1]
|
|
b += k * self.image[yk, xk, 2]
|
|
ksum += k
|
|
m += 1
|
|
|
|
if ksum > 0:
|
|
newimage[y // 2, x // 2] = (r // ksum, g // ksum, b // ksum)
|
|
newmask[y // 2, x // 2] = False
|
|
else:
|
|
newmask[y // 2, x // 2] = True
|
|
|
|
return MaskedImage(image=newimage, mask=newmask)
|
|
|
|
def upscale(self, newH, newW):
|
|
y_indices = np.floor(np.linspace(0, self.height-1, newH)).astype(int)
|
|
x_indices = np.floor(np.linspace(0, self.width-1, newW)).astype(int)
|
|
y_coords, x_coords = np.meshgrid(y_indices, x_indices, indexing='ij')
|
|
newImage = MaskedImage(width=newW, height=newH)
|
|
newImage.image = self.image[y_coords, x_coords]
|
|
newImage.mask = self.mask[y_coords, x_coords]
|
|
return newImage
|