c++ - OpenCV cv::Mat to short* (avoiding memcpy) -
i have c++ function called else's c# application. input function given array of signed short integers, dimensions of image represents, , memory allocated returning data, namely array of signed short integers. represent function's header:
my_function (short* input, int height, int width, short* output)
inside function create cv::mat input
, this:
cv::mat mat_in = cv::mat (height, width, cv_16s, input);
this mat_in
converted cv_32f
, processed opencv's cv::bilateralfilter
. after returns cv::mat mat_out, convert data cv_16s
(bilateralfilter
accepts cv_8u
, cv_32f
). need convert cv::mat mat_out
array of short integers may returned calling function. code:
my_function (short* input, int height, int width, short* output) { mat mat_in_16s = mat (height, width, cv_16s, input); mat mat_in_32f = mat (height, width, cv_32f); mat mat_out_cv_32f = mat (height, width, cv_32f); mat_in_16s.convertto (mat_in_32f, cv_32f); bilateralfilter (mat_in_32f, mat_out_32f, 5, 160, 2); mat mat_out_16s = mat (mat_in_16s.size(), mat_in_16s.type()); mat_out_32f.convertto (mat_out_16s, cv_16s); return 0; }
obviously, somewhere there @ end need data in mat_out_16s
output
. first try return reference:
output = &mat_out_16s.at<short>(0,0);
but of course realised silly idea, mat_out_16s
goes out of scope function returns, leaving output
pointing @ emptiness. best attempt follows (from this question):
memcpy ((short*)output, (short*)mat_out_16s.data, height*width*sizeof(short));
now know, there better way? feels kind of inefficient copy data, don't see else can do. can't return cv::mat unfortunately. if there no better way, current memcpy
method safe @ least? data 2-byte signed short integers, don't think there should issues padding, don't want run unpleasant surprises.
you can use constructor mat_out_16s
:
mat::mat(size size, int type, void* data, size_t step=auto_step)
so function be:
my_function (short* input, int height, int width, short* output) { mat mat_in_16s = mat (height, width, cv_16s, input); mat mat_in_32f = mat (height, width, cv_32f); mat mat_out_cv_32f = mat (height, width, cv_32f); mat_in_16s.convertto (mat_in_32f, cv_32f); bilateralfilter (mat_in_32f, mat_out_32f, 5, 160, 2); mat mat_out_16s = mat (mat_in_16s.size(), mat_in_16s.type(), output); mat_out_32f.convertto (mat_out_16s, cv_16s); return 0; }
Comments
Post a Comment