text_plain_output_1.png
import cv2

import glob

import numpy as np

def load_cv2_images(folder):
    imgs, masks, img_ids = ({}, {}, {})
    for i in range(47):
        imgs[i + 1] = []
        masks[i + 1] = []
        img_ids[i + 1] = []
    paths = glob.glob(folder + '*.tif')
    paths = [p for p in paths if 'mask' not in p]
    for p in paths:
        index = int(p.split('/')[3].split('_')[0])
        try:
            imgs[index].append(cv2.imread(p, 0))
            masks[index].append(cv2.imread(p[:-4] + '_mask.tif', 0))
            img_ids[index].append(p.split('/')[3])
        except:
            pass
    for i in range(47):
        imgs[i + 1] = np.array(imgs[i + 1])
        masks[i + 1] = np.array(masks[i + 1])
    return (imgs, masks, img_ids)
imgs, masks, img_ids = load_cv2_images('../input/train/')

imgs.keys()

def find_pairs(compare_img, compare_mask, compare_id, imgs, masks, img_ids, compare_index, matches):
    threshold = 23000000
    for i, (img, mask, img_id) in enumerate(zip(imgs, masks, img_ids)):
        if np.abs(compare_img - img).sum() < threshold and i != compare_index and ((compare_mask.sum() == 0) != (mask.sum() == 0)):
            matches.append((compare_img, compare_mask, compare_id, img, mask, img_id))
    return matches
matches = []
for j in range(47):
    for i, (img, mask, img_id) in enumerate(zip(imgs[j + 1], masks[j + 1], img_ids[j + 1])):
        matches = find_pairs(img, mask, img_id, imgs[j + 1], masks[j + 1], img_ids[j + 1], i, matches)
len(matches)