def onBtnCreateSaliencyMaskClicked(self, widget):

        ROI_X = 0
        ROI_Y = 76
        ROI_WIDTH = 230
        ROI_HEIGHT = 100

        #ROI_WIDTH = 10

        if self.image != None:

            # Create a saliency map from the current image
            grayImage = np.ndarray((self.image.height, self.image.width),
                                   dtype=np.uint8)
            cv.CvtColor(self.image, grayImage, cv.CV_RGB2GRAY)

            saliencyMap, largeSaliencyMap = self.residualSaliencyFilter.calcSaliencyMap(
                grayImage)
            self.dwgSaliencyDisplay.setImageFromNumpyArray(largeSaliencyMap)

            # Threshold to get blobs
            blobPixels = largeSaliencyMap >= 160
            largeSaliencyMap[blobPixels] = 255
            largeSaliencyMap[blobPixels == False] = 0

            # Label blobs
            largeSaliencyMap, numBlobs = PyBlobLib.labelBlobs(largeSaliencyMap)

            # Find blobs in the region of interest
            testMap = np.copy(largeSaliencyMap)
            testMap[:ROI_Y, :] = 0  # Mask out area above the ROI
            testMap[:, :ROI_X] = 0  # Mask out area to the left of the ROI
            testMap[ROI_Y + ROI_HEIGHT:] = 0  # Mask out area below the ROI
            testMap[:, ROI_X +
                    ROI_WIDTH:] = 0  # Mask out area to the right of the ROI

            biggestBlobIdx = None
            biggestBlobSize = 0

            for blobIdx in range(1, numBlobs + 1):
                if testMap[testMap == blobIdx].size > 0:
                    blobSize = largeSaliencyMap[largeSaliencyMap ==
                                                blobIdx].size
                    if blobSize > biggestBlobSize:
                        biggestBlobSize = blobSize
                        biggestBlobIdx = blobIdx

            # Isolate the largest blob
            if biggestBlobIdx != None:
                biggestBlobPixels = (largeSaliencyMap == biggestBlobIdx)
                print biggestBlobPixels.size
                largeSaliencyMap[biggestBlobPixels] = self.GC_PR_FGD
                largeSaliencyMap[biggestBlobPixels == False] = self.GC_PR_BGD

                # Use the largest blob as the segmentation mask
                self.maskArray = largeSaliencyMap
                self.redrawImageWithMask()
    def onBtnCreateSaliencyMaskClicked(self, widget):

        ROI_X = 0
        ROI_Y = 76
        ROI_WIDTH = 230
        ROI_HEIGHT = 100

        # ROI_WIDTH = 10

        if self.image != None:

            # Create a saliency map from the current image
            grayImage = np.ndarray((self.image.height, self.image.width), dtype=np.uint8)
            cv.CvtColor(self.image, grayImage, cv.CV_RGB2GRAY)

            saliencyMap, largeSaliencyMap = self.residualSaliencyFilter.calcSaliencyMap(grayImage)
            self.dwgSaliencyDisplay.setImageFromNumpyArray(largeSaliencyMap)

            # Threshold to get blobs
            blobPixels = largeSaliencyMap >= 160
            largeSaliencyMap[blobPixels] = 255
            largeSaliencyMap[blobPixels == False] = 0

            # Label blobs
            largeSaliencyMap, numBlobs = PyBlobLib.labelBlobs(largeSaliencyMap)

            # Find blobs in the region of interest
            testMap = np.copy(largeSaliencyMap)
            testMap[:ROI_Y, :] = 0  # Mask out area above the ROI
            testMap[:, :ROI_X] = 0  # Mask out area to the left of the ROI
            testMap[ROI_Y + ROI_HEIGHT :] = 0  # Mask out area below the ROI
            testMap[:, ROI_X + ROI_WIDTH :] = 0  # Mask out area to the right of the ROI

            biggestBlobIdx = None
            biggestBlobSize = 0

            for blobIdx in range(1, numBlobs + 1):
                if testMap[testMap == blobIdx].size > 0:
                    blobSize = largeSaliencyMap[largeSaliencyMap == blobIdx].size
                    if blobSize > biggestBlobSize:
                        biggestBlobSize = blobSize
                        biggestBlobIdx = blobIdx

            # Isolate the largest blob
            if biggestBlobIdx != None:
                biggestBlobPixels = largeSaliencyMap == biggestBlobIdx
                print biggestBlobPixels.size
                largeSaliencyMap[biggestBlobPixels] = self.GC_PR_FGD
                largeSaliencyMap[biggestBlobPixels == False] = self.GC_PR_BGD

                # Use the largest blob as the segmentation mask
                self.maskArray = largeSaliencyMap
                self.redrawImageWithMask()
 def produceSegmentation( self, startFrame, impactMotionImage, 
                             preMotionImages, postMotionImages ):
     
     ROI_X = 0
     ROI_Y = 76
     ROI_WIDTH = 230
     ROI_HEIGHT = 100
     
     blankFrame = np.zeros( ( startFrame.height, startFrame.width ), dtype=np.uint8 )
     
     imageFlowFilter = ImageFlowFilter()    
     
     # Create the accumulator image
     accumulatorArray = np.copy( impactMotionImage ).astype( np.int32 )
         
     # Take maximum values from motion images after the impact but
     # don't add them in to de-emphasise the manipulator
     imageNum = 1
     for postMotionImage in postMotionImages:
         
         print "Aligning post impact image {0}...".format( imageNum )
         imageNum += 1
         
         ( transX, transY, rotationAngle, alignedImage ) = \
             imageFlowFilter.calcImageFlow( impactMotionImage, postMotionImage )
         accumulatorArray = np.maximum( accumulatorArray, alignedImage )
                 
     # Dilate and subtract motion images from before the impact
     imageNum = 1
     for preMotionImage in preMotionImages:
         
         print "Aligning pre impact image {0}...".format( imageNum )
         imageNum += 1
         
         ( transX, transY, rotationAngle, alignedImage ) = \
             imageFlowFilter.calcImageFlow( impactMotionImage, preMotionImage )
             
         cv.Dilate( alignedImage, alignedImage )
         cv.Dilate( alignedImage, alignedImage )
         cv.Dilate( alignedImage, alignedImage )
         accumulatorArray = accumulatorArray - alignedImage
         
     accumulatorImage = np.clip( accumulatorArray, 0, 255 ).astype( np.uint8 )
     
     # Create the segmentation mask from the accumulator image
     startMask = np.copy( accumulatorImage )
     cv.Dilate( startMask, startMask )
     cv.Erode( startMask, startMask )
     cv.Dilate( startMask, startMask )
     cv.Erode( startMask, startMask )
     startMask = scipy.ndimage.filters.gaussian_filter( 
         startMask, 5.0, mode='constant' )
     
     startMask[ startMask > 0 ] = 255
         
     # Find the larget blob in the ROI
     # Label blobs
     startMask, numBlobs = PyBlobLib.labelBlobs( startMask )
     
     # Find blobs in the region of interest
     testMap = np.copy( startMask )
     testMap[ :ROI_Y, : ] = 0       # Mask out area above the ROI
     testMap[ :, :ROI_X ] = 0       # Mask out area to the left of the ROI
     testMap[ ROI_Y+ROI_HEIGHT: ] = 0   # Mask out area below the ROI
     testMap[ :, ROI_X+ROI_WIDTH: ] = 0   # Mask out area to the right of the ROI
 
     biggestBlobIdx = None
     biggestBlobSize = 0
 
     for blobIdx in range( 1, numBlobs + 1 ):
         if testMap[ testMap == blobIdx ].size > 0:
             blobSize = startMask[ startMask == blobIdx ].size
             if blobSize > biggestBlobSize:
                 biggestBlobSize = blobSize
                 biggestBlobIdx = blobIdx
 
     # Isolate the largest blob
     if biggestBlobIdx != None:
         biggestBlobPixels = (startMask == biggestBlobIdx)
         startMask[ biggestBlobPixels ] = 255
         startMask[ biggestBlobPixels == False ] = 0
     else:
         print "No central blob"
         return blankFrame
         
     # Now expand it to get exclusion mask
     exclusionMask = np.copy( startMask )
     for i in range( 10 ):
         cv.Dilate( exclusionMask, exclusionMask )
     cv.Erode( exclusionMask, exclusionMask )
     cv.Erode( exclusionMask, exclusionMask )
     
     #----------------------------------------------------
     
     maskArray = np.copy( startMask )
     possiblyForeground = ( maskArray > 0 ) & ( accumulatorImage > 0 )
     maskArray[ possiblyForeground ] = self.GC_PR_FGD
     maskArray[ possiblyForeground == False ] = self.GC_PR_BGD
     maskArray[ exclusionMask == 0 ] = self.GC_BGD
     
     definiteMask = np.copy( accumulatorImage )
     definiteMask[ possiblyForeground ] = 255
     definiteMask[ possiblyForeground == False ] = 0
     cv.Erode( definiteMask, definiteMask )
     cv.Erode( definiteMask, definiteMask )
     maskArray[ definiteMask == 255 ] = self.GC_FGD
     
     # Now create the working mask and segment the image
     
     workingMask = np.copy( maskArray )
         
     fgModel = cv.CreateMat( 1, 5*13, cv.CV_64FC1 )
     cv.Set( fgModel, 0 )
     bgModel = cv.CreateMat( 1, 5*13, cv.CV_64FC1 )
     cv.Set( bgModel, 0 )
     
     workingImage = np.copy( startFrame )
     cv.GrabCut( workingImage, workingMask, 
         (0,0,0,0), fgModel, bgModel, 6, self.GC_INIT_WITH_MASK )
         
     cv.Set( fgModel, 0 )
     cv.Set( bgModel, 0 )
     bgdPixels = (workingMask != self.GC_PR_FGD) & (workingMask != self.GC_FGD)
     workingMask[ bgdPixels ] = 0
     workingMask[ bgdPixels == False ] = 255
     cv.Erode( workingMask, workingMask )
     bgdPixels = workingMask == 0
     workingMask[ bgdPixels ] = self.GC_PR_BGD
     workingMask[ bgdPixels == False ] = self.GC_PR_FGD
     workingMask[ exclusionMask == 0 ] = self.GC_BGD
     
     cv.GrabCut( workingImage, workingMask, 
         (0,0,0,0), fgModel, bgModel, 6, self.GC_INIT_WITH_MASK )
     
     segmentation = np.copy( startFrame )
     segmentation[ (workingMask != self.GC_PR_FGD) & (workingMask != self.GC_FGD) ] = 0
     
     # Remove everything apart from the biggest blob in the ROI
     graySeg = np.zeros( ( startFrame.height, startFrame.width ), dtype=np.uint8 )
     cv.CvtColor( segmentation, graySeg, cv.CV_RGB2GRAY )
     startMask = np.copy( graySeg )
     startMask[ startMask > 0 ] = 255
         
     # Find the larget blob in the ROI
     
     # Label blobs
     startMask, numBlobs = PyBlobLib.labelBlobs( startMask )
     
     # Find blobs in the region of interest
     testMap = np.copy( startMask )
     testMap[ :ROI_Y, : ] = 0       # Mask out area above the ROI
     testMap[ :, :ROI_X ] = 0       # Mask out area to the left of the ROI
     testMap[ ROI_Y+ROI_HEIGHT: ] = 0   # Mask out area below the ROI
     testMap[ :, ROI_X+ROI_WIDTH: ] = 0   # Mask out area to the right of the ROI
 
     biggestBlobIdx = None
     biggestBlobSize = 0
 
     for blobIdx in range( 1, numBlobs + 1 ):
         if testMap[ testMap == blobIdx ].size > 0:
             blobSize = startMask[ startMask == blobIdx ].size
             if blobSize > biggestBlobSize:
                 biggestBlobSize = blobSize
                 biggestBlobIdx = blobIdx
 
     # Isolate the largest blob
     if biggestBlobIdx != None:
         biggestBlobPixels = (startMask == biggestBlobIdx)
         segmentation[ biggestBlobPixels == False, 0 ] = 255
         segmentation[ biggestBlobPixels == False, 1 ] = 0
         segmentation[ biggestBlobPixels == False, 2 ] = 255
     else:
         print "No central blob after main segmentation"
         return blankFrame
     
     return segmentation
 def updateMaskImage( self ):
     
     USING_OPTICAL_FLOW = False
     ROI_X = 0
     ROI_Y = 76
     ROI_WIDTH = 230
     ROI_HEIGHT = 100
     
     if self.accumulatorImage != None:
         
         # Create the segmentation mask from the accumulator image
         startMask = np.copy( self.accumulatorImage )
         cv.Dilate( startMask, startMask )
         cv.Erode( startMask, startMask )
         cv.Dilate( startMask, startMask )
         cv.Erode( startMask, startMask )
         startMask = scipy.ndimage.filters.gaussian_filter( 
             startMask, 5.0, mode='constant' )
         
         startMask[ startMask > 0 ] = 255
         #cv.Erode( startMask, startMask )
         #cv.Dilate( startMask, startMask )
         
         #if USING_OPTICAL_FLOW:
         #    cv.Erode( startMask, startMask )
         #    cv.Erode( startMask, startMask )
             
         # Find the larget blob in the ROI
         # Label blobs
         startMask, numBlobs = PyBlobLib.labelBlobs( startMask )
         
         # Find blobs in the region of interest
         testMap = np.copy( startMask )
         testMap[ :ROI_Y, : ] = 0       # Mask out area above the ROI
         testMap[ :, :ROI_X ] = 0       # Mask out area to the left of the ROI
         testMap[ ROI_Y+ROI_HEIGHT: ] = 0   # Mask out area below the ROI
         testMap[ :, ROI_X+ROI_WIDTH: ] = 0   # Mask out area to the right of the ROI
     
         biggestBlobIdx = None
         biggestBlobSize = 0
     
         for blobIdx in range( 1, numBlobs + 1 ):
             if testMap[ testMap == blobIdx ].size > 0:
                 blobSize = startMask[ startMask == blobIdx ].size
                 if blobSize > biggestBlobSize:
                     biggestBlobSize = blobSize
                     biggestBlobIdx = blobIdx
     
         # Isolate the largest blob
         if biggestBlobIdx != None:
             biggestBlobPixels = (startMask == biggestBlobIdx)
             startMask[ biggestBlobPixels ] = 255
             startMask[ biggestBlobPixels == False ] = 0
         else:
             print "No central blob"
             self.maskArray = None
             self.dwgMaskImageDisplay.clear()
             
         # Now expand it to get exclusion mask
         self.exclusionMask = np.copy( startMask )
         for i in range( 10 ):
             cv.Dilate( self.exclusionMask, self.exclusionMask )
         cv.Erode( self.exclusionMask, self.exclusionMask )
         cv.Erode( self.exclusionMask, self.exclusionMask )
         
         #----------------------------------------------------
         
         self.maskArray = np.copy( startMask )
         possiblyForeground = ( self.maskArray > 0 ) & ( self.accumulatorImage > 0 )
         self.maskArray[ possiblyForeground ] = self.GC_PR_FGD
         self.maskArray[ possiblyForeground == False ] = self.GC_PR_BGD
         self.maskArray[ self.exclusionMask == 0 ] = self.GC_BGD
         
         self.definiteMask = np.copy( self.accumulatorImage )
         self.definiteMask[ possiblyForeground ] = 255
         self.definiteMask[ possiblyForeground == False ] = 0
         cv.Erode( self.definiteMask, self.definiteMask )
         cv.Erode( self.definiteMask, self.definiteMask )
         self.maskArray[ self.definiteMask == 255 ] = self.GC_FGD
         
         #if not USING_OPTICAL_FLOW:
         #    smallMask = np.copy( startMask )
         #    smallMask[ smallMask > 0 ] = 255
         #    cv.Erode( smallMask, smallMask )
         #    self.maskArray[ smallMask > 0 ] = self.GC_FGD
         
         # Draw the segmentation mask
         composedImage = None
         if "ImpactImage" in self.images.keys() \
             and self.images[ "ImpactImage" ] != None:
             composedImage = np.copy( self.images[ "ImpactImage" ] )
             
         if composedImage == None:
             height = self.accumulatorImage.shape[ 1 ]
             width = self.accumulatorImage.shape[ 0 ]
             composedImage = np.zeros( ( height, width, 3 ), dtype=np.uint8 )
         
         composedImage[ self.maskArray == self.GC_FGD ] = self.FOREGROUND_BRUSH_COLOUR
         composedImage[ self.maskArray == self.GC_PR_FGD ] = self.PROBABLY_FOREGROUND_BRUSH_COLOUR
         composedImage[ self.maskArray == self.GC_BGD ] = self.BACKGROUND_BRUSH_COLOUR
         
         self.dwgMaskImageDisplay.setImageFromNumpyArray( composedImage )
     else:
         
         self.maskArray = None
         self.dwgMaskImageDisplay.clear()
    def updateMaskImage(self):

        USING_OPTICAL_FLOW = False
        ROI_X = 0
        ROI_Y = 76
        ROI_WIDTH = 230
        ROI_HEIGHT = 100

        if self.accumulatorImage != None:

            # Create the segmentation mask from the accumulator image
            startMask = np.copy(self.accumulatorImage)
            cv.Dilate(startMask, startMask)
            cv.Erode(startMask, startMask)
            cv.Dilate(startMask, startMask)
            cv.Erode(startMask, startMask)
            startMask = scipy.ndimage.filters.gaussian_filter(startMask,
                                                              5.0,
                                                              mode='constant')

            startMask[startMask > 0] = 255
            #cv.Erode( startMask, startMask )
            #cv.Dilate( startMask, startMask )

            #if USING_OPTICAL_FLOW:
            #    cv.Erode( startMask, startMask )
            #    cv.Erode( startMask, startMask )

            # Find the larget blob in the ROI
            # Label blobs
            startMask, numBlobs = PyBlobLib.labelBlobs(startMask)

            # Find blobs in the region of interest
            testMap = np.copy(startMask)
            testMap[:ROI_Y, :] = 0  # Mask out area above the ROI
            testMap[:, :ROI_X] = 0  # Mask out area to the left of the ROI
            testMap[ROI_Y + ROI_HEIGHT:] = 0  # Mask out area below the ROI
            testMap[:, ROI_X +
                    ROI_WIDTH:] = 0  # Mask out area to the right of the ROI

            biggestBlobIdx = None
            biggestBlobSize = 0

            for blobIdx in range(1, numBlobs + 1):
                if testMap[testMap == blobIdx].size > 0:
                    blobSize = startMask[startMask == blobIdx].size
                    if blobSize > biggestBlobSize:
                        biggestBlobSize = blobSize
                        biggestBlobIdx = blobIdx

            # Isolate the largest blob
            if biggestBlobIdx != None:
                biggestBlobPixels = (startMask == biggestBlobIdx)
                startMask[biggestBlobPixels] = 255
                startMask[biggestBlobPixels == False] = 0
            else:
                print "No central blob"
                self.maskArray = None
                self.dwgMaskImageDisplay.clear()

            # Now expand it to get exclusion mask
            self.exclusionMask = np.copy(startMask)
            for i in range(10):
                cv.Dilate(self.exclusionMask, self.exclusionMask)
            cv.Erode(self.exclusionMask, self.exclusionMask)
            cv.Erode(self.exclusionMask, self.exclusionMask)

            #----------------------------------------------------

            self.maskArray = np.copy(startMask)
            possiblyForeground = (self.maskArray > 0) & (self.accumulatorImage
                                                         > 0)
            self.maskArray[possiblyForeground] = self.GC_PR_FGD
            self.maskArray[possiblyForeground == False] = self.GC_PR_BGD
            self.maskArray[self.exclusionMask == 0] = self.GC_BGD

            self.definiteMask = np.copy(self.accumulatorImage)
            self.definiteMask[possiblyForeground] = 255
            self.definiteMask[possiblyForeground == False] = 0
            cv.Erode(self.definiteMask, self.definiteMask)
            cv.Erode(self.definiteMask, self.definiteMask)
            self.maskArray[self.definiteMask == 255] = self.GC_FGD

            #if not USING_OPTICAL_FLOW:
            #    smallMask = np.copy( startMask )
            #    smallMask[ smallMask > 0 ] = 255
            #    cv.Erode( smallMask, smallMask )
            #    self.maskArray[ smallMask > 0 ] = self.GC_FGD

            # Draw the segmentation mask
            composedImage = None
            if "ImpactImage" in self.images.keys() \
                and self.images[ "ImpactImage" ] != None:
                composedImage = np.copy(self.images["ImpactImage"])

            if composedImage == None:
                height = self.accumulatorImage.shape[1]
                width = self.accumulatorImage.shape[0]
                composedImage = np.zeros((height, width, 3), dtype=np.uint8)

            composedImage[self.maskArray ==
                          self.GC_FGD] = self.FOREGROUND_BRUSH_COLOUR
            composedImage[self.maskArray == self.
                          GC_PR_FGD] = self.PROBABLY_FOREGROUND_BRUSH_COLOUR
            composedImage[self.maskArray ==
                          self.GC_BGD] = self.BACKGROUND_BRUSH_COLOUR

            self.dwgMaskImageDisplay.setImageFromNumpyArray(composedImage)
        else:

            self.maskArray = None
            self.dwgMaskImageDisplay.clear()