Esempio n. 1
0
def getMaskFromCenterline(image, pointset, spacing, radius=10):
    data = npy.zeros(image.shape, dtype=npy.uint8)
    image_size = image[0, :, :].transpose().shape
    n_pt = 20

    for cnt in range(3):
        temp_point = pointset[npy.where(npy.round(pointset[:, -1]) == cnt)]
        if not temp_point.shape[0]:
            continue
        zmin = int(npy.min(temp_point[:, 2]) + 0.5)
        zmax = int(npy.max(temp_point[:, 2]) + 0.5)

        for z in range(zmin, zmax + 1):
            center_point = temp_point[npy.where(
                npy.round(temp_point[:, 2]) == z)]
            if center_point is not None:
                if center_point.shape[0] == 0:
                    continue

                data_point = npy.zeros([n_pt, 2], dtype=npy.float32)
                for i in range(0, n_pt):
                    data_point[i, 0] = center_point[0, 0] + radius * npy.cos(
                        2 * npy.pi * i / n_pt) / spacing[0]
                    data_point[i, 1] = center_point[0, 1] + radius * npy.sin(
                        2 * npy.pi * i / n_pt) / spacing[1]

                fixPoints(data_point, image_size)
                data[z, :, :] = data[z, :, :] | ac_mask(
                    data_point.transpose(), image_size).transpose()

    result = npy.cast['float32'](data)
    del data
    return result
Esempio n. 2
0
def getBinaryImageFromSegmentation(image, pointset):
    data = npy.zeros(image.shape, dtype=npy.uint8)
    image_size = image[0, :, :].transpose().shape

    for cnt in range(3):
        temp_point = pointset[npy.where(npy.round(pointset[:, -1]) == cnt)]
        if not temp_point.shape[0]:
            continue
        zmin = int(npy.min(temp_point[:, 2]) + 0.5)
        zmax = int(npy.max(temp_point[:, 2]) + 0.5)

        for z in range(zmin, zmax + 1):
            data_point = temp_point[npy.where(
                npy.round(temp_point[:, 2]) == z)]
            if data_point is not None:
                if data_point.shape[0] == 0:
                    continue

                ind = sortContourPoints(data_point)
                data_point = data_point[ind]

                data_point = data_point[:, :2].transpose()
                data[z, :, :] = data[z, :, :] | ac_mask(
                    data_point, image_size).transpose()

    result = npy.cast['float32'](data)
    del data
    return result
Esempio n. 3
0
def getBinaryImageFromSegmentation(image, pointset):
    data = npy.zeros(image.shape, dtype = npy.uint8)
    image_size = image[0, :, :].transpose().shape
    
    for cnt in range(3):
        temp_point = pointset[npy.where(npy.round(pointset[:, -1]) == cnt)]
        if not temp_point.shape[0]:
            continue
        zmin = int(npy.min(temp_point[:, 2]) + 0.5)
        zmax = int(npy.max(temp_point[:, 2]) + 0.5)
        
        for z in range(zmin, zmax + 1):
            data_point = temp_point[npy.where(npy.round(temp_point[:, 2]) == z)]
            if data_point is not None:
                if data_point.shape[0] == 0:
                    continue
                
                ind = sortContourPoints(data_point)
                data_point = data_point[ind]
                
                data_point = data_point[:, :2].transpose()
                data[z, :, :] = data[z, :, :] | ac_mask(data_point, image_size).transpose()
                        
    result =  npy.cast['float32'](data)
    del data
    return result
