Exemplo n.º 1
0
 def jitter( self, i_image , i_n_examples ):
     """1) Apply various random affine transform to i_image (scale, rotation), 
           where i_n_examples is the number of transformations. The affine transforms happen 
           around the center of the original region of interest. 
        2) Translate roi with various values - crop the image from this region.
        3) The same normalisation is then applied to all the cropped images, specified by setParams"""
     
     #Store transforms applied in format tx ty scale angle
     o_transforms = numpy.array([0., 0.,  1.,  0.]) 
     
     if self.__roi == None:
         print "No region of interest - returning input image!"
         return None
     #Always return the normalised verion of the input image
     image = cv.Ipl2NumPy(self.normalise(i_image))
     o_data = numpy.tile( None, (image.shape[0], image.shape[1], 1))
     o_data[:,:,0] =  image
     if i_n_examples == 0:
         return ( o_data, o_transforms)
     #Rotation point should be around original roi center
     center = cv.cvPoint2D32f( self.__roi.x + self.__roi.width/2,self.__roi.y + self.__roi.height/2 )
     angles = numpy.random.uniform(-30.,30.,i_n_examples)
     scales = numpy.random.uniform(0.9,1.1, i_n_examples)
     tx = numpy.int32( numpy.round( numpy.random.uniform(-20,20,i_n_examples)))
     ty = numpy.int32( numpy.round( numpy.random.uniform(-20,20,i_n_examples)))
     x = self.__roi.x + tx
     y = self.__roi.y + ty
     #FIXME: Extend valid indices to outliers due to affine transform!!!
     min_x = 0; min_y = 0;
     max_x = i_image.width - self.__roi.width - 1
     max_y = i_image.height - self.__roi.height - 1  
     valid_x = numpy.hstack([numpy.nonzero( x >= min_x )[0], numpy.nonzero( x < max_x)[0]])
     valid_y = numpy.hstack([numpy.nonzero( y >= min_y )[0], numpy.nonzero( y < max_y)[0]])
     valid_idx = numpy.unique(numpy.hstack([valid_x, valid_y]))
     original_roi = cv.cvRect( self.__roi.x,  self.__roi.y,  self.__roi.width,  self.__roi.height)
     if self.__rot_mat == None:
         original_rot_matrix = None
     else:
         original_rot_matrix = cv.cvCloneImage(self.__rot_mat)
     for index in valid_idx:
         params = numpy.array([tx[index], ty[index], scales[index], angles[index] ]) 
         self.setAffineTransform( center, scales[index], angles[index])
         self.__roi.x = int( x[index] )
         self.__roi.y = int( y[index] )     
         image = cv.Ipl2NumPy(self.normalise( i_image ))
         o_data = numpy.dstack( [o_data, image])
         o_transforms = numpy.vstack( [o_transforms, params])  
     #Restore the original region of interest
     self.__roi.x = original_roi.x
     self.__roi.y = original_roi.y
     if original_rot_matrix == None:
         self.__rot_mat= None
     else:
         self.__rot_mat = cv.cvCloneImage(original_rot_matrix)
     return (o_data, o_transforms)
Exemplo n.º 2
0
 def correctImage(self, i_image, i_transform):
     roi = self.getRoi()
     tx = numpy.int( numpy.round( i_transform[0] ) )
     ty = numpy.int( numpy.round( i_transform[1] ) )
     roi.x += tx
     roi.y += ty
     self.setRoi(roi)
     scale = i_transform[2]
     angle = i_transform[3]
     center = cv.cvPoint2D32f( self.__roi.x + self.__roi.width/2,self.__roi.y + self.__roi.height/2 )
     self.setAffineTransform( center, scale, angle)
     o_image =  self.normalise(i_image)
     return  cv.Ipl2NumPy(o_image)
Exemplo n.º 3
0
 def correctImage(self, i_image, i_transform):
     roi = self.getRoi()
     tx = numpy.int(numpy.round(i_transform[0]))
     ty = numpy.int(numpy.round(i_transform[1]))
     roi.x += tx
     roi.y += ty
     self.setRoi(roi)
     scale = i_transform[2]
     angle = i_transform[3]
     center = cv.cvPoint2D32f(self.__roi.x + self.__roi.width / 2,
                              self.__roi.y + self.__roi.height / 2)
     self.setAffineTransform(center, scale, angle)
     o_image = self.normalise(i_image)
     return cv.Ipl2NumPy(o_image)
