def _get_roi_cnts(img, x, y, roi_size, thresh, max_area): worm_img, roi_corner = getWormROI(img, x, y, roi_size) worm_mask, worm_cnt, _ = getWormMask(worm_img, thresh, min_blob_area=max_area ) if worm_cnt.size > 0: worm_cnt += roi_corner return worm_cnt
def _roi2feats(block): #from a output = [] for irow, (roi_image, roi_corner) in block: row_data = trajectories_data.loc[irow] blob_mask, blob_cnt, _ = getWormMask( roi_image, row_data['threshold'], strel_size, min_blob_area=row_data['area'] / 2, is_light_background=is_light_background) feats = _getBlobFeatures(blob_cnt, blob_mask, roi_image, roi_corner) output.append((irow, feats)) return output
def drawThreshMask(self, worm_img, worm_qimg, row_data, read_center=True): #in very old versions of the tracker I didn't save the area in trajectories table, #let's assign a default value to deal with this cases if 'area' in row_data: min_blob_area = row_data['area'] / 2 else: min_blob_area = 10 c1, c2 = (row_data['coord_x'], row_data['coord_y']) if read_center else (-1, -1) worm_mask, worm_cnt, _ = getWormMask( worm_img, row_data['threshold'], strel_size=self.strel_size, roi_center_x=c1, roi_center_y=c2, min_blob_area=min_blob_area, is_light_background=self.is_light_background) worm_mask = QImage(worm_mask.data, worm_mask.shape[1], worm_mask.shape[0], worm_mask.strides[0], QImage.Format_Indexed8) worm_mask = worm_mask.convertToFormat(QImage.Format_RGB32, Qt.AutoColor) worm_mask = QPixmap.fromImage(worm_mask) worm_mask = worm_mask.createMaskFromColor(Qt.black) p = QPainter(worm_qimg) p.setPen(QColor(0, 204, 102)) p.drawPixmap(worm_qimg.rect(), worm_mask, worm_mask.rect()) if False: #test skeletonization skeleton, ske_len, cnt_side1, cnt_side2, cnt_widths, cnt_area = \ getSkeleton(worm_cnt, np.zeros(0), 49) for cnt in skeleton, cnt_side1, cnt_side2: p.setPen(Qt.black) polyline = QPolygonF() for point in cnt: polyline.append(QPointF(*point)) p.drawPolyline(polyline) p.end()
def getIndCnt(img, x, y, roi_size, thresh, max_area): worm_img, roi_corner = getWormROI(img, x, y, roi_size) worm_mask, worm_cnt, _ = getWormMask(worm_img, thresh) if worm_cnt.size > 0: worm_cnt += roi_corner return worm_cnt
#read the data from all the worms in this frame frame_data = traj_group_by_frame.get_group(frame_number) #this functions crop all the ROIs of all the worms located in the image worms_in_frame = getAllImgROI(img, frame_data) #now i want to pass this into a binary mask with the segmentation labeled_mask = np.zeros_like(img, dtype=np.uint8) for iworm, (ind, (worm_img, roi_corner)) in enumerate(worms_in_frame.items()): row_data = trajectories_data.loc[ind] #here is where the ROI is really segmented. It uses threshold plus some morphology operations to clean a bit worm_mask, worm_cnt, cnt_area = getWormMask(worm_img, row_data['threshold'], strel_size=5, min_blob_area=row_data['area'] / 2, is_light_background = True ) #now I want to put the ROI mask into the larger labelled image xi,yi = roi_corner ss = worm_mask.shape[0] #here I am saving only the pixels located to the worm. #This is safer than assigned the whole ROI in case of overlaping regions. m_roi = labeled_mask[yi:yi+ss, xi:xi+ss].view() m_roi[worm_mask>0] += iworm #visualize the results fig, axs = plt.subplots(1,2, sharex=True, sharey=True) axs[0].imshow(img) axs[1].imshow(labeled_mask)
masked_file = "/Volumes/behavgenom_archive$/Avelino/screening/CeNDR/MaskedVideos/CeNDR_Exp_250417/BRC20067_worms10_food1-3_Set4_Pos5_Ch2_25042017_140346.hdf5" skeletons_file = masked_file.replace('MaskedVideos', 'Results').replace('.hdf5', '_skeletons.hdf5') with pd.HDFStore(skeletons_file, 'r') as fid: trajectories_data = fid['/trajectories_data'] roi_generator = generateMoviesROI(masked_file, trajectories_data) worms_in_frame = next(roi_generator) for row in worms_in_frame: worm_img, roi_corner = worms_in_frame[row] row_data = trajectories_data.loc[row] worm_mask, worm_cnt, _ = getWormMask(worm_img, row_data['threshold'], 5) plt.figure() plt.subplot(1,2,1) plt.imshow(worm_img, interpolation=None, cmap='gray') plt.subplot(1,2,2) plt.imshow(worm_mask, interpolation=None, cmap='gray') break #import h5py #import pandas as pd #import numpy as np #from skimage.filters import threshold_otsu #from scipy.signal import medfilt