How to get image from GstBuffer?

Dear supporter,

This question have confused me several days . I want to get the image data from GstBuffer after recognized. I have reference the function of gst_yoloplugin_transform_ip() and get_converted_mat().

My plotform is on TX2 deepstream 1.5 ,using gst-yoloplugin-tegra.

I added below code at the last function of attach_metadata_full_frame.
The recognized bbox have attached to GstBuffer(inbuf) by function gst_buffer_add_iva_meta_full,right?

It will have Segmentation fault error using below code,can you give me some suggeations for it.

My code:
dsmeta = gst_buffer_add_nvds_meta (inbuf, bbparams, free_ds_meta);
dsmeta->meta_type = NVDS_META_FRAME_INFO;
//my code below*
static gint coun=0;
gchar file[100];
cv::Mat save_in_mat,save_out_mat;
gpointer state = NULL;
GstMeta *gst_meta = NULL;
NvBufferParams buf_params;

GstMapInfo in_map_info;
int in_dmabuf_fd = 0;

gpointer mapped_ptr = NULL;
GstFlowReturn flow_ret = GST_FLOW_OK;


memset (&in_map_info, 0, sizeof (in_map_info));
if (!gst_buffer_map (inbuf, &in_map_info, GST_MAP_READ)) {
flow_ret = GST_FLOW_ERROR;
goto done;

}

if (ExtractFdFromNvBuffer (in_map_info.data, &in_dmabuf_fd)) {
  flow_ret = GST_FLOW_ERROR;
  goto done;
}


for (guint i = 0; i < batch_size; i++) {

  if (NvBufferGetParams (in_dmabuf_fd, &buf_params) != 0) {
    flow_ret = GST_FLOW_ERROR;
    goto done;
  }


	//NvBufferGetParams (in_dmabuf_fd, &buf_params);
	if (NvBufferMemMap(in_dmabuf_fd, 0, NvBufferMem_Read,&mapped_ptr) != 0) {
		    flow_ret = GST_FLOW_ERROR;
goto done;

}

	coun=coun+1;
	sprintf(file,"%u.jpg",coun);
	//cv::imwrite(file, batchBlob);
	//GstBuffer *buf = (GstBuffer *) inbuf->data;

	GstMapInfo map;
	//gst_buffer_map (inbuf, &map, GST_MAP_READ);
	gint width=1280;
	gint height=720;

	save_in_mat=cv::Mat (width, height, CV_8UC4, mapped_ptr, buf_params.pitch[0]);//cv::Mat::AUTO_STEP

	cv::cvtColor (save_in_mat, save_out_mat, CV_BGRA2BGR);

	cv::imwrite(file, save_out_mat);
}
done:
  if (mapped_ptr){
    NvBufferMemUnMap (in_dmabuf_fd, 0, &mapped_ptr);
  }
  if (in_dmabuf_fd){
	NvReleaseFd (in_dmabuf_fd);
  }
  gst_buffer_unmap (inbuf, &in_map_info);
  std::cout << "debug save image " <<":done end!"<< std::endl;
 //return flow_ret;
dsmeta = gst_buffer_add_nvds_meta (inbuf, bbparams, free_ds_meta);
 dsmeta->meta_type = NVDS_META_FRAME_INFO;
//*****my code below******
	static gint coun=0;
	gchar file[100];
	cv::Mat save_in_mat,save_out_mat;
	gpointer state = NULL;
	GstMeta *gst_meta = NULL;
	NvBufferParams buf_params;

	GstMapInfo in_map_info;
	int in_dmabuf_fd = 0;

	gpointer mapped_ptr = NULL;
	GstFlowReturn flow_ret = GST_FLOW_OK;


	memset (&in_map_info, 0, sizeof (in_map_info));
	if (!gst_buffer_map (inbuf, &in_map_info, GST_MAP_READ)) {
    flow_ret = GST_FLOW_ERROR;
    goto done;
  }


	if (ExtractFdFromNvBuffer (in_map_info.data, &in_dmabuf_fd)) {
	  flow_ret = GST_FLOW_ERROR;
	  goto done;
	}


	for (guint i = 0; i < batch_size; i++) {

	  if (NvBufferGetParams (in_dmabuf_fd, &buf_params) != 0) {
	    flow_ret = GST_FLOW_ERROR;
	    goto done;
	  }


		//NvBufferGetParams (in_dmabuf_fd, &buf_params);
		if (NvBufferMemMap(in_dmabuf_fd, 0, NvBufferMem_Read,&mapped_ptr) != 0) {
			    flow_ret = GST_FLOW_ERROR;
    goto done;
  }

		coun=coun+1;
		sprintf(file,"%u.jpg",coun);
		//cv::imwrite(file, batchBlob);
		//GstBuffer *buf = (GstBuffer *) inbuf->data;

		GstMapInfo map;
		//gst_buffer_map (inbuf, &map, GST_MAP_READ);
		gint width=1280;
		gint height=720;

		save_in_mat=cv::Mat (width, height, CV_8UC4, mapped_ptr, buf_params.pitch[0]);//cv::Mat::AUTO_STEP
	
		cv::cvtColor (save_in_mat, save_out_mat, CV_BGRA2BGR);

		cv::imwrite(file, save_out_mat);
	}
	done:
	  if (mapped_ptr){
	    NvBufferMemUnMap (in_dmabuf_fd, 0, &mapped_ptr);
	  }
	  if (in_dmabuf_fd){
    	NvReleaseFd (in_dmabuf_fd);
	  }
  	  gst_buffer_unmap (inbuf, &in_map_info);
	  std::cout << "debug save image " <<":done end!"<< std::endl;
	 //return flow_ret;

duplicate of [url]https://devtalk.nvidia.com/default/topic/1056796/[/url]