Exemplo n.º 4
0
 def similarityTransform(self, i_image, i_transform, i_crop=True ):
     """Apply affine transform around center of region of interest, then translate roi"""
     tx = numpy.int32(numpy.round(i_transform[0]))
     ty = numpy.int32(numpy.round(i_transform[1]))
     scale = i_transform[2]
     angle = i_transform[3]
     center = cv.cvPoint2D32f( self.__roi.x + self.__roi.width/2,self.__roi.y + self.__roi.height/2 )
     self.setAffineTransform( center, scale, angle)
     original_roi = cv.cvRect( self.__roi.x,  self.__roi.y,  self.__roi.width,  self.__roi.height)
     
     if not i_crop:
         self.__roi = None
     else:
         self.__roi.x = self.__roi.x + int(tx)
         self.__roi.y = self.__roi.y + int(ty)
     filter_size = self.__filter_size
     self.__filter_size = 0
     o_image =  self.normalise(i_image)
     self.__filter_size = filter_size
     self.__roi = cv.cvRect(original_roi.x, original_roi.y, original_roi.width,  original_roi.height)
     return  cv.Ipl2NumPy(o_image)
Exemplo n.º 5
0
    def similarityTransform(self, i_image, i_transform, i_crop=True):
        """Apply affine transform around center of region of interest, then translate roi"""
        tx = numpy.int32(numpy.round(i_transform[0]))
        ty = numpy.int32(numpy.round(i_transform[1]))
        scale = i_transform[2]
        angle = i_transform[3]
        center = cv.cvPoint2D32f(self.__roi.x + self.__roi.width / 2,
                                 self.__roi.y + self.__roi.height / 2)
        self.setAffineTransform(center, scale, angle)
        original_roi = cv.cvRect(self.__roi.x, self.__roi.y, self.__roi.width,
                                 self.__roi.height)

        if not i_crop:
            self.__roi = None
        else:
            self.__roi.x = self.__roi.x + int(tx)
            self.__roi.y = self.__roi.y + int(ty)
        filter_size = self.__filter_size
        self.__filter_size = 0
        o_image = self.normalise(i_image)
        self.__filter_size = filter_size
        self.__roi = cv.cvRect(original_roi.x, original_roi.y,
                               original_roi.width, original_roi.height)
        return cv.Ipl2NumPy(o_image)
Exemplo n.º 6
0
    def jitter(self, i_image, i_n_examples):
        """1) Apply various random affine transform to i_image (scale, rotation), 
              where i_n_examples is the number of transformations. The affine transforms happen 
              around the center of the original region of interest. 
           2) Translate roi with various values - crop the image from this region.
           3) The same normalisation is then applied to all the cropped images, specified by setParams"""

        #Store transforms applied in format tx ty scale angle
        o_transforms = numpy.array([0., 0., 1., 0.])

        if self.__roi == None:
            print "No region of interest - returning input image!"
            return None
        #Always return the normalised verion of the input image
        image = cv.Ipl2NumPy(self.normalise(i_image))
        o_data = numpy.tile(None, (image.shape[0], image.shape[1], 1))
        o_data[:, :, 0] = image
        if i_n_examples == 0:
            return (o_data, o_transforms)
        #Rotation point should be around original roi center
        center = cv.cvPoint2D32f(self.__roi.x + self.__roi.width / 2,
                                 self.__roi.y + self.__roi.height / 2)
        angles = numpy.random.uniform(-30., 30., i_n_examples)
        scales = numpy.random.uniform(0.9, 1.1, i_n_examples)
        tx = numpy.int32(
            numpy.round(numpy.random.uniform(-20, 20, i_n_examples)))
        ty = numpy.int32(
            numpy.round(numpy.random.uniform(-20, 20, i_n_examples)))
        x = self.__roi.x + tx
        y = self.__roi.y + ty
        #FIXME: Extend valid indices to outliers due to affine transform!!!
        min_x = 0
        min_y = 0
        max_x = i_image.width - self.__roi.width - 1
        max_y = i_image.height - self.__roi.height - 1
        valid_x = numpy.hstack(
            [numpy.nonzero(x >= min_x)[0],
             numpy.nonzero(x < max_x)[0]])
        valid_y = numpy.hstack(
            [numpy.nonzero(y >= min_y)[0],
             numpy.nonzero(y < max_y)[0]])
        valid_idx = numpy.unique(numpy.hstack([valid_x, valid_y]))
        original_roi = cv.cvRect(self.__roi.x, self.__roi.y, self.__roi.width,
                                 self.__roi.height)
        if self.__rot_mat == None:
            original_rot_matrix = None
        else:
            original_rot_matrix = cv.cvCloneImage(self.__rot_mat)
        for index in valid_idx:
            params = numpy.array(
                [tx[index], ty[index], scales[index], angles[index]])
            self.setAffineTransform(center, scales[index], angles[index])
            self.__roi.x = int(x[index])
            self.__roi.y = int(y[index])
            image = cv.Ipl2NumPy(self.normalise(i_image))
            o_data = numpy.dstack([o_data, image])
            o_transforms = numpy.vstack([o_transforms, params])
        #Restore the original region of interest
        self.__roi.x = original_roi.x
        self.__roi.y = original_roi.y
        if original_rot_matrix == None:
            self.__rot_mat = None
        else:
            self.__rot_mat = cv.cvCloneImage(original_rot_matrix)
        return (o_data, o_transforms)
