<jupyter_start><jupyter_text>$$P_{(dx, dy)}=\|P(i, j)\|_{256 \times 256}$$ \begin{equation}P(i, j)=\sum_{x=1}^{N} \sum_{y=1}^{M}\left\{\begin{array}{ll}1, & \text { якщо } I(x, y)=i \text { та } I(x+d x, y + d y)=j ; \\0, & \text { в інших випадках }\end{array}\right.\end{equation}<jupyter_code>def p(i,j, matr, d):
    n_rows, n_cols = matr.shape
    dx, dy = d
    
    res = 0
    for x in range(n_rows):
        for y in range(n_cols):
            # check for being in image's bounds
            props1 = [x + dx  < n_rows, y + dy < n_cols]
            
            if all(props1):
                if matr[x][y] == i and matr[x + dx][y + dy] == j:
                    res += 1
    
                
    return res

def coincidence_matr(image, d):
    """
    d -- (dx, dy) vector
    image -- N x M matrix (built by image)
    """
    
    res_matr = np.zeros((256, 256))
    vmin, vmax = image.min(), image.max()
    
    # it actually makes sense to look only at
    # rectangle (vmnin x vmax) and make the least
    # equals zero
    for i in range(vmin, vmax):
        for j in range(vmin, vmax):
            res_matr[i, j] = p(i, j, image, d)
            
            
    return res_matr<jupyter_output><empty_output><jupyter_text>\begin{aligned}\hat{P}=\frac{1}{8}\left(P_{(0, dy)}+P_{(0,-dy)}\right.&+P_{(-dx, dy)}+P_{(dx,-dy)}+\\&\left.+P_{(-dx, 0)}+P_{(dx, 0)}+P_{(-dx,-dy)}+P_{(dx, dy)}\right)\end{aligned}<jupyter_code>def mean_coincidence_matr(image, d):
    """
    image is given matrix
    d is (dx, dy) 2-dim vector
    """
    dx, dy = d
    
    D = [(0, dy), (0, -dy), (-dx, dy), (dx, -dy), (-dx,0),
    (dx, 0), (-dx, -dy), (dx, dy)]
    
    res_matr = np.zeros((256, 256))
    if d != (0, 0):
        for d_i in D:
            res_matr += coincidence_matr(ex1, d_i)
        
        return 1 / 8 * (res_matr)
    else:
        return coincidence_matr(ex1, d)
matrices = []

for i in range(6):
    matrices.append([])
    with open(fr"D:\Documents\Курсова файли\needed_files\404\1_{i+1}_Gray.txt") as f:
        lines = f.readlines()
        for line in lines:
            t = [float(x) for x in line.split()]
            matrices[i].append(t)
ex1 = np.array(matrices[0][:-1], dtype=int)
plt.imshow(ex1, cmap='gray') 
plt.imshow(ex1, cmap='gray_r')  
%%time
d = (0, 1)

ex1_res_ = coincidence_matr(ex1, d)
plt.figure(dpi=100)
plt.imshow(ex1_res_, cmap='gray_r')
%%time
ex1_res_mean = mean_coincidence_matr(ex1, d)
plt.figure(dpi=100)
plt.imshow(ex1_res_mean, cmap='gray_r')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18893280632411066, 'fraction_numerical': 0.026877470355731226, 'mean_word_length': 2.3927613941018766, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '166181', 'license': 'MIT', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1029, 'n_words': 235, 'path': 'to_send1.ipynb', 'repo_name': 'qqullar/coursework', 'size': '2530'}