def _step( self ):
        # grab image container from port using traits
        optical_c = self.grab_input_using_trait( 'optical_image' )
        thermal_c = self.grab_input_using_trait( 'thermal_image' )

        # Get python image from conatiner (just for show)
        optical_npy = optical_c.image().asarray().astype('uint8')
        thermal_npy = thermal_c.image().asarray().astype('uint16')

        thermal_norm = normalize_thermal( thermal_npy )

        if thermal_norm is not None and optical_npy is not None:
            # compute transform
            ret, transform, _ = compute_transform(
                optical_npy,
                thermal_norm,
                warp_mode = cv2.MOTION_HOMOGRAPHY,
                match_low_res = True,
                good_match_percent = self._good_match_percent,
                ratio_test = self._ratio_test,
                match_height = self._match_height,
                min_matches = self._min_matches,
                min_inliers = self._min_inliers )
        else:
           ret = False

        if ret:
            # TODO: Make all of these computations conditional on port connection
            inv_transform = np.linalg.inv( transform )

            thermal_warped = cv2.warpPerspective( thermal_npy, transform, \
              ( optical_npy.shape[1], optical_npy.shape[0] ) )
            optical_warped = cv2.warpPerspective( optical_npy, inv_transform, \
              ( thermal_npy.shape[1], thermal_npy.shape[0] ) )

            #self.push_to_port_using_trait( 'thermal_to_optical_homog',
            #   Homography.from_matrix( transform, 'd' )
            #self.push_to_port_using_trait( 'optical_to_thermal_homog',
            #   Homography.from_matrix( inv_transform, 'd' )

            self.push_to_port_using_trait( 'warped_thermal_image',
              ImageContainer.fromarray( thermal_warped ) )
            self.push_to_port_using_trait( 'warped_optical_image',
              ImageContainer.fromarray( optical_warped ) )
        else:
            print( 'alignment failed!' )

            #self.push_to_port_using_trait( "thermal_to_optical_homog", Homography() )
            #self.push_to_port_using_trait( "optical_to_thermal_homog", Homography() )

            self.push_to_port_using_trait( 'warped_optical_image', ImageContainer() )
            self.push_to_port_using_trait( 'warped_thermal_image', ImageContainer() )

        self._base_step()
Exemple #2
0
        def _test_numpy(dtype_name, nchannels, order='c'):
            np_img = create_numpy_image(dtype_name, nchannels, order)
            img_container = ImageContainer.fromarray(np_img)
            recast = img_container.asarray()

            # asarray always returns 3 channels
            np_img = np.atleast_3d(np_img)

            vital_img = img_container.image()
            pixel_type_name = vital_img.pixel_type_name()

            pixel_type_name = vital_img.pixel_type_name()
            want = map_dtype_name_to_pixel_type(dtype_name)

            assert pixel_type_name == want, 'want={} but got={}'.format(
                want, pixel_type_name)

            if not np.all(np_img == recast):
                raise AssertionError(
                    'Failed dtype={}, nchannels={}, order={}'.format(
                        dtype_name, nchannels, order))