Convert CvMat to GstBuffer

raomayurs raomayurs at gmail.com
Thu Jun 8 22:34:36 UTC 2017


Hi,
I am a newbie to gstreamer plugin development. I am trying to create a
plugin using the tool provided by gstreamer. I am following the plugin
writer's guide. I am trying to process data in the _chain function. I was
able to successfully extract the video frame from GstBuffer and do my
processing using openCV. But now I am unable to reconvert CvMat into
GstBuffer.

Here is my code:

GBuffer* process_data(GstPad *sinkPad, GstMyFilter *filter, GstBuffer *buf)
{
 //Get Height and width of input frame
  gint gheight, gwidth;
  static GstClockTime timestamp = 0;
  int height, width, channels = 3;
  GstCaps *caps = gst_pad_get_current_caps(sinkPad);
  GstStructure *str = gst_caps_get_structure (caps, 0);
  if (!gst_structure_get_int (str, "width", &gwidth)) {
    width = 1280;
  } else {
    width = (int) gwidth;
  }
  if (!gst_structure_get_int (str, "height", &gheight)) {
    height = 720;
  } else {
    height = (int) gheight;
  }

// convert buffer to opencv mat
  GstMapInfo map;
  gst_buffer_map (buf, &map, GST_MAP_READ); 
  CvMat frame_yuv = cvMat(height + height/2, width, CV_8UC1, map.data);
  CvMat *frame = cvCreateMat(height, width, CV_8UC3);
  cvCvtColor(&frame_yuv, frame, CV_YUV2RGB_YV12);

// Process frame  
  CvMat *r = cvCreateMat(height, width, CV_8UC1);
  CvMat *g = cvCreateMat(height, width, CV_8UC1);
  CvMat *b = cvCreateMat(height, width, CV_8UC1);

  CvMat cameraMatrix = cvMat(3, 3, CV_64F, filter->cameraMatrix);                                                                                                                                                                                       
  CvMat distortionCoefficients = cvMat(5, 1, CV_64F,
filter->distortionCoefficients);
  CvMat *newcamera_matrix = cvCreateMat(3, 3, CV_64F);
  CvMat *r1 = cvCreateMat(height, width, CV_8UC1);
  CvMat *g1 = cvCreateMat(height, width, CV_8UC1);
  CvMat *b1 = cvCreateMat(height, width, CV_8UC1);
  CvMat *undistorted = cvCreateMat(height, width, CV_8UC3);
  CvSize imgSize = cvSize(width, height);
  cvGetOptimalNewCameraMatrix(&cameraMatrix, &distortionCoefficients,
imgSize, 1.0, newcamera_matrix, imgSize, NULL, 0);
  cvSplit(frame, r,g,b, NULL);
  cvUndistort2(r, r1, &cameraMatrix, &distortionCoefficients,
newcamera_matrix);
  cvUndistort2(g, g1, &cameraMatrix, &distortionCoefficients,
newcamera_matrix);
  cvUndistort2(b, b1, &cameraMatrix, &distortionCoefficients,
newcamera_matrix);
  cvMerge(1r,g1,b1, NULL, undistorted);

//reconvert processed cvMat into GstBuffer
  GstMapInfo mapinfo;
  GstBuffer *buffer;
  gint size = height * width * channels;
  buffer = gst_buffer_new_allocate (NULL, size, NULL);
  gst_buffer_map (buffer, &mapinfo, GST_MAP_WRITE);
  memcpy( (guchar *)map.data, (guchar *)&(undistorted->data), 
gst_buffer_get_size( buffer ) );
  GST_BUFFER_PTS (buffer) = timestamp;
  GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (1, GST_SECOND,
2);

  timestamp += GST_BUFFER_DURATION (buffer);
  return buffer;
}

The returned buffer is passed to the src pad of the filter. The above code
is not creating the buffer correctly. Am I converting cvMat to GstBuffer
correctly? Because the openCV part is working correctly. The undistorted
matrix is as expected. But the conversion to GstBuffer is failing for some
reason. Any help will be really appreciated. 

Thanks



--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/Convert-CvMat-to-GstBuffer-tp4683259.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.


More information about the gstreamer-devel mailing list