Exemplo n.º 7
0
    data = image_utils.Video2Numpy("recordings/calibration.avi", 1)
    detector = ViolaJonesRoi()
    #Compute a region of interest automatically
    face_roi = detector.compute(data)
    eye_roi = detector.convertFace2EyeRoi(face_roi)
    detector.setRoi(eye_roi)
    (min_row, min_col, max_row, max_col) = eye_roi

    roi = image_utils.Numpy2CvRect(min_row, min_col, max_row, max_col)
    #Setup normaliser
    normaliser = IplImageNormaliser()
    normaliser.setParams(i_resize_scale=1.,
                         i_filter_size=0,
                         i_eq=False,
                         i_roi=roi)
    center = cv.cvPoint2D32f(roi.x + roi.width / 2, roi.y + roi.height / 2)
    normaliser.setAffineTransform(center, i_scale=1., i_rot_angle=0)
    nframe = 0
    app = QtGui.QApplication(argv)
    timer = QtCore.QTimer()
    n_jitter_examples = 5
    display = ImageDisplay(n_jitter_examples + 2)
    (jittered_images, transforms,
     n_jittered) = normaliser.jitter_video(data, n_jitter_examples)
    nframe = 0

    def updateDisplay():
        global nframe
        rows = max_row - min_row
        cols = max_col - min_col
Exemplo n.º 8
0
    from roi_detector import ViolaJonesRoi

   
    data = image_utils.Video2Numpy("recordings/calibration.avi", 1) 
    detector = ViolaJonesRoi()
    #Compute a region of interest automatically
    face_roi= detector.compute(data)
    eye_roi = detector.convertFace2EyeRoi(face_roi)
    detector.setRoi(eye_roi)
    (min_row, min_col, max_row, max_col)  = eye_roi
        
    roi = image_utils.Numpy2CvRect(min_row, min_col, max_row, max_col)
    #Setup normaliser
    normaliser = IplImageNormaliser()
    normaliser.setParams(i_resize_scale=1., i_filter_size=0, i_eq=False, i_roi=roi)
    center = cv.cvPoint2D32f( roi.x + roi.width/2, roi.y + roi.height/2 )
    normaliser.setAffineTransform(center, i_scale=1., i_rot_angle=0)
    nframe = 0
    app = QtGui.QApplication(argv)
    timer = QtCore.QTimer()
    n_jitter_examples = 5
    display = ImageDisplay(n_jitter_examples + 2)
    (jittered_images, transforms, n_jittered) = normaliser.jitter_video(data, n_jitter_examples)
    nframe = 0

    def updateDisplay():
        global nframe
        rows = max_row - min_row
        cols = max_col - min_col
                
        if nframe >= data.shape[2]: