Beispiel #1
0
def tpm_go_deeper(A_coms, B_coms, cur_A_com_ids, cur_A_filled, cur_B_com_ids,
                  cur_B_filled):
    A_result = []
    B_result = []

    for ii, com_id in enumerate(cur_A_com_ids):
        if (utils.fill_holes(A_coms[com_id]) == cur_A_filled).all():
            break
    A_result.append(cur_A_com_ids.pop(ii))

    for ii, com_id in enumerate(cur_B_com_ids):
        if (utils.fill_holes(B_coms[com_id]) == cur_B_filled).all():
            break
    B_result.append(cur_B_com_ids.pop(ii))

    cur_A_sub = utils.superimpose([A_coms[com_id] for com_id in cur_A_com_ids])
    cur_B_sub = utils.superimpose([B_coms[com_id] for com_id in cur_B_com_ids])
    cur_A_sub_result, cur_B_sub_result = tpm(A_coms, B_coms, cur_A_sub,
                                             cur_B_sub, cur_A_com_ids,
                                             cur_B_com_ids)
    if 0 == len(cur_A_sub_result) or 0 == len(cur_A_sub_result):
        return [], []
    else:
        A_result = A_result + cur_A_sub_result
        B_result = B_result + cur_B_sub_result

    return A_result, B_result
Beispiel #2
0
def evaluate_shape_texture_transfer(u1, u2, u3):
    u1_filled = utils.fill_holes(u1)
    u2_filled = utils.fill_holes(u2)
    u3_filled = utils.fill_holes(u3)

    # old_u1_u3_shape_index = jaccard.jaccard_coef(u1_filled, u3_filled)[0]
    #
    # u1_u3_shape_index = soft_jaccard.soft_jaccard(u1_filled, u3_filled)[0]

    u1_texture_index = np.logical_and(
        u1_filled, np.logical_not(u1)).sum() / u1_filled.sum()
    u2_texture_index = np.logical_and(
        u2_filled, np.logical_not(u2)).sum() / u2_filled.sum()
    u3_texture_index = np.logical_and(
        u3_filled, np.logical_not(u3)).sum() / u3_filled.sum()
    u1_u2_texture_index = u1_texture_index - u2_texture_index
    u1_u3_texture_index = u1_texture_index - u3_texture_index

    # _, u1_to_u3_x, u1_to_u3_y = jaccard.jaccard_coef(u1, u3)
    # u1_texture_index, u3_texture_index = utils.texture_index(u1, u3, u1_filled, u3_filled, u1_to_u3_x, u1_to_u3_y)
    # texture_score = 1 - abs(u1_texture_index - u3_texture_index)

    # mat_score = (1 - abs(u1_u3_texture_index) + abs(u1_u2_texture_index) + u1_u3_shape_index) / 3
    mat_score = (1 - abs(u1_u3_texture_index) + abs(u1_u2_texture_index)) / 2

    stub = utils.make_stub(u2_texture_index, u1_u2_texture_index, u3_filled,
                           u2_filled, u1_filled)

    return mat_score, stub
Beispiel #3
0
def shadow_mask_unite(A, B):
    shadow_A = utils.fill_holes(A)
    shadow_B = utils.fill_holes(B)

    score, shadow_A_to_B_x, shadow_A_to_B_y = jaccard.jaccard_coef(A, B)

    shadow_A_aligned, shadow_B_aligned, _, _ = utils.align(
        shadow_A, shadow_B, shadow_A_to_B_x, shadow_A_to_B_y)
    mask = np.logical_and(shadow_A_aligned, shadow_B_aligned)

    A_aligned, B_aligned, _, _ = utils.align(A, B, shadow_A_to_B_x,
                                             shadow_A_to_B_y)
    union = np.logical_or(A_aligned, B_aligned)

    masked_union = np.logical_and(mask, union)

    return utils.trim_binary_image(masked_union)
