def computeForegroundMask(self, visualize=True):
        """
        Computes the foreground mask. The camera location of the foreground view should
        already be set correctly. Steps of the pipeline are

        1. Set the background camera to same location as foreground camera
        2. Render depth images from foreground and background scenes
        3. Compute change detection based on pair of depth images
        """

        # assume that foreground view is already in the correct place
        view_f = self.views['foreground']
        view_b = self.views['background']

        view_f.forceRender()
        cameraToWorld = director_utils.getCameraTransform(view_f.camera())

        director_utils.setCameraTransform(view_f.camera(), cameraToWorld)
        view_f.forceRender()

        director_utils.setCameraTransform(view_b.camera(), cameraToWorld)
        view_b.forceRender()

        self.updateDepthScanners()

        # get the depth images
        # make sure to convert them to int32 since we want to avoid wrap-around issues when using
        # uint16 types
        depth_img_foreground_raw = self.depthScanners[
            'foreground'].getDepthImageAsNumpyArray()
        depth_img_f = np.array(depth_img_foreground_raw, dtype=np.int32)
        depth_img_b = np.array(
            self.depthScanners['background'].getDepthImageAsNumpyArray(),
            dtype=np.int32)

        # zero values are interpreted as max-range measurements
        depth_img_b[depth_img_b == 0] = np.iinfo(np.int32).max
        depth_img_f[depth_img_f == 0] = np.iinfo(np.int32).max

        idx, mask = ChangeDetection.computeForegroundMaskFromDepthImagePair(
            depth_img_f, depth_img_b, self.threshold)

        depth_img_change = mask * depth_img_f

        returnData = dict()
        returnData['idx'] = idx
        returnData['mask'] = mask
        returnData['depth_img_foreground'] = depth_img_f
        returnData['depth_img_background'] = depth_img_b
        returnData['depth_img_foreground_raw'] = depth_img_foreground_raw
        returnData['depth_img_change'] = depth_img_change

        if visualize:
            # make sure to remap the mask to [0,255], otherwise it will be all black
            ChangeDetection.drawNumpyImage('Change Detection Mask', mask * 255)

        return returnData
    def testGetCameraTransforms(self):
        vis.updateFrame(vtk.vtkTransform(), "origin frame", view=self.views['background'])
        for viewType, view in self.views.iteritems():
            print "\n\nviewType: ", viewType
            cameraTransform = director_utils.getCameraTransform(view.camera())
            pos, quat = transformUtils.poseFromTransform(cameraTransform)
            print "pos: ", pos
            print "quat: ", quat


            vis.updateFrame(cameraTransform, viewType + "_frame")
    def computeForegroundMask(self, visualize=True):
        """
        Computes the foreground mask. The camera location of the foreground view should
        already be set correctly. Steps of the pipeline are

        1. Set the background camera to same location as foreground camera
        2. Render depth images from foreground and background scenes
        3. Compute change detection based on pair of depth images
        """

        # assume that foreground view is already in the correct place
        view_f = self.views['foreground']
        view_b = self.views['background']

        view_f.forceRender()
        cameraToWorld = director_utils.getCameraTransform(view_f.camera())

        director_utils.setCameraTransform(view_f.camera(), cameraToWorld)
        view_f.forceRender()

        director_utils.setCameraTransform(view_b.camera(), cameraToWorld)
        view_b.forceRender()

        self.updateDepthScanners()

        # get the depth images
        # make sure to convert them to int32 since we want to avoid wrap-around issues when using
        # uint16 types
        depth_img_foreground_raw = self.depthScanners['foreground'].getDepthImageAsNumpyArray()
        depth_img_f = np.array(depth_img_foreground_raw, dtype=np.int32)
        depth_img_b = np.array(self.depthScanners['background'].getDepthImageAsNumpyArray(), dtype=np.int32)

        # zero values are interpreted as max-range measurements
        depth_img_b[depth_img_b == 0] = np.iinfo(np.int32).max
        depth_img_f[depth_img_f == 0] = np.iinfo(np.int32).max

        idx, mask = ChangeDetection.computeForegroundMaskFromDepthImagePair(depth_img_f, depth_img_b, self.threshold)

        depth_img_change = mask * depth_img_f

        returnData = dict()
        returnData['idx'] = idx
        returnData['mask'] = mask
        returnData['depth_img_foreground'] = depth_img_f
        returnData['depth_img_background'] = depth_img_b
        returnData['depth_img_foreground_raw'] = depth_img_foreground_raw
        returnData['depth_img_change'] = depth_img_change


        if visualize:
            # make sure to remap the mask to [0,255], otherwise it will be all black
            ChangeDetection.drawNumpyImage('Change Detection Mask', mask*255)

        return returnData
    def testGetCameraTransforms(self):
        vis.updateFrame(vtk.vtkTransform(), "origin frame", view=self.views['background'])
        for viewType, view in self.views.iteritems():
            print "\n\nviewType: ", viewType
            cameraTransform = director_utils.getCameraTransform(view.camera())
            pos, quat = transformUtils.poseFromTransform(cameraTransform)
            print "pos: ", pos
            print "quat: ", quat


            vis.updateFrame(cameraTransform, viewType + "_frame")
    def computeForegroundMaskUsingCropStrategy(self, visualize=True):
        """
        Computes the foreground mask. The camera location of the foreground view should
        already be set correctly. Steps of the pipeline are

        1. Render depth image from cropped foreground
        2. Everything in the "crop" should be foreground for now
        """

        # assume that foreground view is already in the correct place
        view_f = self.views['foreground']
        view_b = self.views['background']

        view_f.forceRender()
        cameraToWorld = director_utils.getCameraTransform(view_f.camera())
        self.updateDepthScanners()

        # get the depth images
        # make sure to convert them to int32 since we want to avoid wrap-around issues when using
        # uint16 types
        depth_img_foreground_raw = self.depthScanners[
            'foreground'].getDepthImageAsNumpyArray()
        depth_img_f = np.array(depth_img_foreground_raw, dtype=np.int32)

        idx = depth_img_f > 0
        mask = np.zeros(np.shape(depth_img_f))
        mask[idx] = 1

        returnData = dict()
        returnData['idx'] = idx
        returnData['mask'] = mask
        returnData['depth_img_foreground_raw'] = depth_img_foreground_raw
        returnData['depth_img_foreground'] = depth_img_f

        if visualize:
            # make sure to remap the mask to [0,255], otherwise it will be all black
            ChangeDetection.drawNumpyImage('Change Detection Mask', mask * 255)

        return returnData
    def computeForegroundMaskUsingCropStrategy(self, visualize=True):
        """
        Computes the foreground mask. The camera location of the foreground view should
        already be set correctly. Steps of the pipeline are

        1. Render depth image from cropped foreground
        2. Everything in the "crop" should be foreground for now
        """

        # assume that foreground view is already in the correct place
        view_f = self.views['foreground']
        view_b = self.views['background']

        view_f.forceRender()
        cameraToWorld = director_utils.getCameraTransform(view_f.camera())
        self.updateDepthScanners()

        # get the depth images
        # make sure to convert them to int32 since we want to avoid wrap-around issues when using
        # uint16 types
        depth_img_foreground_raw = self.depthScanners['foreground'].getDepthImageAsNumpyArray()
        depth_img_f = np.array(depth_img_foreground_raw, dtype=np.int32)


        idx = depth_img_f > 0
        mask = np.zeros(np.shape(depth_img_f))
        mask[idx] = 1

        returnData = dict()
        returnData['idx'] = idx
        returnData['mask'] = mask
        returnData['depth_img_foreground_raw'] = depth_img_foreground_raw
        returnData['depth_img_foreground'] = depth_img_f

        if visualize:
            # make sure to remap the mask to [0,255], otherwise it will be all black
            ChangeDetection.drawNumpyImage('Change Detection Mask', mask*255)

        return returnData
 def testIdentity(self):
     view = self.views['foreground']
     cameraToWorld = director_utils.getCameraTransform(view.camera())
     self.setCameraTransform(cameraToWorld)
     self.updateDepthScanners()
 def showCameraTransform(self):
     t = director_utils.getCameraTransform(
         self.views['foreground'].camera())
     vis.updateFrame(t, 'camera transform', scale=0.15)
 def testIdentity(self):
     view = self.views['foreground']
     cameraToWorld = director_utils.getCameraTransform(view.camera())
     self.setCameraTransform(cameraToWorld)
     self.updateDepthScanners()
 def showCameraTransform(self):
     t = director_utils.getCameraTransform(self.views['foreground'].camera())
     vis.updateFrame(t, 'camera transform', scale=0.15)