Esempio n. 4
0
def getMaskFromCenterline(image, pointset, spacing, radius = 10):
    data = npy.zeros(image.shape, dtype = npy.uint8)
    image_size = image[0, :, :].transpose().shape
    n_pt = 20
    
    for cnt in range(3):
        temp_point = pointset[npy.where(npy.round(pointset[:, -1]) == cnt)]
        if not temp_point.shape[0]:
            continue
        zmin = int(npy.min(temp_point[:, 2]) + 0.5)
        zmax = int(npy.max(temp_point[:, 2]) + 0.5)
        
        for z in range(zmin, zmax + 1):
            center_point = temp_point[npy.where(npy.round(temp_point[:, 2]) == z)]
            if center_point is not None:
                if center_point.shape[0] == 0:
                    continue
                
                data_point = npy.zeros([n_pt, 2], dtype = npy.float32)
                for i in range(0, n_pt):
                    data_point[i, 0] = center_point[0, 0] + radius * npy.cos(2 * npy.pi * i / n_pt) / spacing[0]
                    data_point[i, 1] = center_point[0, 1] + radius * npy.sin(2 * npy.pi * i / n_pt) / spacing[1]
                
                fixPoints(data_point, image_size)
                data[z, :, :] = data[z, :, :] | ac_mask(data_point.transpose(), image_size).transpose()
                
    result =  npy.cast['float32'](data)
    del data
    return result
Esempio n. 5
0
def calCenterFromContour(pointset, image):
    mask = ac_mask(pointset[:, :2].transpose(), image.transpose().shape).transpose()
    dist = cv2.distanceTransform(mask, cv.CV_DIST_L2, 3)
    value = npy.max(dist)

    indx, indy = npy.where(dist == npy.max(dist))
    indx = npy.cast['float32'](indx)
    indy = npy.cast['float32'](indy)

    center = npy.array([[npy.mean(indy), npy.mean(indx)]])
    return center
Esempio n. 6
0
def calIntensityCentroidFromContour(pointset, image):
    mask = ac_mask(pointset[:, :2].transpose(), image.transpose().shape).transpose()
    result = image * mask
    cnt = npy.sum(mask)
    m00 = npy.sum(result) / cnt
    xx, yy = npy.mgrid[:image.shape[0], :image.shape[1]]
    m10 = npy.sum(xx * result) / cnt
    m01 = npy.sum(yy * result) / cnt
    
    center = npy.array([[m01 / m00, m10 / m00]])
    return center
Esempio n. 7
0
def calCenterFromContour(pointset, image):
    mask = ac_mask(pointset[:, :2].transpose(),
                   image.transpose().shape).transpose()
    dist = cv2.distanceTransform(mask, cv.CV_DIST_L2, 3)
    value = npy.max(dist)

    indx, indy = npy.where(dist == npy.max(dist))
    indx = npy.cast['float32'](indx)
    indy = npy.cast['float32'](indy)

    center = npy.array([[npy.mean(indy), npy.mean(indx)]])
    return center
Esempio n. 8
0
def calIntensityCentroidFromContour(pointset, image):
    mask = ac_mask(pointset[:, :2].transpose(),
                   image.transpose().shape).transpose()
    result = image * mask
    cnt = npy.sum(mask)
    m00 = npy.sum(result) / cnt
    xx, yy = npy.mgrid[:image.shape[0], :image.shape[1]]
    m10 = npy.sum(xx * result) / cnt
    m01 = npy.sum(yy * result) / cnt

    center = npy.array([[m01 / m00, m10 / m00]])
    return center