Beispiel #4
0
def tpm(A_coms, B_coms, cur_A, cur_B, cur_A_com_ids, cur_B_com_ids):
    if len(cur_A_com_ids) != len(cur_B_com_ids):
        return [], []

    if 1 == len(cur_A_com_ids) and 1 == len(cur_B_com_ids):
        return cur_A_com_ids, cur_B_com_ids

    cur_A_filled = utils.fill_holes(cur_A)
    cur_B_filled = utils.fill_holes(cur_B)

    cur_A_filled_coms, _, _ = utils.decompose(cur_A_filled, 8, trim=False)
    cur_B_filled_coms, _, _ = utils.decompose(cur_B_filled, 8, trim=False)

    if len(cur_A_filled_coms) != len(cur_B_filled_coms):
        return [], []

    if len(cur_A_filled_coms) == len(cur_A_com_ids):
        # TODO inside-outside topo mapping failed here.
        # TODO fallback mapping method can be applied here, but for the current problems, it is unnecessary.
        return [], []

    A_com_groups = [[
        com_id for com_id in cur_A_com_ids
        if (np.logical_and(A_coms[com_id], A_filled_com) == A_coms[com_id]
            ).all()
    ] for A_filled_com in cur_A_filled_coms]

    B_com_groups = [[
        com_id for com_id in cur_B_com_ids
        if (np.logical_and(B_coms[com_id], B_filled_com) == B_coms[com_id]
            ).all()
    ] for B_filled_com in cur_B_filled_coms]

    if 1 == len(A_com_groups) and 1 == len(B_com_groups):
        return tpm_go_deeper(A_coms, B_coms, cur_A_com_ids, cur_A_filled,
                             cur_B_com_ids, cur_B_filled)
    else:
        return tpm_go_wider(A_coms, B_coms, A_com_groups, B_com_groups)
Beispiel #5
0
    def cloth_segmentation(self):
        # extract source image and segmented cloth
        try:
            source_img, source_seg = cloth_extractor.extract_cloth(
                self.source_img)

            source_img = cv.cvtColor(source_img, cv.COLOR_RGB2BGR)
            source_seg = cv.cvtColor(source_seg, cv.COLOR_RGB2BGR)
            # print(source_img.shape, source_seg.shape)
            # fill holes
            source_seg = utils.fill_holes(source_img, source_seg)

            return source_img, source_seg
        except Exception:
            raise Exception("Source image without cloth.")
Beispiel #6
0
def predict_shape_texture_transfer(prob, anlg, tran, d):
    u3 = prob.matrix[anlg.get("value")[2]]
    u1_filled = d.get("stub").get("u1_filled")
    u2_filled = d.get("stub").get("u2_filled")
    u3_filled = d.get("stub").get("u3_filled")
    u1_u2_texture_index = d.get("stub").get("u1_u2_texture_index")
    u2_texture_index = d.get("stub").get("u2_texture_index")
    u1_u3_shape_index = soft_jaccard.soft_jaccard(u1_filled, u3_filled)[0]

    pred_data = []
    for ii, opt in enumerate(prob.options):
        print(prob.name, anlg.get("name"), tran.get("name"), ii)

        opt_filled = utils.fill_holes(opt)

        # old_u2_opt_shape_index = jaccard.jaccard_coef(u2_filled, opt_filled)[0]
        # u2_opt_shape_index = soft_jaccard.soft_jaccard(u2_filled, opt_filled)[0]

        u2_opt_shape_index = soft_jaccard.soft_jaccard(u2_filled,
                                                       opt_filled)[0]

        u3_texture_index = np.logical_and(
            u3_filled, np.logical_not(u3)).sum() / u3_filled.sum()
        opt_texture_index = np.logical_and(
            opt_filled, np.logical_not(opt)).sum() / opt_filled.sum()
        u3_opt_texture_index = u3_texture_index - opt_texture_index

        # _, u2_to_opt_x, u2_to_opt_y = jaccard.jaccard_coef(u2, opt)
        # u2_texture_index, opt_texture_index = utils.texture_index(u2, opt, u2_filled, opt_filled, u2_to_opt_x, u2_to_opt_y)
        delta_texture_score = 1 - abs(u1_u2_texture_index -
                                      u3_opt_texture_index)
        texture_score = 1 - abs(u2_texture_index - opt_texture_index)
        delta_shape_score = 1 - abs(u1_u3_shape_index - u2_opt_shape_index)
        score = (texture_score + delta_texture_score + delta_shape_score) / 3

        pred_data.append({
            **d, "optn": ii + 1,
            "optn_score": score,
            "mato_score": (d.get("mat_score") + score) / 2,
            "pred": opt
        })

    return pred_data
