def get_images_for_display(self, draw_mixture=True): """ returns a list of images that show the intermediate and final results of the image segmentation """ image_list = [] ## display the resulting gaussians if draw_mixture: for i, ellipse in enumerate(gmm2ellipses(self.gmm)): ellipse.draw_on_image(self.image, color=(255 * i, 255, 255 * i), principal_axis_color=(255, 255, 255)) if self.fg_object_ellipse is not None: self.fg_object_ellipse.draw_on_image( self.image, color=(255, 0, 0), principal_axis_color=(255, 255, 255)) image_list.append(self.image) if self.class_image is not None: if str(type(self.class_image)).find('opencv') == -1: disp_im = ut.np2cv(self.class_image) else: disp_im = self.class_image image_list.append(disp_im) if self.clean_class_image is not None: if str(type(self.clean_class_image)).find('opencv') == -1: clean_disp_im = ut.np2cv(self.clean_class_image) else: clean_disp_im = self.class_image image_list.append(clean_disp_im) if self.large_obj is not None: if str(type(self.large_obj)).find('opencv') == -1: obj_disp_im = ut.np2cv(self.large_obj) else: obj_disp_im = self.class_image if draw_mixture: if self.fg_object_ellipse is not None: self.fg_object_ellipse.draw_on_image( obj_disp_im, color=(255, 0, 0), principal_axis_color=(255, 255, 255)) image_list.append(obj_disp_im) return image_list
def get_images_for_display(self, draw_mixture=True): """ returns a list of images that show the intermediate and final results of the image segmentation """ image_list = [] ## display the resulting gaussians if draw_mixture: for i, ellipse in enumerate(gmm2ellipses(self.gmm)): ellipse.draw_on_image(self.image, color=(255*i,255,255*i), principal_axis_color=(255,255,255)) if self.fg_object_ellipse is not None: self.fg_object_ellipse.draw_on_image(self.image, color=(255, 0, 0), principal_axis_color=(255,255,255)) image_list.append(self.image) if self.class_image is not None: if str(type(self.class_image)).find('opencv') == -1: disp_im = ut.np2cv(self.class_image) else: disp_im = self.class_image image_list.append(disp_im) if self.clean_class_image is not None: if str(type(self.clean_class_image)).find('opencv') == -1: clean_disp_im = ut.np2cv(self.clean_class_image) else: clean_disp_im = self.class_image image_list.append(clean_disp_im) if self.large_obj is not None: if str(type(self.large_obj)).find('opencv') == -1: obj_disp_im = ut.np2cv(self.large_obj) else: obj_disp_im = self.class_image if draw_mixture: if self.fg_object_ellipse is not None: self.fg_object_ellipse.draw_on_image(obj_disp_im, color=(255, 0, 0), principal_axis_color=(255,255,255)) image_list.append(obj_disp_im) return image_list
def fit_to_largest_object(self): """ fit an ellipse to the segmented, cleaned, and selected object """ if self.large_obj is None: self.find_largest_object() obj_mask = self.large_obj > 0 h, w = obj_mask.shape xcoor, ycoor = xy_features(w,h) obj_mask = obj_mask.flatten() xcoor = xcoor[obj_mask] ycoor = ycoor[obj_mask] points = np.concatenate((xcoor, ycoor), axis=1).transpose() points = np.matrix(points) print "points shape:" print points.shape start = gm.simple_start(1, points) gmm = gm.GMM(points, start) gmm.fit(iter_limit=self.iter_limit) #weights = np.matrix(np.ones(points.shape[0])) #gauss = pb.Gaussian(0,0,dimension=2) self.fg_object_ellipse = gmm2ellipses(gmm)[0] if False: tmp = points.astype(np.float32).T tmp = np.reshape(tmp, [1, tmp.shape[1], 2]) cvpoints = ut.np2cv(tmp) ellipse = cv.cvFitEllipse2(cvpoints) self.fg_object_ellipse = ut.Ellipse() self.fg_object_ellipse.set_from_cvbox2d(ellipse)
def fit_to_largest_object(self): """ fit an ellipse to the segmented, cleaned, and selected object """ if self.large_obj is None: self.find_largest_object() obj_mask = self.large_obj > 0 h, w = obj_mask.shape xcoor, ycoor = xy_features(w, h) obj_mask = obj_mask.flatten() xcoor = xcoor[obj_mask] ycoor = ycoor[obj_mask] points = np.concatenate((xcoor, ycoor), axis=1).transpose() points = np.matrix(points) print "points shape:" print points.shape start = gm.simple_start(1, points) gmm = gm.GMM(points, start) gmm.fit(iter_limit=self.iter_limit) #weights = np.matrix(np.ones(points.shape[0])) #gauss = pb.Gaussian(0,0,dimension=2) self.fg_object_ellipse = gmm2ellipses(gmm)[0] if False: tmp = points.astype(np.float32).T tmp = np.reshape(tmp, [1, tmp.shape[1], 2]) cvpoints = ut.np2cv(tmp) ellipse = cv.cvFitEllipse2(cvpoints) self.fg_object_ellipse = ut.Ellipse() self.fg_object_ellipse.set_from_cvbox2d(ellipse)
def segment_center_object(image, display_on=False, nsamp=10000, iter_limit=30, use_texture=True, use_hsv=True, set_v_to_zero=True, use_mask=True, remove_saturation=False, remove_boundary = True, prior_gmm=None, use_flip_heuristic=True): """ segment the input image (OpenCV image) and return an ellipse fit to the center object (foreground) and a binary image mask for this foreground object nsamp : number of pixels to be used when fitting the Gaussian mixture model (impacts speed and accuracy) iter_limit : maximum number of iterations when fitting the texture model use_texture : use texture features use_hsv : use hsv color space for features set_v_to_zero : effectively remove the value (brightness) component of the hsv features use_mask : mask out areas of the image prior to training the appearance model and segmenting remove_saturation : if use_mask, then remove saturated pixels (RGB values = 255 = max value) remove_boundary : if use_mask, then remove the borders of the image prior to segmenting it returns a segmentation object (SegmentObject) """ #remove_low_freq = True if use_hsv: hsv_image = cv.cvCreateImage(cv.cvSize(image.width, image.height), cv.IPL_DEPTH_8U, 3) cv.cvCvtColor(image, hsv_image, cv.CV_RGB2HSV) #cv.CV_BGR2HSV) if set_v_to_zero: #cvSet(hsv_image, cvScalarAll(0), ) for y in xrange(hsv_image.height): for x in xrange(hsv_image.width): pix = hsv_image[y,x] hsv_image[y,x] = cv.cvScalar(pix[0], pix[1], 0.0) image = hsv_image if display_on: image_list = [] else: image_list = None imf = ImageFeatures(image, use_texture=use_texture) #imf.texture_features() if use_mask: #test_mask = np.zeros([image.height, image.width]) #test_mask[0:200, 0:200] = 1.0 #test_mask = test_mask > 0.0 # select saturation mask nim = ut.cv2np(image) if remove_saturation: # remove saturated pixels #saturation_mask = ~np.alltrue(nim > 255, axis=2) saturation_mask = ~np.any(nim >= 255, axis=2) #saturation_mask = np.sum(nim >= 255, axis=2) < 2 if remove_boundary: # remove boundaries beyond the possible object size border_y = 50 border_x = 100 too_big_mask = np.zeros(nim.shape[:2], dtype=np.bool) w = nim.shape[1] h = nim.shape[0] too_big_mask[border_y : h - border_y, border_x : w - border_x] = True if remove_saturation and remove_boundary: feature_mask = saturation_mask & too_big_mask elif remove_saturation: feature_mask = saturation_mask else: feature_mask = too_big_mask disp_mask = feature_mask.copy() features = imf.select_subset(nsamp, mask_image=feature_mask) cv_mask = ut.np2cv(disp_mask.astype(np.uint8) * 255) if image_list is not None: image_list.append(cv_mask) else: features = imf.select_subset(nsamp) #sego = SegmentObject(image, features, iter_limit=iter_limit) sego = SegmentObject(image, imf, iter_limit=iter_limit, prior_gmm=prior_gmm) sego.classify_image(use_flip_heuristic) sego.clean_classified_image() #sego.find_largest_object() sego.find_best_object() sego.fit_to_largest_object() if image_list is not None: image_list.extend(sego.get_images_for_display()) ut.display_images(image_list) return sego
def segment_center_object(image, display_on=False, nsamp=10000, iter_limit=30, use_texture=True, use_hsv=True, set_v_to_zero=True, use_mask=True, remove_saturation=False, remove_boundary=True, prior_gmm=None, use_flip_heuristic=True): """ segment the input image (OpenCV image) and return an ellipse fit to the center object (foreground) and a binary image mask for this foreground object nsamp : number of pixels to be used when fitting the Gaussian mixture model (impacts speed and accuracy) iter_limit : maximum number of iterations when fitting the texture model use_texture : use texture features use_hsv : use hsv color space for features set_v_to_zero : effectively remove the value (brightness) component of the hsv features use_mask : mask out areas of the image prior to training the appearance model and segmenting remove_saturation : if use_mask, then remove saturated pixels (RGB values = 255 = max value) remove_boundary : if use_mask, then remove the borders of the image prior to segmenting it returns a segmentation object (SegmentObject) """ #remove_low_freq = True if use_hsv: hsv_image = cv.cvCreateImage(cv.cvSize(image.width, image.height), cv.IPL_DEPTH_8U, 3) cv.cvCvtColor(image, hsv_image, cv.CV_RGB2HSV) #cv.CV_BGR2HSV) if set_v_to_zero: #cvSet(hsv_image, cvScalarAll(0), ) for y in xrange(hsv_image.height): for x in xrange(hsv_image.width): pix = hsv_image[y, x] hsv_image[y, x] = cv.cvScalar(pix[0], pix[1], 0.0) image = hsv_image if display_on: image_list = [] else: image_list = None imf = ImageFeatures(image, use_texture=use_texture) #imf.texture_features() if use_mask: #test_mask = np.zeros([image.height, image.width]) #test_mask[0:200, 0:200] = 1.0 #test_mask = test_mask > 0.0 # select saturation mask nim = ut.cv2np(image) if remove_saturation: # remove saturated pixels #saturation_mask = ~np.alltrue(nim > 255, axis=2) saturation_mask = ~np.any(nim >= 255, axis=2) #saturation_mask = np.sum(nim >= 255, axis=2) < 2 if remove_boundary: # remove boundaries beyond the possible object size border_y = 50 border_x = 100 too_big_mask = np.zeros(nim.shape[:2], dtype=np.bool) w = nim.shape[1] h = nim.shape[0] too_big_mask[border_y:h - border_y, border_x:w - border_x] = True if remove_saturation and remove_boundary: feature_mask = saturation_mask & too_big_mask elif remove_saturation: feature_mask = saturation_mask else: feature_mask = too_big_mask disp_mask = feature_mask.copy() features = imf.select_subset(nsamp, mask_image=feature_mask) cv_mask = ut.np2cv(disp_mask.astype(np.uint8) * 255) if image_list is not None: image_list.append(cv_mask) else: features = imf.select_subset(nsamp) #sego = SegmentObject(image, features, iter_limit=iter_limit) sego = SegmentObject(image, imf, iter_limit=iter_limit, prior_gmm=prior_gmm) sego.classify_image(use_flip_heuristic) sego.clean_classified_image() #sego.find_largest_object() sego.find_best_object() sego.fit_to_largest_object() if image_list is not None: image_list.extend(sego.get_images_for_display()) ut.display_images(image_list) return sego