Esempio n. 1
0
        def autoDetectContour(point, cnt, start, end, delta):
            self.new_points = npy.append(self.new_points, point, 0)
            points = point[:, :-2]
            count = 0
            for j in range(start + delta, end + delta, delta):
                center = calCentroidFromContour(points).reshape(2)
                image = dataset['fix'].getData()[j, :, :].transpose().copy()
                image = (image - npy.min(image)) / (npy.max(image) -
                                                    npy.min(image)) * 255
                result = ac_segmentation(center, image)

                a1 = ac_area(points.transpose(), image.shape)
                a2 = ac_area(result, image.shape)
                rate = a2 * 1.0 / a1
                if rate >= min(1.5 + count * 0.2, 2.1) or rate <= 0.7:
                    temp_array = points.copy()
                    if cnt != 1 and rate > 0.7:
                        count += 1
                else:
                    temp_array = result.transpose().copy()
                    count = 0
                points = temp_array.copy()
                temp_array = npy.insert(temp_array, 2, [[j], [cnt]], 1)
                self.new_points = npy.append(self.new_points, temp_array, 0)

                sys.stdout.write(str(j) + ',')
                sys.stdout.flush()
            print ' '
        def autoDetectContour(point, cnt, start, end, delta, res, type):
            self.new_points = npy.append(self.new_points, point, 0)
            points = point[:, :-2]
            d = 20
            count = 0
            for i in range(start + delta, end + delta, delta):
                center = calCentroidFromContour(points).reshape(2)
                image = dataset[type].getData()[i, :, :].transpose().copy()
                image = (image - npy.min(image)) / (npy.max(image) -
                                                    npy.min(image)) * 255
                down = npy.max([npy.ceil(center[0] - d / res[0]), 0])
                up = npy.min(
                    [npy.floor(center[0] + d / res[0]), image.shape[0]])
                left = npy.max([npy.ceil(center[1] - d / res[1]), 0])
                right = npy.min(
                    [npy.floor(center[1] + d / res[1]), image.shape[1]])
                crop_image = image[down:up, left:right]
                center -= [down, left]

                result = ac_segmentation(center, crop_image)

                a1 = ac_area(points.transpose(), image.shape)
                a2 = ac_area(result, crop_image.shape)
                rate = a2 * 1.0 / a1
                if rate >= min(1.5 + count * 0.2, 2.1) or rate <= 0.7:
                    temp_array = points.copy()
                    if cnt != 1 and rate > 0.7:
                        count += 1
                else:
                    temp_array = result.transpose().copy()
                    temp_array[:, :2] += [down, left]
                    count = 0
                points = temp_array.copy()

                temp_array = npy.insert(temp_array, 2, [[i], [cnt]], 1)
                self.new_points = npy.append(self.new_points, temp_array, 0)

                sys.stdout.write(str(i) + ',')
                sys.stdout.flush()
            print ' '
 def autoDetectContour(point, cnt, start, end, delta, res):
     self.new_points = npy.append(self.new_points, point, 0)
     points = point[:, :-2]
     d = 20
     count = 0
     for i in range(start + delta, end + delta, delta):
         center = calCentroidFromContour(points).reshape(2)
         image = dataset['fix'].getData()[i, :, :].transpose().copy()
         image = (image - npy.min(image)) / (npy.max(image) - npy.min(image)) * 255
         down = npy.max([npy.ceil(center[0] - d / res[0]), 0])
         up = npy.min([npy.floor(center[0] + d / res[0]), image.shape[0]])
         left = npy.max([npy.ceil(center[1] - d / res[1]), 0])
         right = npy.min([npy.floor(center[1] + d / res[1]), image.shape[1]])
         crop_image = image[down : up, left : right]
         center -= [down, left]
         
         result = ac_segmentation(center, crop_image)
         
         a1 = ac_area(points.transpose(), image.shape)
         a2 = ac_area(result, crop_image.shape)
         rate = a2 * 1.0 / a1
         if rate >= min(1.5 + count * 0.2, 2.1) or rate <= 0.7:
             temp_array = points.copy()
             if cnt != 1 and rate > 0.7:
                 count += 1
         else:
             temp_array = result.transpose().copy()
             temp_array[:, :2] += [down, left]
             count = 0
         points = temp_array.copy()
         
         
         temp_array = npy.insert(temp_array, 2, [[i], [cnt]], 1)
         self.new_points = npy.append(self.new_points, temp_array, 0)
         
         sys.stdout.write(str(i) + ',')
         sys.stdout.flush()
     print ' '
