R2454 commited on
Commit
7cb6304
·
1 Parent(s): c4bac2c

Added Multiple things

Browse files
Files changed (1) hide show
  1. models/lancros_interpolation.py +54 -54
models/lancros_interpolation.py CHANGED
@@ -9,68 +9,68 @@ def upsample_lancros(image_x,scale=4):
9
  pred = cv2.resize(image_x, (new_w, new_h), interpolation=cv2.INTER_LANCZOS4)
10
  return pred
11
 
12
- def lanczos_kernel(x, a):
13
- """Lanczos kernel function."""
14
- if x == 0:
15
- return 1
16
- elif -a < x < a:
17
- return a * np.sinc(x) * np.sinc(x / a)
18
- else:
19
- return 0
20
 
21
- def upsample_lancros_2(image, scale=4, a=3):
22
- """
23
- Upsample an image using Lanczos interpolation.
24
 
25
- Parameters:
26
- image: numpy array (grayscale or RGB)
27
- scale: scaling factor (default 4x)
28
- a: size of Lanczos window (default 3)
29
 
30
- Returns:
31
- Upsampled image as numpy array.
32
- """
33
- if image.ndim == 2: # Grayscale
34
- h, w = image.shape
35
- channels = 1
36
- else: # RGB
37
- h, w, channels = image.shape
38
 
39
- new_h, new_w = int(h * scale), int(w * scale)
40
- output = np.zeros((new_h, new_w, channels)) if channels > 1 else np.zeros((new_h, new_w))
41
 
42
- for y_new in range(new_h):
43
- for x_new in range(new_w):
44
- print(y_new,x_new)
45
- # Map new pixel to original image space
46
- x_orig = x_new / scale
47
- y_orig = y_new / scale
48
 
49
- x0 = int(np.floor(x_orig))
50
- y0 = int(np.floor(y_orig))
51
 
52
- # Accumulators
53
- pixel = np.zeros(channels) if channels > 1 else 0.0
54
- norm = 0.0
55
 
56
- for j in range(y0 - a + 1, y0 + a + 1):
57
- for i in range(x0 - a + 1, x0 + a + 1):
58
- if 0 <= i < w and 0 <= j < h:
59
- wx = lanczos_kernel(x_orig - i, a)
60
- wy = lanczos_kernel(y_orig - j, a)
61
- weight = wx * wy
62
- if channels > 1:
63
- pixel += image[j, i] * weight
64
- else:
65
- pixel += image[j, i] * weight
66
- norm += weight
67
 
68
- if norm > 0:
69
- output[y_new, x_new] = pixel / norm
70
 
71
- if channels == 1:
72
- output = output.astype(image.dtype)
73
- else:
74
- output = output.clip(0, 255).astype(image.dtype)
75
 
76
- return output
 
9
  pred = cv2.resize(image_x, (new_w, new_h), interpolation=cv2.INTER_LANCZOS4)
10
  return pred
11
 
12
+ # def lanczos_kernel(x, a):
13
+ # """Lanczos kernel function."""
14
+ # if x == 0:
15
+ # return 1
16
+ # elif -a < x < a:
17
+ # return a * np.sinc(x) * np.sinc(x / a)
18
+ # else:
19
+ # return 0
20
 
21
+ # def upsample_lancros_2(image, scale=4, a=3):
22
+ # """
23
+ # Upsample an image using Lanczos interpolation.
24
 
25
+ # Parameters:
26
+ # image: numpy array (grayscale or RGB)
27
+ # scale: scaling factor (default 4x)
28
+ # a: size of Lanczos window (default 3)
29
 
30
+ # Returns:
31
+ # Upsampled image as numpy array.
32
+ # """
33
+ # if image.ndim == 2: # Grayscale
34
+ # h, w = image.shape
35
+ # channels = 1
36
+ # else: # RGB
37
+ # h, w, channels = image.shape
38
 
39
+ # new_h, new_w = int(h * scale), int(w * scale)
40
+ # output = np.zeros((new_h, new_w, channels)) if channels > 1 else np.zeros((new_h, new_w))
41
 
42
+ # for y_new in range(new_h):
43
+ # for x_new in range(new_w):
44
+ # print(y_new,x_new)
45
+ # # Map new pixel to original image space
46
+ # x_orig = x_new / scale
47
+ # y_orig = y_new / scale
48
 
49
+ # x0 = int(np.floor(x_orig))
50
+ # y0 = int(np.floor(y_orig))
51
 
52
+ # # Accumulators
53
+ # pixel = np.zeros(channels) if channels > 1 else 0.0
54
+ # norm = 0.0
55
 
56
+ # for j in range(y0 - a + 1, y0 + a + 1):
57
+ # for i in range(x0 - a + 1, x0 + a + 1):
58
+ # if 0 <= i < w and 0 <= j < h:
59
+ # wx = lanczos_kernel(x_orig - i, a)
60
+ # wy = lanczos_kernel(y_orig - j, a)
61
+ # weight = wx * wy
62
+ # if channels > 1:
63
+ # pixel += image[j, i] * weight
64
+ # else:
65
+ # pixel += image[j, i] * weight
66
+ # norm += weight
67
 
68
+ # if norm > 0:
69
+ # output[y_new, x_new] = pixel / norm
70
 
71
+ # if channels == 1:
72
+ # output = output.astype(image.dtype)
73
+ # else:
74
+ # output = output.clip(0, 255).astype(image.dtype)
75
 
76
+ # return output