Esempio n. 9
0
    def analysis(self, data, point_data_fix=None, area=False):
        if point_data_fix is None:
            point_data_fix = self.gui.dataModel[
                data.getFixedIndex()].getPointSet('Contour').copy()
        point_data_result = data.getPointSet('Contour').copy()
        image_size = image = data.getData()[0, :, :].transpose().shape
        self.spacing = data.getResolution().tolist()
        self.spacing[
            2] = 1.0  # The resolution of z axis is nothing to do with the analysis
        point_data_fix[:, :3] *= self.spacing[:3]
        point_data_result[:, :3] *= self.spacing[:3]

        cnt_num = npy.array([0, 0, 0])
        union_area = npy.array([0.0, 0.0, 0.0])
        total_area = npy.array([0.0, 0.0, 0.0])
        if area:
            mr_area = [0.0, 0.0, 0.0, 0.0]
            us_area = [0.0, 0.0, 0.0, 0.0]

        for cnt in range(3):
            temp_result = point_data_result[npy.where(
                npy.round(point_data_result[:, -1]) == cnt)]
            temp_fix = point_data_fix[npy.where(
                npy.round(point_data_fix[:, -1]) == cnt)]
            if not temp_result.shape[0] or not temp_fix.shape[0]:
                continue
            zmin = int(
                npy.min([npy.min(temp_result[:, 2]),
                         npy.min(temp_fix[:, 2])]) + 0.5)
            zmax = int(
                npy.max([npy.max(temp_result[:, 2]),
                         npy.max(temp_fix[:, 2])]) + 0.5)

            for z in range(zmin, zmax + 1):
                data_fix = temp_fix[npy.where(npy.round(temp_fix[:, 2]) == z)]
                data_result = temp_result[npy.where(
                    npy.round(temp_result[:, 2]) == z)]
                if data_fix is not None and data_result is not None:
                    if data_fix.shape[0] == 0 or data_result.shape[0] == 0:
                        continue
                    cnt_num[cnt] += 1
                    #center_fix = npy.mean(data_fix[:, :2], axis = 0)
                    center_fix = calCentroidFromContour(data_fix[:, :2])[0]
                    #center_result = npy.mean(data_result[:, :2], axis = 0)
                    center_result = calCentroidFromContour(
                        data_result[:, :2])[0]
                    points_fix = getPointsOntheSpline(
                        data_fix, center_fix, data_fix.shape[0] * 10)[:, :2]
                    points_result = getPointsOntheSpline(
                        data_result, center_result,
                        data_result.shape[0] * 10)[:, :2]

                    fix_mask = ac_mask(points_fix.transpose(), image_size)
                    result_mask = ac_mask(points_result.transpose(),
                                          image_size)
                    temp_fix_area = npy.sum(fix_mask)
                    temp_result_area = npy.sum(result_mask)
                    total_area[cnt] += temp_fix_area + temp_result_area
                    union_area[cnt] += npy.sum(fix_mask | result_mask)
                    if area:
                        mr_area[cnt] += temp_fix_area
                        mr_area[3] += temp_fix_area
                        us_area[cnt] += temp_result_area
                        us_area[3] += temp_result_area

        intersect_area = total_area - union_area
        jaccard_index = intersect_area / union_area
        dice_index = 2 * intersect_area / total_area
        jaccard_index_all = npy.sum(intersect_area) / npy.sum(union_area)
        dice_index_all = 2 * npy.sum(intersect_area) / npy.sum(total_area)
        # Replace the NAN
        jaccard_index[jaccard_index != jaccard_index] = 0
        dice_index[dice_index != dice_index] = 0

        if self.gui is not None:
            message = "Jaccard Index on Vessel 0: %0.3f\nJaccard Index on Vessel 1: %0.3f\nJaccard Index on Vessel 2: %0.3f\nTotal Jaccard Index: %0.3f\n" \
                % (jaccard_index[0], jaccard_index[1], jaccard_index[2], jaccard_index_all) + \
                "-------------------------------------------------------\n" + \
                "Dice Index on Vessel 0: %0.3f\nDice Index on Vessel 1: %0.3f\nDice Index on Vessel 2: %0.3f\nTotal Dice Index: %0.3f" \
                % (dice_index[0], dice_index[1], dice_index[2], dice_index_all)
            self.gui.showErrorMessage("Registration Area Index", message)

        if not area:
            return dice_index, dice_index_all
        else:
            for i in range(3):
                mr_area[i] /= cnt_num[i]
                us_area[i] /= cnt_num[i]
            mr_area[3] /= npy.sum(cnt_num)
            us_area[3] /= npy.sum(cnt_num)
            return dice_index, dice_index_all, mr_area, us_area