Esempio n. 4
0
    def analysis(self, data, point_data_fix=None, all=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()
        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])
        mean_dis = npy.array([0.0, 0.0, 0.0])
        max_dis = npy.array([0.0, 0.0, 0.0])
        square_sum_dis = npy.array([0.0, 0.0, 0.0])
        if all:
            result = [{}, {}, {}]
            bif = db.getBifurcation(point_data_fix)

        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.max([npy.min(temp_result[:, 2]),
                         npy.min(temp_fix[:, 2])]) + 0.5)
            zmax = int(
                npy.min([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,
                                                      900)
                    points_result = getPointsOntheSpline(
                        data_result, center_result, 900)

                    i = j = 0
                    for k in range(-44, 46):
                        angle = k * 4 / 180.0 * npy.pi
                        while i < 900 and points_fix[i, 2] < angle:
                            i += 1
                        if i == 900 or (i > 0 and angle - points_fix[i - 1, 2]
                                        < points_fix[i, 2] - angle):
                            ind_fix = i - 1
                        else:
                            ind_fix = i
                        while j < 900 and points_result[j, 2] < angle:
                            j += 1
                        if j == 900 or (j > 0
                                        and angle - points_result[j - 1, 2] <
                                        points_result[j, 2] - angle):
                            ind_result = j - 1
                        else:
                            ind_result = j
                        temp_dis = npy.hypot(
                            points_fix[ind_fix, 0] -
                            points_result[ind_result, 0],
                            points_fix[ind_fix, 1] -
                            points_result[ind_result, 1])
                        max_dis[cnt] = npy.max([max_dis[cnt], temp_dis])
                        mean_dis[cnt] += temp_dis
                        square_sum_dis[cnt] += temp_dis**2
                        if all:
                            result[cnt][z - bif] = result[cnt].get(
                                z - bif, 0) + temp_dis

        cnt_total = npy.sum(cnt_num)
        sd = npy.sqrt(
            npy.max([(square_sum_dis - mean_dis**2 / (90 * cnt_num)) /
                     (90 * cnt_num - 1), [0, 0, 0]],
                    axis=0))
        sd[sd != sd] = 0
        sd_all = npy.sqrt(
            npy.max([(npy.sum(square_sum_dis) - npy.sum(mean_dis)**2 /
                      (90 * cnt_total)) / (90 * cnt_total - 1), 0]))

        mean_dis /= 90
        mean_whole = npy.sum(mean_dis)
        mean_dis /= cnt_num
        mean_dis[
            mean_dis != mean_dis] = 0  # Replace the NAN in the mean distance

        if self.gui is not None:
            message = "Error on Vessel 0: %0.2fmm (SD = %0.2fmm, Total %d slices)\nError on Vessel 1: %0.2fmm (SD = %0.2fmm, Total %d slices)\nError on Vessel 2: %0.2fmm (SD = %0.2fmm, Total %d slices)\nWhole Error: %0.2fmm (SD = %0.2fmm, Total %d slices)\n" \
                % (mean_dis[0], sd[0], cnt_num[0], mean_dis[1], sd[1], cnt_num[1], mean_dis[2], sd[2], cnt_num[2], mean_whole / cnt_total, sd_all, cnt_total) + \
                "-----------------------------------------------------------------------------\n" + \
                "Max Error on Vessel 0: %0.2fmm\nMax Error on Vessel 1: %0.2fmm\nMax Error on Vessel 2: %0.2fmm\nTotal Max Error: %0.2fmm" \
                % (max_dis[0], max_dis[1], max_dis[2], npy.max(max_dis))
            self.gui.showErrorMessage("Contour Registration Error", message)

        if not all:
            return mean_dis, mean_whole / cnt_total, max_dis, npy.max(max_dis)
        else:
            for cnt in range(3):
                for x in result[cnt].keys():
                    result[cnt][x] /= 90
            return mean_dis, mean_whole / cnt_total, max_dis, npy.max(
                max_dis), result
Esempio n. 5
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
 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()
     self.spacing = data.getResolution().tolist()
     point_data_fix[:, :2] *= self.spacing[:2]
     point_data_result[:, :2] *= self.spacing[:2]
     
     center_data = calCenterlineFromContour({'Contour': point_data_fix})
     ind = center_data[:, 2].argsort()
     center_data = center_data[ind]
     center_data_z = center_data[:, 2].copy()
     fixed_bif = db.getBifurcation(center_data)
     bif_point = center_data[npy.round(center_data[:, 2]) == fixed_bif - 1]
     center_data[:, :3] *= self.spacing[:3]
     bif_point[:, :3] *= self.spacing[:3]
     
     spline = [None, None, None]
     for cnt in range(3):
         spline[cnt] = [None, None, None]
         xx = center_data_z[npy.round(center_data[:, -1]) == cnt]
         yy = center_data[npy.round(center_data[:, -1]) == cnt]
         if cnt > 0:
             xx = npy.append(fixed_bif - 1, xx)
             yy = npy.append(bif_point, yy, axis = 0)
         
         for i in range(3):
             spline[cnt][i] = itp.InterpolatedUnivariateSpline(xx, yy[:, i])
     
     cnt_num = npy.array([0, 0, 0])
     mean_dis = npy.array([0.0, 0.0, 0.0])
     max_dis = npy.array([0.0, 0.0, 0.0])
     square_sum_dis = npy.array([0.0, 0.0, 0.0])
     if area:
         area_mr = npy.array([0.0, 0.0, 0.0])
         area_us = npy.array([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.max([npy.min(temp_result[:, 2]), npy.min(temp_fix[:, 2])]) + 0.5)
         zmax = int(npy.min([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, area_fix = calCentroidFromContour(data_fix[:, :2], True)
                 center_fix = center_fix[0]
                 #center_result = npy.mean(data_result[:, :2], axis = 0)
                 center_result, area_result = calCentroidFromContour(data_result[:, :2], True)
                 center_result = center_result[0]
                 if area:
                     area_mr[cnt] += area_fix
                     area_us[cnt] += area_result
                 points_fix = getPointsOntheSpline(data_fix, center_fix, 900)
                 points_result = getPointsOntheSpline(data_result, center_result, 900)
                 
                 normal = npy.array([None, None, None])
                 
                 for i in range(3):
                     normal[i] = spline[cnt][i].derivatives(z)[1]
                 w1 = normal[2] ** 2 / npy.sum(normal ** 2) # cos(alpha) ^ 2
                 theta0 = npy.arctan2(normal[1], normal[0])
                 
                 i = j = 0
                 for k in range(-44, 46):
                     angle = k * 4 / 180.0 * npy.pi
                     while i < 900 and points_fix[i, 2] < angle:
                         i += 1
                     if i == 900 or (i > 0 and angle - points_fix[i - 1, 2] < points_fix[i, 2] - angle):
                         ind_fix = i - 1
                     else:
                         ind_fix = i
                     while j < 900 and points_result[j, 2] < angle:
                         j += 1
                     if j == 900 or (j > 0 and angle - points_result[j - 1, 2] < points_result[j, 2] - angle):
                         ind_result = j - 1
                     else:
                         ind_result = j
                         
                     weigh = npy.sqrt(npy.sin(angle - theta0) ** 2 + npy.cos(angle - theta0) ** 2 / w1)
                     temp_dis = npy.hypot(points_fix[ind_fix, 0] - points_result[ind_result, 0], points_fix[ind_fix, 1] - points_result[ind_result, 1]) / weigh
                     #if area:
                     #    temp_dis /= max([area_result / area_fix, area_fix / area_result])
                     max_dis[cnt] = npy.max([max_dis[cnt], temp_dis])
                     mean_dis[cnt] += temp_dis
     
     cnt_total = npy.sum(cnt_num)
     
     mean_dis /= 90
     
     if area:
         for cnt in range(3):
             if area_mr[cnt] < area_us[cnt]:
                 rate = area_us[cnt] / area_mr[cnt]
             else:
                 rate = area_us[cnt] / area_mr[cnt]
             mean_dis[cnt] /= rate
     
     mean_whole = npy.sum(mean_dis)
     mean_dis /= cnt_num
     mean_dis[mean_dis != mean_dis] = 0 # Replace the NAN in the mean distance
     
     if self.gui is not None:
         message = "Error on Vessel 0: %0.2fmm (Total %d slices)\nError on Vessel 1: %0.2fmm (Total %d slices)\nError on Vessel 2: %0.2fmm (Total %d slices)\nWhole Error: %0.2fmm (Total %d slices)\n" \
             % (mean_dis[0], cnt_num[0], mean_dis[1], cnt_num[1], mean_dis[2], cnt_num[2], mean_whole / cnt_total, cnt_total) + \
             "-----------------------------------------------------------------------------\n" + \
             "Max Error on Vessel 0: %0.2fmm\nMax Error on Vessel 1: %0.2fmm\nMax Error on Vessel 2: %0.2fmm\nTotal Max Error: %0.2fmm" \
             % (max_dis[0], max_dis[1], max_dis[2], npy.max(max_dis));
         self.gui.showErrorMessage("Weighted Contour Registration Error", message)
     return mean_dis, mean_whole / cnt_total, max_dis, npy.max(max_dis)
Esempio n. 7
0
def augmentPointset(ori_points, multiple, opt_size, bif, nn=-1):
    if multiple <= 1:
        return ori_points
    if nn < 0:
        zmin = int(npy.min(ori_points[:, 2]) + 0.5)
        zmax = int(npy.max(ori_points[:, 2]) + 0.5)
        nn = int((opt_size - ori_points.shape[0]) / ((2 * zmax - zmin - bif)) /
                 (multiple - 1) + 0.5) + 1
        #print nn

    new_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32)
    zmin = [0, 0, 0]
    zmax = [0, 0, 0]
    resampled_points = [None, None, None]
    for cnt in range(3):
        temp_result = ori_points[npy.where(
            npy.round(ori_points[:, -1]) == cnt)]
        if not temp_result.shape[0]:
            continue
        zmin[cnt] = int(npy.min(temp_result[:, 2]) + 0.5)
        zmax[cnt] = int(npy.max(temp_result[:, 2]) + 0.5)
        resampled_points[cnt] = npy.zeros(
            [(zmax[cnt] - zmin[cnt] + 1) * nn, 4], dtype=npy.float32)
        resampled_index = 0

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

                #center_result = npy.mean(data_result[:, :2], axis = 0)
                center_result = calCentroidFromContour(data_result[:, :2])[0]
                points_result = getPointsOntheSpline(data_result,
                                                     center_result, 900)

                i = 0
                for k in range(-nn / 2 + 1, nn / 2 + 1):
                    angle = k * 2 * npy.pi / nn

                    while i < 900 and points_result[i, 2] < angle:
                        i += 1
                    if i == 900 or (i > 0 and angle - points_result[i - 1, 2] <
                                    points_result[i, 2] - angle):
                        ind_result = i - 1
                    else:
                        ind_result = i

                    resampled_points[cnt][resampled_index, :2] = points_result[
                        ind_result, :2]
                    resampled_points[cnt][resampled_index, 2] = z
                    resampled_points[cnt][resampled_index, 3] = k + 4
                    resampled_index += 1
    trans_points = npy.array(ori_points)
    for cnt in range(3):
        for k in range(0, nn):
            data = resampled_points[cnt][npy.where(
                npy.round(resampled_points[cnt][:, -1]) == k)]
            count = data.shape[0]
            if count == 0:
                continue
            points = vtk.vtkPoints()
            for i in range(count):
                points.InsertPoint(i, data[i, 0], data[i, 1], data[i, 2])

            para_spline = vtk.vtkParametricSpline()
            para_spline.SetPoints(points)
            para_spline.ClosedOff()

            deltaz = 1.0 / multiple
            old_pt = [0.0, 0.0, 0.0]
            numberOfOutputPoints = int((zmax[cnt] - zmin[cnt] + 1) * 10)
            i = 0

            for z in range(zmin[cnt], zmax[cnt] + 1):
                znow = float(z)
                for dd in range(1, multiple):
                    znow += deltaz
                    while i < numberOfOutputPoints:
                        t = i * 1.0 / numberOfOutputPoints
                        pt = [0.0, 0.0, 0.0]
                        para_spline.Evaluate([t, t, t], pt, [0] * 9)
                        if pt[2] >= znow:
                            if pt[2] - znow < znow - old_pt[2]:
                                new_point = pt
                            else:
                                new_point = old_pt
                            trans_points = npy.append(
                                trans_points,
                                [[new_point[0], new_point[1], znow, cnt]],
                                axis=0)

                            old_pt = pt
                            break
                        i += 1
    return trans_points
Esempio n. 8
0
def resliceTheResultPoints(moving_points,
                           moving_center,
                           nn,
                           moving_res,
                           fixed_res,
                           discard,
                           R,
                           T,
                           C=npy.asmatrix([0, 0, 0]).T):
    resampled_points = [None, None, None]
    nearbif_points = [None, None, None]
    nearbif_center = [None, None]
    bif_slice = 0

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

        resampled_points[cnt] = npy.zeros([(zmax - zmin + 1) * nn, 4],
                                          dtype=npy.float32)
        resampled_index = 0

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

                center_result = calCentroidFromContour(data_result[:, :2])[0]
                points_result = getPointsOntheSpline(data_result,
                                                     center_result, 900)
                if cnt > 0 and z == zmin:
                    nearbif_points[cnt] = points_result
                    nearbif_center[cnt - 1] = center_result
                    bif_slice = z
                elif cnt == 0 and z == zmax - 1:
                    nearbif_points[cnt] = points_result

                i = 0
                for k in range(-nn / 2 + 1, nn / 2 + 1):
                    angle = k * 360 / nn / 180.0 * npy.pi

                    while i < 900 and points_result[i, 2] < angle:
                        i += 1
                    if i == 900 or (i > 0 and angle - points_result[i - 1, 2] <
                                    points_result[i, 2] - angle):
                        ind_result = i - 1
                    else:
                        ind_result = i

                    resampled_points[cnt][resampled_index, :2] = points_result[
                        ind_result, :2]
                    resampled_points[cnt][resampled_index, 2] = z
                    resampled_points[cnt][resampled_index, 3] = k + nn / 2 - 1
                    resampled_index += 1

    nearbif_angle = [
        npy.arctan2(nearbif_center[1][1] - nearbif_center[0][1],
                    nearbif_center[1][0] - nearbif_center[0][0]),
        npy.arctan2(nearbif_center[0][1] - nearbif_center[1][1],
                    nearbif_center[0][0] - nearbif_center[1][0])
    ]
    point_near_bif = npy.zeros([2, 2], dtype=npy.float32)
    for cnt in range(2):
        ind = npy.argmin(
            npy.abs(nearbif_points[cnt + 1][:, 2] - nearbif_angle[cnt]))
        point_near_bif[cnt, :] = nearbif_points[cnt + 1][ind, :2]
    bif_points = npy.zeros([3, 3], dtype=npy.float32)
    bif_points[0, :2] = npy.mean(point_near_bif, axis=0)
    bif_points[0, 2] = bif_slice
    nearbif_angle = [
        npy.arctan2(nearbif_center[1][0] - nearbif_center[0][0],
                    nearbif_center[0][1] - nearbif_center[1][1]),
        npy.arctan2(nearbif_center[0][0] - nearbif_center[1][0],
                    nearbif_center[1][1] - nearbif_center[0][1])
    ]
    nearbif_points[0][:, 2] = npy.arctan2(
        nearbif_points[0][:, 1] - bif_points[0, 1],
        nearbif_points[0][:, 0] - bif_points[0, 0])
    for cnt in range(1, 3):
        ind = npy.argmin(
            npy.abs(nearbif_points[0][:, 2] - nearbif_angle[cnt - 1]))
        bif_points[cnt, :2] = nearbif_points[0][ind, :2]
    bif_points[1:, 2] = bif_slice - 1

    # Apply the transformation on the resampled points
    for cnt in range(3):
        resampled_points[cnt][:, :3] = applyTransformForPoints(
            resampled_points[cnt][:, :3], moving_res, fixed_res, R, T, C)
    bif_points = applyTransformForPoints(bif_points, moving_res, fixed_res, R,
                                         T, C)

    # Resample the points near the bifurcation
    points = vtk.vtkPoints()
    points.InsertPoint(0, bif_points[1, 0], bif_points[1, 1], bif_points[1, 2])
    points.InsertPoint(1, bif_points[0, 0], bif_points[0, 1], bif_points[0, 2])
    points.InsertPoint(2, bif_points[2, 0], bif_points[2, 1], bif_points[2, 2])

    para_spline = vtk.vtkParametricSpline()
    para_spline.SetPoints(points)
    para_spline.ClosedOff()

    numberOfOutputPoints = 6
    new_bif_points = npy.zeros([numberOfOutputPoints + 1, 4],
                               dtype=npy.float32)

    for i in range(0, numberOfOutputPoints + 1):
        t = i * 1.0 / numberOfOutputPoints
        pt = [0.0, 0.0, 0.0]
        para_spline.Evaluate([t, t, t], pt, [0] * 9)
        new_bif_points[i, :3] = pt
    new_bif_points[:, 3] = 0

    # Reslice the result points
    trans_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32)
    bif_slice = int(npy.ceil(bif_points[0, 2]))

    for cnt in range(3):
        zmin = int(npy.ceil(npy.max(resampled_points[cnt][:nn, 2])))
        zmax = int(npy.min(resampled_points[cnt][-nn:, 2]))
        if not discard:
            if cnt == 0:
                zmax = bif_slice
            else:
                zmin = bif_slice

        for k in range(0, nn):
            data = resampled_points[cnt][npy.where(
                npy.round(resampled_points[cnt][:, -1]) == k)]
            if not discard:
                if cnt == 0:
                    dis1 = npy.hypot(
                        data[-1, 0] - resampled_points[1][nn:nn * 2, 0],
                        data[-1, 1] - resampled_points[1][nn:nn * 2, 1])
                    #dis1 = npy.hypot(data[-1, 0] - nearbif_points[1][:, 0], data[-1, 1] - nearbif_points[1][:, 1])
                    ind1 = npy.argmin(dis1)
                    dis2 = npy.hypot(
                        data[-1, 0] - resampled_points[2][nn:nn * 2, 0],
                        data[-1, 1] - resampled_points[2][nn:nn * 2, 1])
                    ind2 = npy.argmin(dis2)
                    if dis1[ind1] < dis2[ind2]:
                        data_add = resampled_points[1][npy.where(
                            (npy.round(resampled_points[1][:, -1]) == ind1)
                            & (resampled_points[1][:, 2] <= zmax + 1))]
                    else:
                        data_add = resampled_points[2][npy.where(
                            (npy.round(resampled_points[2][:, -1]) == ind2)
                            & (resampled_points[2][:, 2] <= zmax + 1))]
                    data = npy.append(data, data_add[1:, :], axis=0)
                else:
                    dis1 = npy.hypot(
                        data[0, 0] - resampled_points[0][-nn * 2:-nn, 0],
                        data[0, 1] - resampled_points[0][-nn * 2:-nn, 1])
                    ind1 = npy.argmin(dis1)
                    dis2 = npy.sqrt(
                        npy.sum((new_bif_points[:, :3] - data[0, :3])**2,
                                axis=1))
                    ind2 = npy.argmin(dis2)
                    if dis1[ind1] < dis2[ind2]:
                        data_add = resampled_points[0][npy.where(
                            (npy.round(resampled_points[0][:, -1]) == ind1)
                            & (resampled_points[0][:, 2] >= zmin - 1))]
                    else:
                        data_add = new_bif_points[ind2, :].reshape(1, -1)
                    data = npy.append(data_add[:-1, :], data, axis=0)

            count = data.shape[0]
            if count == 0:
                continue
            points = vtk.vtkPoints()
            for i in range(count):
                points.InsertPoint(i, data[i, 0], data[i, 1], data[i, 2])

            para_spline = vtk.vtkParametricSpline()
            para_spline.SetPoints(points)
            para_spline.ClosedOff()

            znow = zmin
            old_pt = [0.0, 0.0, 0.0]
            numberOfOutputPoints = int((zmax - zmin + 3) * nn)

            for i in range(0, numberOfOutputPoints):
                t = i * 1.0 / numberOfOutputPoints
                pt = [0.0, 0.0, 0.0]
                para_spline.Evaluate([t, t, t], pt, [0] * 9)
                if pt[2] >= znow:
                    if pt[2] - znow < znow - old_pt[2]:
                        new_point = pt
                    else:
                        new_point = old_pt
                    trans_points = npy.append(
                        trans_points,
                        [[new_point[0], new_point[1], znow, cnt]],
                        axis=0)
                    znow += 1
                    if znow > zmax:
                        break
                old_pt = pt

    new_trans_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32)
    for cnt in range(3):
        temp_result = trans_points[npy.where(
            npy.round(trans_points[:, -1]) == cnt)]
        if not temp_result.shape[0]:
            continue
        zmin = int(npy.min(temp_result[:, 2]) + 0.5)
        zmax = int(npy.max(temp_result[:, 2]) + 0.5)
        for z in range(zmin, zmax + 1):
            data_result = temp_result[npy.where(
                npy.round(temp_result[:, 2]) == z)]
            if data_result is not None:
                if data_result.shape[0] == 0:
                    continue

                ind = sortContourPoints(data_result)
                data_result[:, :] = data_result[ind, :]

                for x in data_result:
                    if isDifferent(new_trans_points[-1, :], x):
                        new_trans_points = npy.append(new_trans_points,
                                                      x.reshape(1, -1),
                                                      axis=0)

    result_center_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32)
    if moving_center is not None and moving_center.shape[0] > 1:
        result_center = moving_center[npy.where(moving_center[:, 0] >= 0)]

        result_center[:, :3] = applyTransformForPoints(result_center[:, :3],
                                                       moving_res, fixed_res,
                                                       R, T, C)
        ind = result_center[:, 2].argsort()
        result_center = result_center[ind]

        result_center_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32)
        for cnt in range(3):
            resampled_points = result_center[npy.where(
                npy.round(result_center[:, -1]) == cnt)]
            zmin = int(npy.ceil(resampled_points[0, 2]))
            zmax = int(resampled_points[-1, 2])

            count = resampled_points.shape[0]
            points = vtk.vtkPoints()
            for i in range(count):
                points.InsertPoint(i, resampled_points[i, 0],
                                   resampled_points[i, 1], resampled_points[i,
                                                                            2])

            para_spline = vtk.vtkParametricSpline()
            para_spline.SetPoints(points)
            para_spline.ClosedOff()

            znow = zmin
            old_pt = [0.0, 0.0, 0.0]
            numberOfOutputPoints = int((zmax - zmin + 1) * 10)

            for i in range(0, numberOfOutputPoints):
                t = i * 1.0 / numberOfOutputPoints
                pt = [0.0, 0.0, 0.0]
                para_spline.Evaluate([t, t, t], pt, [0] * 9)
                if pt[2] >= znow:
                    if pt[2] - znow < znow - old_pt[2]:
                        new_point = pt
                    else:
                        new_point = old_pt
                    result_center_points = npy.append(
                        result_center_points,
                        [[new_point[0], new_point[1], znow, cnt]],
                        axis=0)
                    znow += 1
                    if znow > zmax:
                        break
                old_pt = pt
    return new_trans_points, result_center_points
Esempio n. 9
0
def augmentPointset(ori_points, multiple, opt_size, bif, nn = -1):
    if multiple <= 1:
        return ori_points
    if nn < 0:
        zmin = int(npy.min(ori_points[:, 2]) + 0.5)
        zmax = int(npy.max(ori_points[:, 2]) + 0.5)
        nn = int((opt_size - ori_points.shape[0]) / ((2 * zmax - zmin - bif)) / (multiple - 1) + 0.5) + 1
        #print nn
    
    new_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32)
    zmin = [0, 0, 0]
    zmax = [0, 0, 0]
    resampled_points = [None, None, None]
    for cnt in range(3):
        temp_result = ori_points[npy.where(npy.round(ori_points[:, -1]) == cnt)]
        if not temp_result.shape[0]:
            continue
        zmin[cnt] = int(npy.min(temp_result[:, 2]) + 0.5)
        zmax[cnt] = int(npy.max(temp_result[:, 2]) + 0.5)
        resampled_points[cnt] = npy.zeros([(zmax[cnt] - zmin[cnt] + 1) * nn, 4], dtype = npy.float32)
        resampled_index = 0
        
        for z in range(zmin[cnt], zmax[cnt] + 1):
            data_result = temp_result[npy.where(npy.round(temp_result[:, 2]) == z)]
            if data_result is not None:
                if data_result.shape[0] == 0:
                    continue
                
                #center_result = npy.mean(data_result[:, :2], axis = 0)
                center_result = calCentroidFromContour(data_result[:, :2])[0]
                points_result = getPointsOntheSpline(data_result, center_result, 900)
                
                i = 0
                for k in range(-nn / 2 + 1, nn / 2 + 1):
                    angle = k * 2 * npy.pi / nn 
                    
                    while i < 900 and points_result[i, 2] < angle:
                        i += 1
                    if i == 900 or (i > 0 and angle - points_result[i - 1, 2] < points_result[i, 2] - angle):
                        ind_result = i - 1
                    else:
                        ind_result = i
                    
                    resampled_points[cnt][resampled_index, :2] = points_result[ind_result, :2]
                    resampled_points[cnt][resampled_index, 2] = z
                    resampled_points[cnt][resampled_index, 3] = k + 4
                    resampled_index += 1
    trans_points = npy.array(ori_points)
    for cnt in range(3):
        for k in range(0, nn):
            data = resampled_points[cnt][npy.where(npy.round(resampled_points[cnt][:, -1]) == k)]
            count = data.shape[0]
            if count == 0:
                continue
            points = vtk.vtkPoints()
            for i in range(count):
                points.InsertPoint(i, data[i, 0], data[i, 1], data[i, 2])
    
            para_spline = vtk.vtkParametricSpline()
            para_spline.SetPoints(points)
            para_spline.ClosedOff()
            
            deltaz = 1.0 / multiple
            old_pt = [0.0, 0.0, 0.0]
            numberOfOutputPoints = int((zmax[cnt] - zmin[cnt] + 1) * 10)
            i = 0
            
            for z in range(zmin[cnt], zmax[cnt] + 1):
                znow = float(z)
                for dd in range(1, multiple):
                    znow += deltaz 
                    while i < numberOfOutputPoints:
                        t = i * 1.0 / numberOfOutputPoints
                        pt = [0.0, 0.0, 0.0]
                        para_spline.Evaluate([t, t, t], pt, [0] * 9)
                        if pt[2] >= znow:
                            if pt[2] - znow < znow - old_pt[2]:
                                new_point = pt
                            else:
                                new_point = old_pt
                            trans_points = npy.append(trans_points, [[new_point[0], new_point[1], znow, cnt]], axis = 0)
                            
                            old_pt = pt
                            break
                        i += 1
    return trans_points
