How to caputre Object Images from DeepStream

Hi,
I am curious about how to capture objects image after object detection.(1st GIE)

I think that I have to get images from OSD (RGBA) by using metadata info with Frame buffer so I read this doc below

https://docs.nvidia.com/metropolis/deepstream/4.0/dev-guide/DeepStream_Development_Guide/baggage/index.html

Could you give me advice about this?

I am developing a program about cropping images after I refered to test2/3 sample
I checked RTSP / FILE was working well

DeepStream
uridecodedbin -> nvstreammux- -> nvinfer -> nvtracker -> nvinfer -> nvinfer -> nvinfer -> nvvideoconvert -> nvdsosd -> nvegltransform -> nveglglessink

and then added NvBufSurfTransform into osd_sink_pad_buffer_probe
I met error messages after executing my program

Error Message

NvRmChannelSubmit: NvError_IoctlFailed with error code 22
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 54, SyncPointValue = 0)
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 54, SyncPointValue = 0)
......

I read this issue
in my case, I didn’t use createNvBuffer and NvBufSurfaceDestroy, only used NvBufSurfTransform based on GstBuffer
https://devtalk.nvidia.com/default/topic/1035330/jetson-tx2/-mmapi-r28-2-r28-1-deinitplane-of-nvvideoencoder-memory-leak-/1

osd_sink_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * info,   gpointer u_data)
{
........

    GstMapInfo in_map_info;
    gdouble scale_ratio = 1.0;
    NvBufSurface * surface;


    NvBufSurfTransformConfigParams transform_config_params;
    NvBufSurfTransformParams transform_params;
    NvBufSurfTransformRect src_rect;
    NvBufSurfTransformRect dst_rect;
    NvOSD_RectParams * crop_rect_params;
    NvBufSurfTransform_Error err;

[b]    memset (&in_map_info, 0, sizeof (in_map_info));
    if (!gst_buffer_map (buf, &in_map_info, GST_MAP_READ)) {
      g_print ("Error: Failed to map gst buffer\n");
    }

    surface = (NvBufSurface *) in_map_info.data;[/b]

    transform_config_params.compute_mode = NvBufSurfTransformCompute_Default;
    transform_config_params.gpu_id = 0;
    transform_config_params.cuda_stream = cuda_stream;   // already called cudaStreamCreate()

    // Set the transform session parameters for the conversions executed in this
    // thread.
     err = NvBufSurfTransformSetSessionParams (&transform_config_params);

    if (err != NvBufSurfTransformError_Success) {
    	g_print ("NvBufSurfTransformSetSessionParams failed with error %d", err);
     }

......
            		crop_rect_params = &obj_meta->rect_params;

					gint src_left = GST_ROUND_UP_2(crop_rect_params->left);
					gint src_top = GST_ROUND_UP_2(crop_rect_params->top);
					gint src_width = GST_ROUND_DOWN_2(crop_rect_params->width);
					gint src_height = GST_ROUND_DOWN_2(crop_rect_params->height);

					// Set the transform ROIs for source and destination
					src_rect.top   = src_top;
					src_rect.left  = src_left;
					src_rect.width = src_width;
					src_rect.height= src_height;

					dst_rect.top = 0 + (vehicle_count-1)*240  ;
					dst_rect.left = MUXER_OUTPUT_WIDTH-320;
					dst_rect.width = 320;
					dst_rect.height =240;

					// Set the transform parameters
					transform_params.src_rect = &src_rect;
					transform_params.dst_rect = &dst_rect;
					transform_params.transform_flag =  NVBUFSURF_TRANSFORM_FILTER  | NVBUFSURF_TRANSFORM_CROP_SRC |
							NVBUFSURF_TRANSFORM_CROP_DST;
					transform_params.transform_filter = NvBufSurfTransformInter_Default;

					//printf("fill=%d batch=%d \n ",surface->numFilled,surface->batchSize);


					// Transformation scaling+format conversion if any.
					<b>err = NvBufSurfTransform (surface, surface, &transform_params);</b>
					if (err != NvBufSurfTransformError_Success) {
					  g_print ("NvBufSurfTransform failed with error %d while converting buffer\n", err);
					}


}

Please refer to sources/gst-plugins/gst-dsexample/gstdsexample.cpp gst_dsexample_transform_ip()