Skip to content

Commit 95d1417

Browse files
committed
Fixed and simplified diagonal_split
1 parent 993cb2d commit 95d1417

File tree

1 file changed

+32
-67
lines changed

1 file changed

+32
-67
lines changed

frc_utils.py

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,38 @@
1414
import itertools
1515
import sys
1616

17-
def diagonal_split(x):
18-
19-
''' pre-processing steps interms of
20-
cropping to enable the diagonal
21-
splitting of the input image
22-
'''
23-
24-
h, w = x.shape
25-
cp_x = x
26-
''' cropping the rows '''
27-
if (np.mod(h, 4)==1):
28-
cp_x = cp_x[:-1]
29-
elif(np.mod(h, 4)==2):
30-
cp_x = cp_x[1:-1]
31-
elif(np.mod(h, 4)==3):
32-
cp_x = cp_x[1:-2]
33-
34-
''' cropping the columns'''
35-
if (np.mod(w, 4)==1):
36-
cp_x = cp_x[:, :-1]
37-
elif(np.mod(w, 4)==2):
38-
cp_x = cp_x[:,1:-1]
39-
elif(np.mod(w, 4)==3):
40-
cp_x = cp_x[:, 1:-2]
41-
42-
43-
x = cp_x
44-
h, w = x.shape
45-
if((np.mod(h, 4)!=0) or (np.mod(w, 4)!=0)):
46-
print('[!] diagonal splitting not possible due to cropping issue')
47-
print('[!] re-check the cropping portion')
48-
end()
49-
50-
row_indices = np.arange(0, h)
51-
col_indices = np.arange(0, w)
52-
53-
row_split_u = row_indices[::2]
54-
row_split_d = np.asanyarray(list(set(row_indices)-set(row_split_u)))
55-
56-
col_split_l = col_indices[::2]
57-
col_split_r = np.asanyarray(list(set(col_indices)-set(col_split_l)))
58-
59-
''' ordered pair of pre-processing
60-
of the diagonal elements
61-
and sub-sequent splits of the image
62-
'''
63-
op1 = list(itertools.product(row_split_u, col_split_l))
64-
ind = [np.asanyarray([fo for fo, _ in op1]), np.asanyarray([so for _, so in op1])]
65-
s_a1 = x[ind]
66-
s_a1 = s_a1.reshape((len(row_split_u), len(col_split_l)))
67-
68-
op2 = list(itertools.product(row_split_d, col_split_r))
69-
ind = [np.asanyarray([fo for fo, _ in op2]), np.asanyarray([so for _, so in op2])]
70-
s_a2 = x[ind]
71-
s_a2 = s_a2.reshape((len(row_split_d), len(col_split_r)))
72-
73-
op3 = list(itertools.product(row_split_d, col_split_l))
74-
ind = [np.asanyarray([fo for fo, _ in op3]), np.asanyarray([so for _, so in op3])]
75-
s_b1 = x[ind]
76-
s_b1 = s_b1.reshape((len(row_split_d), len(col_split_l)))
77-
78-
op4 = list(itertools.product(row_split_u, col_split_r))
79-
ind = [np.asanyarray([fo for fo, _ in op4]), np.asanyarray([so for _, so in op4])]
80-
s_b2 = x[ind]
81-
s_b2 = s_b2.reshape((len(row_split_u), len(col_split_r)))
82-
83-
return(s_a1, s_a2, s_b1, s_b2)
17+
def diagonal_split(img):
18+
'''
19+
This function takes an input image and splits it diagonally into four sub-regions.
20+
The input image must have dimensions that are divisible by 4.
21+
'''
22+
# Get the shape of the input image
23+
h, w = img.shape
24+
# Check that the image has dimensions divisible by 4
25+
if (h % 4 != 0) or (w % 4 != 0):
26+
raise ValueError('Input image must have dimensions divisible by 4')
27+
# Crop the image to make sure the dimensions are divisible by 4
28+
img = img[:h//4*4, :w//4*4]
29+
h, w = img.shape
30+
# Create indices for the rows and columns
31+
row_indices = np.arange(h)
32+
col_indices = np.arange(w)
33+
34+
# Split the indices into two groups, one for each diagonal split
35+
row_split_u = row_indices[::2]
36+
row_split_d = row_indices[1::2]
37+
38+
col_split_l = col_indices[::2]
39+
col_split_r = col_indices[1::2]
40+
41+
# Split the image into four sub-regions using advanced indexing
42+
sub_a1 = img[np.ix_(row_split_u, col_split_l)]
43+
sub_a2 = img[np.ix_(row_split_d, col_split_r)]
44+
sub_b1 = img[np.ix_(row_split_d, col_split_l)]
45+
sub_b2 = img[np.ix_(row_split_u, col_split_r)]
46+
47+
# Return the four sub-regions
48+
return sub_a1, sub_a2, sub_b1, sub_b2
8449

8550
def get_frc_img(img, frc_img_lx, center=None):
8651
''' Returns a cropped image version of input image "img"

0 commit comments

Comments
 (0)