Esempio n. 10
0
def resliceTheResultPoints(moving_points, moving_center, nn, moving_res, fixed_res, discard, R, T, C = npy.asmatrix([0, 0, 0]).T):
    resampled_points = [None, None, None]
    nearbif_points = [None, None, None]
    nearbif_center = [None, None]
    bif_slice = 0
    
    for cnt in range(3):
        temp_result = moving_points[npy.where(npy.round(moving_points[:, -1]) == cnt)].copy()
        if not temp_result.shape[0]:
            continue
        zmin = int(npy.min(temp_result[:, 2]) + 0.5)
        zmax = int(npy.max(temp_result[:, 2]) + 0.5)
        
        resampled_points[cnt] = npy.zeros([(zmax - zmin + 1) * nn, 4], dtype = npy.float32)
        resampled_index = 0
        
        for z in range(zmin, zmax + 1):
            data_result = temp_result[npy.where(npy.round(temp_result[:, 2]) == z)]
            if data_result is not None:
                if data_result.shape[0] == 0:
                    continue
                
                center_result = calCentroidFromContour(data_result[:, :2])[0]
                points_result = getPointsOntheSpline(data_result, center_result, 900)
                if cnt > 0 and z == zmin:
                    nearbif_points[cnt] = points_result
                    nearbif_center[cnt - 1] = center_result
                    bif_slice = z
                elif cnt == 0 and z == zmax - 1:
                    nearbif_points[cnt] = points_result
                    
                i = 0
                for k in range(- nn / 2 + 1, nn / 2 + 1):
                    angle = k * 360 / nn / 180.0 * npy.pi
                    
                    while i < 900 and points_result[i, 2] < angle:
                        i += 1
                    if i == 900 or (i > 0 and angle - points_result[i - 1, 2] < points_result[i, 2] - angle):
                        ind_result = i - 1
                    else:
                        ind_result = i
                    
                    resampled_points[cnt][resampled_index, :2] = points_result[ind_result, :2]
                    resampled_points[cnt][resampled_index, 2] = z
                    resampled_points[cnt][resampled_index, 3] = k + nn / 2 - 1
                    resampled_index += 1
    
    nearbif_angle = [npy.arctan2(nearbif_center[1][1] - nearbif_center[0][1], nearbif_center[1][0] - nearbif_center[0][0]), 
                     npy.arctan2(nearbif_center[0][1] - nearbif_center[1][1], nearbif_center[0][0] - nearbif_center[1][0])]
    point_near_bif = npy.zeros([2, 2], dtype = npy.float32)
    for cnt in range(2):
        ind = npy.argmin(npy.abs(nearbif_points[cnt + 1][:, 2] - nearbif_angle[cnt]))
        point_near_bif[cnt, :] = nearbif_points[cnt + 1][ind, :2]
    bif_points = npy.zeros([3, 3], dtype = npy.float32)
    bif_points[0, :2] = npy.mean(point_near_bif, axis = 0)
    bif_points[0, 2] = bif_slice
    nearbif_angle = [npy.arctan2(nearbif_center[1][0] - nearbif_center[0][0], nearbif_center[0][1] - nearbif_center[1][1]), 
                     npy.arctan2(nearbif_center[0][0] - nearbif_center[1][0], nearbif_center[1][1] - nearbif_center[0][1])]
    nearbif_points[0][:, 2] = npy.arctan2(nearbif_points[0][:, 1] - bif_points[0, 1], nearbif_points[0][:, 0] - bif_points[0, 0])
    for cnt in range(1, 3):
        ind = npy.argmin(npy.abs(nearbif_points[0][:, 2] - nearbif_angle[cnt - 1]))
        bif_points[cnt, :2] = nearbif_points[0][ind, :2]
    bif_points[1:, 2] = bif_slice - 1
    
    # Apply the transformation on the resampled points
    for cnt in range(3):
        resampled_points[cnt][:, :3] = applyTransformForPoints(resampled_points[cnt][:, :3], moving_res, fixed_res, R, T, C)
    bif_points = applyTransformForPoints(bif_points, moving_res, fixed_res, R, T, C)

    # Resample the points near the bifurcation
    points = vtk.vtkPoints()
    points.InsertPoint(0, bif_points[1, 0], bif_points[1, 1], bif_points[1, 2])
    points.InsertPoint(1, bif_points[0, 0], bif_points[0, 1], bif_points[0, 2])
    points.InsertPoint(2, bif_points[2, 0], bif_points[2, 1], bif_points[2, 2])

    para_spline = vtk.vtkParametricSpline()
    para_spline.SetPoints(points)
    para_spline.ClosedOff()
    
    numberOfOutputPoints = 6
    new_bif_points = npy.zeros([numberOfOutputPoints + 1, 4], dtype = npy.float32)
    
    for i in range(0, numberOfOutputPoints + 1):
        t = i * 1.0 / numberOfOutputPoints
        pt = [0.0, 0.0, 0.0]
        para_spline.Evaluate([t, t, t], pt, [0] * 9)
        new_bif_points[i, :3] = pt
    new_bif_points[:, 3] = 0
    
    # Reslice the result points
    trans_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32)
    bif_slice = int(npy.ceil(bif_points[0, 2]))
    
    for cnt in range(3):
        zmin = int(npy.ceil(npy.max(resampled_points[cnt][:nn, 2])))
        zmax = int(npy.min(resampled_points[cnt][-nn:, 2]))
        if not discard:
            if cnt == 0:
                zmax = bif_slice
            else:
                zmin = bif_slice
        
        for k in range(0, nn):
            data = resampled_points[cnt][npy.where(npy.round(resampled_points[cnt][:, -1]) == k)]
            if not discard:
                if cnt == 0:
                    dis1 = npy.hypot(data[-1, 0] - resampled_points[1][nn:nn * 2, 0], data[-1, 1] - resampled_points[1][nn:nn * 2, 1])
                    #dis1 = npy.hypot(data[-1, 0] - nearbif_points[1][:, 0], data[-1, 1] - nearbif_points[1][:, 1])
                    ind1 = npy.argmin(dis1)
                    dis2 = npy.hypot(data[-1, 0] - resampled_points[2][nn:nn * 2, 0], data[-1, 1] - resampled_points[2][nn:nn * 2, 1])
                    ind2 = npy.argmin(dis2)
                    if dis1[ind1] < dis2[ind2]:
                        data_add = resampled_points[1][npy.where((npy.round(resampled_points[1][:, -1]) == ind1) & (resampled_points[1][:, 2] <= zmax + 1))]
                    else:
                        data_add = resampled_points[2][npy.where((npy.round(resampled_points[2][:, -1]) == ind2) & (resampled_points[2][:, 2] <= zmax + 1))]
                    data = npy.append(data, data_add[1:, :], axis = 0)
                else:
                    dis1 = npy.hypot(data[0, 0] - resampled_points[0][-nn * 2:-nn, 0], data[0, 1] - resampled_points[0][-nn * 2:-nn, 1])
                    ind1 = npy.argmin(dis1)
                    dis2 = npy.sqrt(npy.sum((new_bif_points[:, :3] - data[0, :3]) ** 2, axis = 1))
                    ind2 = npy.argmin(dis2)
                    if dis1[ind1] < dis2[ind2]:
                        data_add = resampled_points[0][npy.where((npy.round(resampled_points[0][:, -1]) == ind1) & (resampled_points[0][:, 2] >= zmin - 1))]
                    else:
                        data_add = new_bif_points[ind2, :].reshape(1, -1)
                    data = npy.append(data_add[:-1, :], data, axis = 0)
                
            count = data.shape[0]
            if count == 0:
                continue
            points = vtk.vtkPoints()
            for i in range(count):
                points.InsertPoint(i, data[i, 0], data[i, 1], data[i, 2])
    
            para_spline = vtk.vtkParametricSpline()
            para_spline.SetPoints(points)
            para_spline.ClosedOff()
            
            znow = zmin
            old_pt = [0.0, 0.0, 0.0]
            numberOfOutputPoints = int((zmax - zmin + 3) * nn)
            
            for i in range(0, numberOfOutputPoints):
                t = i * 1.0 / numberOfOutputPoints
                pt = [0.0, 0.0, 0.0]
                para_spline.Evaluate([t, t, t], pt, [0] * 9)
                if pt[2] >= znow:
                    if pt[2] - znow < znow - old_pt[2]:
                        new_point = pt
                    else:
                        new_point = old_pt
                    trans_points = npy.append(trans_points, [[new_point[0], new_point[1], znow, cnt]], axis = 0)
                    znow += 1
                    if znow > zmax:
                        break
                old_pt = pt
    
    new_trans_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32)
    for cnt in range(3):
        temp_result = trans_points[npy.where(npy.round(trans_points[:, -1]) == cnt)]
        if not temp_result.shape[0]:
            continue
        zmin = int(npy.min(temp_result[:, 2]) + 0.5)
        zmax = int(npy.max(temp_result[:, 2]) + 0.5)
        for z in range(zmin, zmax + 1):
            data_result = temp_result[npy.where(npy.round(temp_result[:, 2]) == z)]
            if data_result is not None:
                if data_result.shape[0] == 0:
                    continue
                
                ind = sortContourPoints(data_result)
                data_result[:, :] = data_result[ind, :]

                for x in data_result:
                    if isDifferent(new_trans_points[-1, :], x):
                        new_trans_points = npy.append(new_trans_points, x.reshape(1, -1), axis = 0)
                    
                        
    result_center_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32)
    if moving_center is not None and moving_center.shape[0] > 1:
        result_center = moving_center[npy.where(moving_center[:, 0] >= 0)]
        
        result_center[:, :3] = applyTransformForPoints(result_center[:, :3], moving_res, fixed_res, R, T, C)
        ind = result_center[:, 2].argsort()
        result_center = result_center[ind]
        
        result_center_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32)
        for cnt in range(3):
            resampled_points = result_center[npy.where(npy.round(result_center[:, -1]) == cnt)]
            zmin = int(npy.ceil(resampled_points[0, 2]))
            zmax = int(resampled_points[-1, 2])
            
            count = resampled_points.shape[0]
            points = vtk.vtkPoints()
            for i in range(count):
                points.InsertPoint(i, resampled_points[i, 0], resampled_points[i, 1], resampled_points[i, 2])
    
            para_spline = vtk.vtkParametricSpline()
            para_spline.SetPoints(points)
            para_spline.ClosedOff()
            
            znow = zmin
            old_pt = [0.0, 0.0, 0.0]
            numberOfOutputPoints = int((zmax - zmin + 1) * 10)
            
            for i in range(0, numberOfOutputPoints):
                t = i * 1.0 / numberOfOutputPoints
                pt = [0.0, 0.0, 0.0]
                para_spline.Evaluate([t, t, t], pt, [0] * 9)
                if pt[2] >= znow:
                    if pt[2] - znow < znow - old_pt[2]:
                        new_point = pt
                    else:
                        new_point = old_pt
                    result_center_points = npy.append(result_center_points, [[new_point[0], new_point[1], znow, cnt]], axis = 0)
                    znow += 1
                    if znow > zmax:
                        break
                old_pt = pt
    return new_trans_points, result_center_points
