def process(self, dataset, i, k): print 'Register Data %s with ICP(centerline)...(Type %d)' % ( self.ini.file.name_result[i], k) dataset['fix'].pointSet.data['Centerline'] = calCenterlineFromContour( dataset['fix'].pointSet.data, k, dataset['fix'].getData()) dataset['mov'].pointSet.data['Centerline'] = calCenterlineFromContour( dataset['mov'].pointSet.data, k, dataset['mov'].getData()) data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole for j in range(3): self.sheet.write(j + 1 + 4 * k, i + 2, float(mean_dis[j])) self.sheet.write(4 + 4 * k, i + 2, mean_whole) self.book.save('./Result/Center_merge.xls') del data, point, resultData
def load(self, i): dataset = {'mov': [], 'fix': []} data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/' + self.ini.file.name_mov[i] + '.mat', None) point['Centerline'] = calCenterlineFromContour(point) dataset['mov'] = db.BasicData(data, info, point) data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/' + self.ini.file.name_fix[i] + '.mat', None) point['Centerline'] = calCenterlineFromContour(point) dataset['fix'] = db.BasicData(data, info, point) print 'Data %s loaded!' % self.ini.file.name_result[i] return dataset
def load(self, k, i): dataset = {'mov': [], 'fix': []} data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/' + self.ini.file.name_fix[i] + '.mat', None) point['Centerline'] = calCenterlineFromContour(point) fileData = db.BasicData(data, info, point) dataset['fix'] = fileData data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/' + self.ini.file.name_mov[i] + '.mat', None) point['Centerline'] = calCenterlineFromContour(point) fileData = db.BasicData(data, info, point) dataset['mov'] = fileData print 'Data %s loaded!' % self.ini.file.name_result[i] return dataset
def process(self, dataset, i, k): print 'Register Data %s with ICP(centerline)...(Type %d)' % (self.ini.file.name_result[i], k) dataset['fix'].pointSet.data['Centerline'] = calCenterlineFromContour(dataset['fix'].pointSet.data, k, dataset['fix'].getData()) dataset['mov'].pointSet.data['Centerline'] = calCenterlineFromContour(dataset['mov'].pointSet.data, k, dataset['mov'].getData()) data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole for j in range(3): self.sheet.write(j + 1 + 4 * k, i + 2, float(mean_dis[j])) self.sheet.write(4 + 4 * k, i + 2, mean_whole) self.book.save('./Result/Center_merge.xls') del data, point, resultData
def process(self, dataset, i): 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 ' ' # Segmentation of data print 'Segment Data %s...' % self.ini.file.name_result[i] tmp = dataset['fix'].pointSet.data['Contour'] tmp = tmp[tmp[:, 0] >= 0] self.new_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32) bottom = int(npy.round(npy.min(tmp[:, 2]))) bif = int(db.getBifurcation(tmp) + 0.5) up = int(npy.round(npy.max(tmp[:, 2]))) bottom += (bif - bottom) / 2 up -= (up - bif) / 2 point_vital = [0] * 3 point_vital[0] = tmp[(npy.round(tmp[:, -1]) == 0) & (npy.round(tmp[:, 2]) == bottom)].copy() point_vital[1] = tmp[(npy.round(tmp[:, -1]) == 1) & (npy.round(tmp[:, 2]) == up)].copy() point_vital[2] = tmp[(npy.round(tmp[:, -1]) == 2) & (npy.round(tmp[:, 2]) == up)].copy() autoDetectContour(point_vital[0], 0, bottom, bif, 1, dataset['fix'].getResolution().tolist(), 'fix') autoDetectContour(point_vital[1], 1, up, bif, -1, dataset['fix'].getResolution().tolist(), 'fix') autoDetectContour(point_vital[2], 2, up, bif, -1, dataset['fix'].getResolution().tolist(), 'fix') print ' ' print 'Finish segmentation for fix data. ' pointset = {'Contour': self.new_points} dataset['fix'].pointSet.data['Centerline'] = calCenterlineFromContour( pointset) self.new_points_fix = self.new_points.copy() # For mov data tmp = dataset['mov'].pointSet.data['Contour'] tmp = tmp[tmp[:, 0] >= 0] self.new_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32) bottom = int(npy.round(npy.min(tmp[:, 2]))) bif = int(db.getBifurcation(tmp) + 0.5) up = int(npy.round(npy.max(tmp[:, 2]))) bottom += (bif - bottom) / 2 up -= (up - bif) / 2 point_vital = [0] * 3 point_vital[0] = tmp[(npy.round(tmp[:, -1]) == 0) & (npy.round(tmp[:, 2]) == bottom)].copy() point_vital[1] = tmp[(npy.round(tmp[:, -1]) == 1) & (npy.round(tmp[:, 2]) == up)].copy() point_vital[2] = tmp[(npy.round(tmp[:, -1]) == 2) & (npy.round(tmp[:, 2]) == up)].copy() autoDetectContour(point_vital[0], 0, bottom, bif, 1, dataset['mov'].getResolution().tolist(), 'mov') autoDetectContour(point_vital[1], 1, up, bif, -1, dataset['mov'].getResolution().tolist(), 'mov') autoDetectContour(point_vital[2], 2, up, bif, -1, dataset['mov'].getResolution().tolist(), 'mov') print ' ' print 'Finish segmentation for mov data. ' pointset = {'Contour': self.new_points} dataset['mov'].pointSet.data['Centerline'] = calCenterlineFromContour( pointset) self.new_points_mov = self.new_points.copy() # ICP with centerline without label print 'Register Data %s with ICP(centerline) without label...' % self.ini.file.name_result[ i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1, op=True) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole dice_index, dice_index_all = self.areaerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet2.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet2.write(j + 1, i + 2, mean_dis[j]) self.sheet2.write(j + 5, i + 2, max_dis[j]) self.sheet2.write(j + 9, i + 2, dice_index[j]) self.sheet2.write(4, i + 2, mean_whole) self.sheet2.write(8, i + 2, max_whole) self.sheet2.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData # ICP with centerline with label print 'Register Data %s with ICP(centerline) with label...' % self.ini.file.name_result[ i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1, op=False) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole dice_index, dice_index_all = self.areaerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet4.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet4.write(j + 1, i + 2, mean_dis[j]) self.sheet4.write(j + 5, i + 2, max_dis[j]) self.sheet4.write(j + 9, i + 2, dice_index[j]) self.sheet4.write(4, i + 2, mean_whole) self.sheet4.write(8, i + 2, max_whole) self.sheet4.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData fix_points = dataset['fix'].getPointSet('Contour').copy() dataset['fix'].pointSet.data['Contour'] = self.new_points_fix mov_points = dataset['mov'].getPointSet('Contour').copy() dataset['mov'].pointSet.data['Contour'] = self.new_points_mov print 'Saving Data %s...' % self.ini.file.name_result[i] db.saveMatData( self.path + self.ini.file.savedir + self.ini.file.name_result[i] + '_snap.mat', [dataset['fix']], 0) db.saveMatData( self.path + self.ini.file.savedir + self.ini.file.name_result[i] + '_merge.mat', [dataset['mov']], 0) print 'Done!' # ICP with contour without label print 'Register Data %s with ICP(contour) without label...' % self.ini.file.name_result[ i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0, op=False) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, fix_points.copy(), mov_points.copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole para = resultData.info.getData('transform') R = ml.mat(para[:9]).reshape(3, 3) T = ml.mat(para[9:12]).T T = R.I * T T = -T tmp_con, result_center_points = util.resliceTheResultPoints( mov_points, None, 20, dataset['mov'].getResolution().tolist(), dataset['fix'].getResolution().tolist(), False, R, T) resultData.pointSet.data['Contour'] = tmp_con dice_index, dice_index_all = self.areaerror.analysis( resultData, fix_points.copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet1.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet1.write(j + 1, i + 2, mean_dis[j]) self.sheet1.write(j + 5, i + 2, max_dis[j]) self.sheet1.write(j + 9, i + 2, dice_index[j]) self.sheet1.write(4, i + 2, mean_whole) self.sheet1.write(8, i + 2, max_whole) self.sheet1.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData # ICP with contour with label print 'Register Data %s with ICP(contour) with label...' % self.ini.file.name_result[ i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0, op=True) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, fix_points.copy(), mov_points.copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole para = resultData.info.getData('transform') R = ml.mat(para[:9]).reshape(3, 3) T = ml.mat(para[9:12]).T T = R.I * T T = -T tmp_con, result_center_points = util.resliceTheResultPoints( mov_points, None, 20, dataset['mov'].getResolution().tolist(), dataset['fix'].getResolution().tolist(), False, R, T) resultData.pointSet.data['Contour'] = tmp_con dice_index, dice_index_all = self.areaerror.analysis( resultData, fix_points.copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet3.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet3.write(j + 1, i + 2, mean_dis[j]) self.sheet3.write(j + 5, i + 2, max_dis[j]) self.sheet3.write(j + 9, i + 2, dice_index[j]) self.sheet3.write(4, i + 2, mean_whole) self.sheet3.write(8, i + 2, max_whole) self.sheet3.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData del self.new_points, fix_points, self.new_points_fix, self.new_points_mov, mov_points, tmp_con, result_center_points
def process(self, dataset, i): 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 ' ' # Segmentation of data print 'Segment Data %s...' % self.ini.file.name_result[i] tmp = dataset['fix'].pointSet.data['Contour'] tmp = tmp[tmp[:, 0] >= 0] self.new_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32) time1 = time.time() bottom = int(npy.round(npy.min(tmp[:, 2]))) bif = int(db.getBifurcation(tmp) + 0.5) up = int(npy.round(npy.max(tmp[:, 2]))) point_vital = [0] * 3 point_vital[0] = tmp[(npy.round(tmp[:, -1]) == 0) & (npy.round(tmp[:, 2]) == bottom)].copy() point_vital[1] = tmp[(npy.round(tmp[:, -1]) == 1) & (npy.round(tmp[:, 2]) == up)].copy() point_vital[2] = tmp[(npy.round(tmp[:, -1]) == 2) & (npy.round(tmp[:, 2]) == up)].copy() autoDetectContour(point_vital[0], 0, bottom, bif, 1, dataset['fix'].getResolution().tolist()) autoDetectContour(point_vital[1], 1, up, bif, -1, dataset['fix'].getResolution().tolist()) autoDetectContour(point_vital[2], 2, up, bif, -1, dataset['fix'].getResolution().tolist()) print ' ' print 'Finish segmentation of MR. ' true_fixed_points = dataset['fix'].pointSet.data['Contour'].copy() dataset['fix'].pointSet.data['Contour'] = self.new_points dataset['fix'].pointSet.data['Centerline'] = calCenterlineFromContour(dataset['fix'].pointSet.data) hybrid = NonrigidHybridRegistration(None) print 'Register Data %s with Hybrid Method...' % (self.ini.file.name_result[i]) data, point, para = hybrid.register(dataset['fix'], dataset['mov'], regPara = self.regPara, true_fixed_points = true_fixed_points) print 'Done!' for k in range(len(self.regPara)): self.sheet1.write(k + 1, i + 2, float(para[k, 0])) self.sheet2.write(k + 1, i + 2, float(para[k, 1])) self.sheet3.write(k + 1, i + 2, float(para[k, 2])) self.sheet1.write(0, i + 2, self.ini.file.name_result[i]) self.sheet2.write(0, i + 2, self.ini.file.name_result[i]) self.sheet3.write(0, i + 2, self.ini.file.name_result[i]) self.book.save(self.path + self.ini.file.savedir + 'nonrigid' + str(self.first) + '.xls') del para del hybrid del true_fixed_points
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)
def process(self, dataset, i): # ICP with centerline print 'Register Data %s with ICP...' % self.ini.file.name_result[i] tmp = dataset['mov'].pointSet.data['Contour'].copy() for sd in range(0, 16): mean_dis_all = npy.zeros([4, 3], dtype=npy.float32) mean_whole_all = npy.zeros([4, 1], dtype=npy.float32) if sd > 0: repeat = self.repeat else: repeat = 1 for i in range(repeat): dataset['mov'].pointSet.data['Contour'] = AddNoise( tmp, float(sd) / 5) dataset['mov'].pointSet.data[ 'Centerline'] = calCenterlineFromContour( dataset['mov'].pointSet.data) # Centerline label data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1) resultData = db.ResultData( data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) mean_dis_all[0, :] += mean_dis mean_whole_all[0] += mean_whole del data, point, resultData, para # Contour label data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0, op=True) resultData = db.ResultData( data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) mean_dis_all[1, :] += mean_dis mean_whole_all[1] += mean_whole del data, point, resultData, para # Centerline no-label data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1, op=True) resultData = db.ResultData( data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) mean_dis_all[2, :] += mean_dis mean_whole_all[2] += mean_whole del data, point, resultData, para # Contour no-label data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0) resultData = db.ResultData( data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) mean_dis_all[3, :] += mean_dis mean_whole_all[3] += mean_whole del data, point, resultData, para sys.stdout.write(str(i) + ',') sys.stdout.flush() mean_dis_all /= repeat mean_whole_all /= repeat print ' ' print 'Noise level %fmm Done!' % (float(sd) / 5) print 'Contour Error Done! Whole mean is %0.2fmm vs %0.2fmm.' % ( mean_whole_all[0], mean_whole_all[1]) for i in range(4): self.error[i, sd, :3] += mean_dis_all[i, :] self.error[i, sd, 3] += mean_whole_all[i]
def process(self, dataset, i): 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 ' ' # Segmentation of data print 'Segment Data %s...' % self.ini.file.name_result[i] tmp = dataset['fix'].pointSet.data['Contour'] tmp = tmp[tmp[:, 0] >= 0] self.new_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32) bottom = int(npy.round(npy.min(tmp[:, 2]))) bif = int(db.getBifurcation(tmp) + 0.5) up = int(npy.round(npy.max(tmp[:, 2]))) bottom += (bif - bottom) / 2 up -= (up - bif) / 2 point_vital = [0] * 3 point_vital[0] = tmp[(npy.round(tmp[:, -1]) == 0) & (npy.round(tmp[:, 2]) == bottom)].copy() point_vital[1] = tmp[(npy.round(tmp[:, -1]) == 1) & (npy.round(tmp[:, 2]) == up)].copy() point_vital[2] = tmp[(npy.round(tmp[:, -1]) == 2) & (npy.round(tmp[:, 2]) == up)].copy() autoDetectContour(point_vital[0], 0, bottom, bif, 1, dataset['fix'].getResolution().tolist(), 'fix') autoDetectContour(point_vital[1], 1, up, bif, -1, dataset['fix'].getResolution().tolist(), 'fix') autoDetectContour(point_vital[2], 2, up, bif, -1, dataset['fix'].getResolution().tolist(), 'fix') print ' ' print 'Finish segmentation for fix data. ' pointset = {'Contour': self.new_points} dataset['fix'].pointSet.data['Centerline'] = calCenterlineFromContour(pointset) self.new_points_fix = self.new_points.copy() # For mov data tmp = dataset['mov'].pointSet.data['Contour'] tmp = tmp[tmp[:, 0] >= 0] self.new_points = npy.array([[-1, -1, -1, -1]], dtype = npy.float32) bottom = int(npy.round(npy.min(tmp[:, 2]))) bif = int(db.getBifurcation(tmp) + 0.5) up = int(npy.round(npy.max(tmp[:, 2]))) bottom += (bif - bottom) / 2 up -= (up - bif) / 2 point_vital = [0] * 3 point_vital[0] = tmp[(npy.round(tmp[:, -1]) == 0) & (npy.round(tmp[:, 2]) == bottom)].copy() point_vital[1] = tmp[(npy.round(tmp[:, -1]) == 1) & (npy.round(tmp[:, 2]) == up)].copy() point_vital[2] = tmp[(npy.round(tmp[:, -1]) == 2) & (npy.round(tmp[:, 2]) == up)].copy() autoDetectContour(point_vital[0], 0, bottom, bif, 1, dataset['mov'].getResolution().tolist(), 'mov') autoDetectContour(point_vital[1], 1, up, bif, -1, dataset['mov'].getResolution().tolist(), 'mov') autoDetectContour(point_vital[2], 2, up, bif, -1, dataset['mov'].getResolution().tolist(), 'mov') print ' ' print 'Finish segmentation for mov data. ' pointset = {'Contour': self.new_points} dataset['mov'].pointSet.data['Centerline'] = calCenterlineFromContour(pointset) self.new_points_mov = self.new_points.copy() # ICP with centerline without label print 'Register Data %s with ICP(centerline) without label...' % self.ini.file.name_result[i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1, op = True) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole dice_index, dice_index_all = self.areaerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet2.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet2.write(j + 1, i + 2, mean_dis[j]) self.sheet2.write(j + 5, i + 2, max_dis[j]) self.sheet2.write(j + 9, i + 2, dice_index[j]) self.sheet2.write(4, i + 2, mean_whole) self.sheet2.write(8, i + 2, max_whole) self.sheet2.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData # ICP with centerline with label print 'Register Data %s with ICP(centerline) with label...' % self.ini.file.name_result[i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1, op = False) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Contour').copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole dice_index, dice_index_all = self.areaerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet4.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet4.write(j + 1, i + 2, mean_dis[j]) self.sheet4.write(j + 5, i + 2, max_dis[j]) self.sheet4.write(j + 9, i + 2, dice_index[j]) self.sheet4.write(4, i + 2, mean_whole) self.sheet4.write(8, i + 2, max_whole) self.sheet4.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData fix_points = dataset['fix'].getPointSet('Contour').copy() dataset['fix'].pointSet.data['Contour'] = self.new_points_fix mov_points = dataset['mov'].getPointSet('Contour').copy() dataset['mov'].pointSet.data['Contour'] = self.new_points_mov print 'Saving Data %s...' % self.ini.file.name_result[i] db.saveMatData(self.path + self.ini.file.savedir + self.ini.file.name_result[i] + '_snap.mat', [dataset['fix']], 0) db.saveMatData(self.path + self.ini.file.savedir + self.ini.file.name_result[i] + '_merge.mat', [dataset['mov']], 0) print 'Done!' # ICP with contour without label print 'Register Data %s with ICP(contour) without label...' % self.ini.file.name_result[i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0, op = False) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis(resultData, fix_points.copy(), mov_points.copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole para = resultData.info.getData('transform') R = ml.mat(para[:9]).reshape(3, 3) T = ml.mat(para[9:12]).T T = R.I * T T = -T tmp_con, result_center_points = util.resliceTheResultPoints(mov_points, None, 20, dataset['mov'].getResolution().tolist(), dataset['fix'].getResolution().tolist(), False, R, T) resultData.pointSet.data['Contour'] = tmp_con dice_index, dice_index_all = self.areaerror.analysis(resultData, fix_points.copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet1.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet1.write(j + 1, i + 2, mean_dis[j]) self.sheet1.write(j + 5, i + 2, max_dis[j]) self.sheet1.write(j + 9, i + 2, dice_index[j]) self.sheet1.write(4, i + 2, mean_whole) self.sheet1.write(8, i + 2, max_whole) self.sheet1.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData # ICP with contour with label print 'Register Data %s with ICP(contour) with label...' % self.ini.file.name_result[i] data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0, op = True) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.surfaceerror.analysis(resultData, fix_points.copy(), mov_points.copy(), dataset['mov'].getPointSet('Mask').copy(), dataset['mov'].getResolution().tolist()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole para = resultData.info.getData('transform') R = ml.mat(para[:9]).reshape(3, 3) T = ml.mat(para[9:12]).T T = R.I * T T = -T tmp_con, result_center_points = util.resliceTheResultPoints(mov_points, None, 20, dataset['mov'].getResolution().tolist(), dataset['fix'].getResolution().tolist(), False, R, T) resultData.pointSet.data['Contour'] = tmp_con dice_index, dice_index_all = self.areaerror.analysis(resultData, fix_points.copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet3.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet3.write(j + 1, i + 2, mean_dis[j]) self.sheet3.write(j + 5, i + 2, max_dis[j]) self.sheet3.write(j + 9, i + 2, dice_index[j]) self.sheet3.write(4, i + 2, mean_whole) self.sheet3.write(8, i + 2, max_whole) self.sheet3.write(12, i + 2, dice_index_all) self.book.save(self.path + self.ini.file.savedir + 'multicontrast_seg_feature.xls') del data, point, resultData del self.new_points, fix_points, self.new_points_fix, self.new_points_mov, mov_points, tmp_con, result_center_points
def process(self, dataset, i): 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 ' ' print 'Segment Data %s...' % self.ini.file.name_result[i] tmp = dataset['fix'].pointSet.data['Contour'] tmp = tmp[tmp[:, 0] >= 0] self.new_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32) time1 = time.time() bottom = int(npy.round(npy.min(tmp[:, 2]))) bif = int(db.getBifurcation(tmp) + 0.5) up = int(npy.round(npy.max(tmp[:, 2]))) point_vital = [0] * 3 point_vital[0] = tmp[(npy.round(tmp[:, -1]) == 0) & (npy.round(tmp[:, 2]) == bottom)].copy() point_vital[1] = tmp[(npy.round(tmp[:, -1]) == 1) & (npy.round(tmp[:, 2]) == up)].copy() point_vital[2] = tmp[(npy.round(tmp[:, -1]) == 2) & (npy.round(tmp[:, 2]) == up)].copy() autoDetectContour(point_vital[0], 0, bottom, bif, 1) autoDetectContour(point_vital[1], 1, up, bif, -1) autoDetectContour(point_vital[2], 2, up, bif, -1) time2 = time.time() ''' # Use centerline for contour j = 0 for center in tmp: image = dataset['fix'].getData()[npy.round(center[2]), :, :].transpose().copy() image = (image - npy.min(image)) / (npy.max(image) - npy.min(image)) * 255 result = ac_segmentation(center[:2], image) point_array = npy.insert(result.transpose(), 2, [[center[2]],[center[3]]], axis = 1) new_points = npy.append(new_points, point_array, 0) j += 1 if j % 10 == 0: sys.stdout.write(str(j) + ',') sys.stdout.flush() ''' print ' ' print 'Done! Time for segmentation is %0.2fs' % (time2 - time1) pointset = {'Contour': self.new_points} pointset['Centerline'] = calCenterlineFromContour(pointset) print 'Saving Data %s...' % self.ini.file.name_result[i] new_data = db.BasicData(dataset['fix'].data, db.ImageInfo(dataset['fix'].info.data), pointset) db.saveMatData( self.savepath + self.ini.file.name_result[i] + '_mr.mat', [new_data], 0) print 'Done!' mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis( new_data, dataset['fix'].getPointSet('Contour').copy()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole self.sheet1.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet1.write(j + 1, i + 2, mean_dis[j]) self.sheet1.write(4, i + 2, mean_whole) self.sheet1.write(5, i + 2, time2 - time1) self.book.save(self.path + self.ini.file.savedir + 'Test_segmentation_final_refined.xls') # ICP with centerline print 'Register Data %s with ICP(centerline)...' % self.ini.file.name_result[ i] time1 = time.time() data, point, para = self.icp.register(new_data, dataset['mov'], 1) time2 = time.time() print 'Done! Time for registration is %0.2fs' % (time2 - time1) resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point) resultData.info.addData('fix', 1) resultData.info.addData('move', 2) resultData.info.addData('transform', para) mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy()) print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole dice_index, dice_index_all = self.areaerror.analysis( resultData, dataset['fix'].getPointSet('Contour').copy()) print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all self.sheet2.write(0, i + 2, self.ini.file.name_result[i]) for j in range(3): self.sheet2.write(j + 1, i + 2, mean_dis[j]) self.sheet2.write(j + 5, i + 2, max_dis[j]) self.sheet2.write(j + 9, i + 2, dice_index[j]) self.sheet2.write(4, i + 2, mean_whole) self.sheet2.write(8, i + 2, max_whole) self.sheet2.write(12, i + 2, dice_index_all) self.sheet2.write(13, i + 2, time2 - time1) self.book.save(self.path + self.ini.file.savedir + 'Test_segmentation_final_refined2.xls') del data, point, resultData