Пример #1
0
    def texture_features(self, block_size=5, filter_size=3):
        """
        Calculates the texture features associated with the image.
        block_size gives the size of the texture neighborhood to be processed
        filter_size gives the size of the Sobel operator used to find gradient information
        """
        #block_size = cv.cvSize(block_size, block_size)

        #convert to grayscale float
        channels = 1
        self.gray_image = cv.cvCreateImage(
            cv.cvSize(self.im_width, self.im_height),
            cv.IPL_DEPTH_8U,  #cv.IPL_DEPTH_16U, #cv.IPL_DEPTH_32F,
            channels)

        #cv.CV_32FC1, #cv.IPL_DEPTH_32F, #cv.IPL_DEPTH_8U, #cv.IPL_DEPTH_16U,
        channels = 1
        eig_tex = cv.cvCreateImage(
            cv.cvSize(self.im_width * 6, self.im_height), cv.IPL_DEPTH_32F,
            channels)

        cv.cvCvtColor(self.image, self.gray_image, cv.CV_BGR2GRAY)

        #cv.cvAdd(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL );

        #highgui.cvConvertImage(self.image, self.gray_image)

        cv.cvCornerEigenValsAndVecs(
            self.gray_image,
            eig_tex,  #CvArr* eigenvv,
            block_size,
            filter_size)

        eig_tex = ut.cv2np(eig_tex)
        eig_tex = np.reshape(eig_tex, [self.im_height, self.im_width, 6])
        #print eig_tex.shape ## [480,640,3]
        ## (l1, l2, x1, y1, x2, y2), where
        ## l1, l2 - eigenvalues of M; not sorted
        ## (x1, y1) - eigenvector corresponding to l1
        ## (x2, y2) - eigenvector corresponding to l2
        tex_feat = np.zeros([3, self.im_height * self.im_width],
                            dtype=np.float32)
        tmp = np.reshape(eig_tex, [self.im_height * self.im_width, 6]).T
        s = tmp[0] > tmp[1]
        tex_feat[1:3, s] = tmp[0, s] * tmp[2:4, s]
        tex_feat[0, s] = tmp[1, s]
        tex_feat[1:3, -s] = tmp[1, -s] * tmp[4:6, -s]
        tex_feat[0, -s] = tmp[0, -s]

        self.tex_feat = tex_feat.T
        self.tex_image = np.reshape(self.tex_feat,
                                    [self.im_height, self.im_width, 3])
Пример #2
0
    def texture_features(self, block_size=5, filter_size=3):
        """
        Calculates the texture features associated with the image.
        block_size gives the size of the texture neighborhood to be processed
        filter_size gives the size of the Sobel operator used to find gradient information
        """
        #block_size = cv.cvSize(block_size, block_size)

        #convert to grayscale float
        channels = 1
        self.gray_image = cv.cvCreateImage(cv.cvSize(self.im_width, self.im_height),
                                           cv.IPL_DEPTH_8U, #cv.IPL_DEPTH_16U, #cv.IPL_DEPTH_32F,
                                           channels)


        #cv.CV_32FC1, #cv.IPL_DEPTH_32F, #cv.IPL_DEPTH_8U, #cv.IPL_DEPTH_16U, 
        channels = 1
        eig_tex = cv.cvCreateImage(cv.cvSize(self.im_width*6, self.im_height),
                                    cv.IPL_DEPTH_32F, 
                                    channels)


        cv.cvCvtColor(self.image, self.gray_image, cv.CV_BGR2GRAY);

        #cv.cvAdd(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL );
        
        #highgui.cvConvertImage(self.image, self.gray_image)
        
        cv.cvCornerEigenValsAndVecs(self.gray_image, eig_tex,#CvArr* eigenvv,
                                    block_size, filter_size)

        eig_tex = ut.cv2np(eig_tex)
        eig_tex = np.reshape(eig_tex, [self.im_height, self.im_width, 6])
        #print eig_tex.shape ## [480,640,3]
        ## (l1, l2, x1, y1, x2, y2), where
        ## l1, l2 - eigenvalues of M; not sorted
        ## (x1, y1) - eigenvector corresponding to l1
        ## (x2, y2) - eigenvector corresponding to l2
        tex_feat = np.zeros([3, self.im_height * self.im_width], dtype=np.float32)
        tmp = np.reshape(eig_tex, [self.im_height * self.im_width, 6]).T
        s = tmp[0] > tmp[1]
        tex_feat[1:3, s] = tmp[0, s] * tmp[2:4, s]
        tex_feat[0, s] = tmp[1, s]
        tex_feat[1:3, -s] = tmp[1, -s] * tmp[4:6, -s]
        tex_feat[0, -s] = tmp[0, -s]
        
        self.tex_feat = tex_feat.T
        self.tex_image = np.reshape(self.tex_feat, [self.im_height, self.im_width, 3])
Пример #3
0
 def __init__(self, image, use_texture):
     """
     Create an ImageFeatures object for an input image. use_texture is a boolean that when true results in the inclusion of texture features in addition to spatial location and color features. 
     """
     self.image = image
     self.im_size = cv.cvGetSize(image)
     self.im_width = self.im_size.width
     self.im_height = self.im_size.height
     self.im_colors = 3
     self.array_image = ut.cv2np(image)
     self.tex_feat = None
     self.selected_features = None
     #self.im_width = image.shape[1]
     #self.im_height = image.shape[0]
     #self.im_colors = image.shape[2]
     self.use_texture = use_texture
     self.create_features(self.use_texture)
     self.mask_image = None
Пример #4
0
 def __init__(self, image, use_texture):
     """
     Create an ImageFeatures object for an input image. use_texture is a boolean that when true results in the inclusion of texture features in addition to spatial location and color features. 
     """
     self.image = image
     self.im_size = cv.cvGetSize(image)
     self.im_width = self.im_size.width
     self.im_height = self.im_size.height
     self.im_colors = 3
     self.array_image = ut.cv2np(image)
     self.tex_feat = None
     self.selected_features = None
     #self.im_width = image.shape[1]
     #self.im_height = image.shape[0]
     #self.im_colors = image.shape[2]
     self.use_texture = use_texture
     self.create_features(self.use_texture)
     self.mask_image = None
Пример #5
0
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
Пример #6
0
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