Esempio n. 11
0
 def analysis(self, data, point_data_fix = None, all = 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()
     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])
     mean_dis = npy.array([0.0, 0.0, 0.0])
     max_dis = npy.array([0.0, 0.0, 0.0])
     square_sum_dis = npy.array([0.0, 0.0, 0.0])
     if all:
         result = [{}, {}, {}]
         bif = db.getBifurcation(point_data_fix)
     
     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.max([npy.min(temp_result[:, 2]), npy.min(temp_fix[:, 2])]) + 0.5)
         zmax = int(npy.min([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, 900)
                 points_result = getPointsOntheSpline(data_result, center_result, 900)
                 
                 i = j = 0
                 for k in range(-44, 46):
                     angle = k * 4 / 180.0 * npy.pi
                     while i < 900 and points_fix[i, 2] < angle:
                         i += 1
                     if i == 900 or (i > 0 and angle - points_fix[i - 1, 2] < points_fix[i, 2] - angle):
                         ind_fix = i - 1
                     else:
                         ind_fix = i
                     while j < 900 and points_result[j, 2] < angle:
                         j += 1
                     if j == 900 or (j > 0 and angle - points_result[j - 1, 2] < points_result[j, 2] - angle):
                         ind_result = j - 1
                     else:
                         ind_result = j
                     temp_dis = npy.hypot(points_fix[ind_fix, 0] - points_result[ind_result, 0], points_fix[ind_fix, 1] - points_result[ind_result, 1])
                     max_dis[cnt] = npy.max([max_dis[cnt], temp_dis])
                     mean_dis[cnt] += temp_dis
                     square_sum_dis[cnt] += temp_dis ** 2
                     if all:
                         result[cnt][z - bif] = result[cnt].get(z - bif, 0) + temp_dis
     
     cnt_total = npy.sum(cnt_num)
     sd = npy.sqrt(npy.max([(square_sum_dis - mean_dis ** 2 / (90 * cnt_num)) / (90 * cnt_num - 1), [0, 0, 0]], axis = 0))
     sd[sd != sd] = 0
     sd_all = npy.sqrt(npy.max([(npy.sum(square_sum_dis) - npy.sum(mean_dis) ** 2 / (90 * cnt_total)) / (90 * cnt_total - 1), 0]))
     
     mean_dis /= 90
     mean_whole = npy.sum(mean_dis)
     mean_dis /= cnt_num
     mean_dis[mean_dis != mean_dis] = 0 # Replace the NAN in the mean distance
     
     if self.gui is not None:
         message = "Error on Vessel 0: %0.2fmm (SD = %0.2fmm, Total %d slices)\nError on Vessel 1: %0.2fmm (SD = %0.2fmm, Total %d slices)\nError on Vessel 2: %0.2fmm (SD = %0.2fmm, Total %d slices)\nWhole Error: %0.2fmm (SD = %0.2fmm, Total %d slices)\n" \
             % (mean_dis[0], sd[0], cnt_num[0], mean_dis[1], sd[1], cnt_num[1], mean_dis[2], sd[2], cnt_num[2], mean_whole / cnt_total, sd_all, cnt_total) + \
             "-----------------------------------------------------------------------------\n" + \
             "Max Error on Vessel 0: %0.2fmm\nMax Error on Vessel 1: %0.2fmm\nMax Error on Vessel 2: %0.2fmm\nTotal Max Error: %0.2fmm" \
             % (max_dis[0], max_dis[1], max_dis[2], npy.max(max_dis));
         self.gui.showErrorMessage("Contour Registration Error", message)
         
     if not all:
         return mean_dis, mean_whole / cnt_total, max_dis, npy.max(max_dis)
     else:
         for cnt in range(3):
             for x in result[cnt].keys():
                 result[cnt][x] /= 90
         return mean_dis, mean_whole / cnt_total, max_dis, npy.max(max_dis), result