def extract_candidates(predictions_scan, annotations, tf_matrix, pid, outputs_path): print 'computing blobs' start_time = time.time() blobs = blobs_detection.blob_dog(predictions_scan[0, 0], min_sigma=1, max_sigma=15, threshold=0.1) print 'blobs computation time:', (time.time() - start_time) / 60. print 'n_blobs detected', len(blobs) correct_blobs_idxs = [] for zyxd in annotations: r = zyxd[-1] / 2. distance2 = ((zyxd[0] - blobs[:, 0]) ** 2 + (zyxd[1] - blobs[:, 1]) ** 2 + (zyxd[2] - blobs[:, 2]) ** 2) blob_idx = np.argmin(distance2) print 'node', zyxd print 'closest blob', blobs[blob_idx] if distance2[blob_idx] <= r ** 2: correct_blobs_idxs.append(blob_idx) else: print 'not detected !!!' # we will save blobs the the voxel space of the original image # blobs that are true detections will have blobs[-1] = 1 else 0 blobs_original_voxel_coords = [] for j in xrange(blobs.shape[0]): blob_j = np.append(blobs[j, :3], [1]) blob_j_original = tf_matrix.dot(blob_j) blob_j_original[-1] = 1 if j in correct_blobs_idxs else 0 if j in correct_blobs_idxs: print 'blob in original', blob_j_original blobs_original_voxel_coords.append(blob_j_original) blobs = np.asarray(blobs_original_voxel_coords) utils.save_pkl(blobs, outputs_path + '/%s.pkl' % pid)
def extract_candidates(predictions_scan, tf_matrix, pid, outputs_path): print 'computing blobs' start_time = time.time() blobs = blobs_detection.blob_dog(predictions_scan[0, 0], min_sigma=1, max_sigma=15, threshold=0.1) print 'blobs computation time:', (time.time() - start_time) / 60. print 'n blobs detected:', blobs.shape[0] blobs_original_voxel_coords = [] for j in xrange(blobs.shape[0]): blob_j = np.append(blobs[j, :3], [1]) blob_j_original = tf_matrix.dot(blob_j) blobs_original_voxel_coords.append(blob_j_original) blobs = np.asarray(blobs_original_voxel_coords) print blobs.shape utils.save_pkl(blobs, outputs_path + '/%s.pkl' % pid)
def notateProcessor(self, notateQueue): while True: # get crop from queue crop = notateQueue.get() # blobs detection image = exposure.equalize_hist(crop["image"]) print("notateProcessor", image.shape) blobs = blobs_detection.blob_dog(image, threshold=0.3) # get radius of blobs - now we have notation notationList = np.zeros(blobs.shape) for i in range(len(blobs)): notationList[i] = self.calcRadius(blobs[i]) # get prob - now we have notation notations = [] for i in range(len(notationList)): probCube = crop["image"] note = notationList[i] z = int(note[0]) y = int(note[1]) x = int(note[2]) prob = probCube[z][y][x] / 2 r = note[3] notation = np.array([z, y, x, r, prob]) notations.append(notation) # get world coords position = crop["position"] metaFilename = crop["seriesuid"] + ".npy" meta = self.serializer.readFromNpy("meta/", metaFilename) worldCoord = meta["worldOrigin"] steps = meta["steps"] for i in range(len(notations)): notations[i] = self.calcOffset(notations[i], position, steps) notations[i] = self.calcWorldCoord(notations[i], worldCoord) # write to disk print("notateProcessor", crop["seriesuid"], crop["number"]) self.serializer.writeToNpy( "notates/", "{0}-{1}.npy".format(crop["seriesuid"], crop["number"]), notations)
def notate(self): csvPath = self.dataPath + self.phaseSubPath + "csv/" if not os.path.isdir(csvPath): os.makedirs(csvPath) with open(csvPath + "annotation.csv", "w") as file: file.write("seriesuid,coordX,coordY,coordZ,diameter_mm\n") with open(csvPath + "seriesuids.csv", "w") as file: file.write("seriesuid\n") pathList = glob(self.dataPath + self.phaseSubPath + "concat/*.npy") for path in enumerate(tqdm(pathList)): filename = os.path.basename(path[1]) seriesuid = filename.split(".")[0] print(seriesuid) data = self.serializer.readFromNpy("concat/", filename) data = np.squeeze(data) # notation filter with lung mask mask = self.serializer.readFromNpy("mask/", filename) data = data * mask # blob dectection data = exposure.equalize_hist(data) blobs = blobs_detection.blob_dog(data, threshold=0.3, min_sigma=self.sqrt3) # blobs = feature.blob_dog(data, threshold = 0.3) # blobs = feature.blob_log(data) # blobs = feature.blob_doh(data) # get radius of blobs - now we have notation notations = np.zeros(blobs.shape) for i in range(len(blobs)): notations[i] = self.calcRadius(blobs[i]) print(notations) print(len(notations)) # convert to world coord mhdFile = os.path.join(self.dataPath, self.phaseSubPath, "raw/", seriesuid + ".mhd") rawImage = sitk.ReadImage(mhdFile) worldOrigin = np.array(rawImage.GetOrigin())[::-1] for i in range(len(notations)): notations[i] = self.calcWorldCoord(notations[i], worldOrigin) # write notations to csv csvPath = self.dataPath + self.phaseSubPath + "csv/" if not os.path.isdir(csvPath): os.makedirs(csvPath) with open(csvPath + "annotation.csv", "w+") as file: file.write("seriesuid,coordX,coordY,coordZ,diameter_mm\n") for i in range(len(notations)): line = "{0},{1},{2},{3},{4}\n".format( seriesuid, notations[i][2], notations[i][1], notations[i][0], notations[i][3] * 2.0) file.write(line) with open(csvPath + "seriesuids.csv", "w+") as file: file.write("seriesuid") for i in range(len(notations)): file.write(seriesuid)