Beispiel #7
0
def standardize(coms):

    coms_ref = []
    for com in coms:
        coms_ref.append(utils.fill_holes(com))

    min_sim = np.inf
    for A in coms_ref:
        for B in coms_ref:
            if 0 == A.sum() or 0 == B.sum():
                continue
            sim, _, _ = jaccard.jaccard_coef(A, B)
            if sim < min_sim:
                min_sim = sim

    if min_sim > 0.85:
        coms = utils.resize_to_average_shape(coms)
        coms_ref = [np.full_like(coms[0], fill_value=True)] * len(coms)
        return coms, coms_ref
    else:
        return coms, coms
Beispiel #8
0
    def guessFLAIRity(self):
        """
        Guess at whether data's contrast suppressed free water or not, i.e. return
        True for FLAIR DTI and False otherwise.  It operates by doing a rough
        segmentation of brain tissue and CSF, and checking whether the tissue is
        brighter than CSF for b ~ 0.

        Sets self.mask as a side-effect.
        """
        b = np.asarray(self.bvals)
        b0 = utils.calc_average_s0(self.data,
                                   b,
                                   self.relbthresh,
                                   estimator=np.median)
        embDt = np.exp(-b * self.Dt)
        embDCSF = np.exp(-b * self.DCSF)
        w = (embDt * (1.0 - embDCSF))**2
        tisssig = np.zeros(b0.shape)
        embb0s = {}
        sw = 0.0
        for v, emb in enumerate(embDCSF):
            if emb not in embb0s:
                embb0s[emb] = emb * b0
            tisssig += w[v] * (self.data[..., v] - embb0s[emb])
            sw += w[v]
        del embb0s
        tisssig /= sw

        # Don't use median_otsu because it assumes isotropic voxels.
        if self.nmed > 0:
            if self.medrad >= 1:
                ball = utils.make_structural_sphere(
                    self.aff, self.medrad * self.maxscale)
                if self.verbose:
                    print("Median filtering %d times with radius %f." %
                          (self.nmed, self.medrad * self.maxscale))
                for i in range(self.nmed):
                    tisssig = median_filter(tisssig, footprint=ball)
            elif self.medrad > 0:
                print("Warning: not median filtering since medrad < 1.")

        if self.verbose:
            print("Getting the Otsu threshold.")
        thresh = otsu(tisssig)
        self._mask = np.zeros(tisssig.shape, np.bool)
        self._mask[tisssig >= thresh] = 1

        ball = utils.make_structural_sphere(self.aff, max(10.0, self.maxscale))
        self._csfmask = utils.binary_closing(self._mask, ball)
        gaprad = max(self.closerad, 2 * self.maxscale)
        self._csfmask, success = utils.fill_holes(self._csfmask, self.aff,
                                                  gaprad, self.verbose)
        self._csfmask = utils.binary_opening(self._csfmask, ball)
        self._csfmask[self._mask > 0] = 0
        csfmed = np.median(b0[self._csfmask > 0])
        b0tiss = b0[self._mask > 0]

        # Now we have an approximate brain, and we know it is surrounded by CSF
        # (in vivo) or solution (ex vivo), which we'll call CSF.  Figure out
        # whether CSF is brighter or darker than tissue in the b0.
        tissmed = np.median(b0tiss)
        return tissmed > 2.0 * csfmed