Define a function named 'get_loc' taking in 3 parameters - self, bw_map, rg and fbw

def get_loc(self, bw_map, rg=8, fbw=0):

# Add 'self.loc_off // 3' to the value of 'rg'
rg += self.loc_off // 3

# Set 'rge' to '88 + rg'
rge = 88 + rg

# Create a numpy array of zeros with dimensions 'rge * 2' x 'rge * 2' and of the same data type as 'self.big_map'
loc_big = np.zeros((rge * 2, rge * 2), dtype=self.big_map.dtype)

# Set the variable 'tpl' to a tuple containing the values of 'self.now_loc[0]' and 'self.now_loc[1]'
tpl = (self.now_loc[0], self.now_loc[1])

# Set the variables 'x0' and 'y0' to the maximum value between 0 and 'rge - tpl[0]' and 'rge - tpl[1]' respectively
x0, y0 = max(rge - tpl[0], 0), max(rge - tpl[1], 0)

# Set the variables 'x1' and 'y1' to the maximum value between 0 and 'tpl[0] + rge - self.big_map.shape[0]' and 'tpl[1] + rge - self.big_map.shape[1]' respectively
x1, y1 = max(tpl[0] + rge - self.big_map.shape[0], 0), max(tpl[1] + rge - self.big_map.shape[1], 0)

# Set the values of 'loc_big' to the values of 'self.big_map' within the range specified by the variables 'x0', 'y0', 'x1' and 'y1'
loc_big[x0:rge * 2 - x1, y0:rge * 2 - y1] = self.big_map[tpl[0] - rge + x0:tpl[0] + rge - x1, tpl[1] - rge + y0:tpl[1] + rge - y1]

# Set 'max_val' to -1 and 'max_loc' to 0
max_val, max_loc = -1, 0

# Set 'bo_1' to a boolean array where all values that are equal to 255 in 'bw_map' are set to True and the rest are set to False
bo_1 = (bw_map == 255)

# Set 'tt' to 4
tt = 4

# Check if 'self.find' is True and 'fbw' is equal to 0
if self.find and fbw == 0:
    
    # Resize 'bw_map' to a size of (176 + tt * 2, 176 + tt * 2) and set the result to 'tbw'
    tbw = cv.resize(bw_map, (176 + tt * 2, 176 + tt * 2))
    
    # Set all values in 'tbw' that are greater than 150 to 255 and the rest to 0
    tbw[tbw > 150] = 255
    tbw[tbw <= 150] = 0
    
    # Set 'tbw' to the values of 'tbw' within the range specified by 'tt', '176 + tt', 'tt' and '176 + tt'
    tbw = tbw[tt:176 + tt, tt:176 + tt]
    
    # Write 'tbw' to a JPEG file named 'imgs/tbw.jpg'
    cv.imwrite('imgs/tbw.jpg', tbw)
    
    # Set 'bo_2' to a boolean array where all values that are equal to 255 in 'tbw' are set to True and the rest are set to False
    bo_2 = (tbw == 255)
    
# Set 'bo_3' to a boolean array where all values in 'loc_big' that are greater than or equal to 50 are set to True and the rest are set to False
bo_3 = (loc_big >= 50)

# Loop through each value 'i' in the range 'rge * 2 - 176'
for i in range(rge * 2 - 176):
    
    # Loop through each value 'j' in the range 'rge * 2 - 176'
    for j in range(rge * 2 - 176):
        
        # Check if the square of the difference between 'i - rge + 88' and 'j - rge + 88' is greater than 'rg ** 2'
        if (i - rge + 88) ** 2 + (j - rge + 88) ** 2 > rg ** 2:
            
            # Continue to the next iteration of the loop
            continue
        
        # Set 'p' to the number of non-zero values in the boolean array obtained by performing an 'AND' operation between 'bo_3[i:i + 176, j:j + 176]' and 'bo_1'
        p = np.count_nonzero(bo_3[i:i + 176, j:j + 176] & bo_1)
        
        # Check if 'p' is greater than 'max_val'
        if p > max_val:
            
            # Set 'max_val' to 'p' and 'max_loc' to a tuple containing the values of 'i' and 'j'
            max_val = p
            max_loc = (i, j)
            
        # Check if 'self.find' is True and 'fbw' is equal to 0
        if self.find and fbw == 0:
            
            # Set 'p' to the number of non-zero values in the boolean array obtained by performing an 'AND' operation between 'bo_3[i:i + 176, j:j + 176]' and 'bo_2'
            p = np.count_nonzero(bo_3[i:i + 176, j:j + 176] & bo_2)
            
            # Check if 'p' is greater than 'max_val'
            if p > max_val:
                
                # Set 'max_val' to 'p' and 'max_loc' to a tuple containing the values of 'i' and 'j'
                max_val = p
                max_loc = (i, j)

# Write 'bw_map' to a JPEG file named 'imgs/bwmap.jpg'
cv.imwrite('imgs/bwmap.jpg', bw_map)

# Create a deep copy of the values in 'loc_big' within the range specified by the values of 'max_loc[0]', 'max_loc[0] + 176', 'max_loc[1]' and 'max_loc[1] + 176' and set the result to 'tp'
tp = deepcopy(loc_big[max_loc[0]:max_loc[0] + 176, max_loc[1]:max_loc[1] + 176])

# Set the values in 'tp' within the range specified by '86', '90', '86' and '90' to 100
tp[86:90, 86:90] = 100

# Write 'tp' to a JPEG file named 'imgs/maxloc.jpg'
cv.imwrite('imgs/maxloc.jpg', tp)

# Set 'lst' to the value of 'self.now_loc'
lst = self.now_loc

# Check if 'max_val' is not equal to 0
if max_val != 0:
    
    # Set 'self.now_loc' to a tuple containing the values of 'max_loc[0] + 88 - rge + self.now_loc[0]' and 'max_loc[1] + 88 - rge + self.now_loc[1]'
    self.now_loc = (max_loc[0] + 88 - rge + self.now_loc[0], max_loc[1] + 88 - rge + self.now_loc[1])

# Check if 'lst' is equal to 'self.now_loc'
if lst == self.now_loc:
    
    # Set 'self.loc_off' to the minimum value between 'self.loc_off + 1' and '14'
    self.loc_off = min(self.loc_off + 1, 14)

# Otherwise
else:
    
    # Set 'self.loc_off' to 0
    self.loc_off = 0

# Set 'self.real_loc' to a tuple containing the values of 'self.now_loc[0]' and 'self.now_loc[1]'
self.real_loc = (self.now_loc[0], self.now_loc[1])

# Print the values of 'self.real_loc' and 'max_val'
# print(self.real_loc,max_val

原文地址: https://www.cveoy.top/t/topic/olZJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录