Created
April 6, 2020 10:38
-
-
Save mmm444/203bc4b296f6884aabce7c2d74659d8f to your computer and use it in GitHub Desktop.
cv::Mat and Jitter Matrix conversions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
#include "c74_jitter.h" | |
using namespace c74::max; | |
void cvJitter2CvMat(t_object *x, t_object *jitMat, cv::Mat &mat) { | |
t_jit_matrix_info info; | |
char *data; | |
int type = 0; | |
if (!jitMat) { | |
object_error(x, "Error converting Jitter matrix: invalid pointer."); | |
return; | |
} | |
object_method(jitMat, _jit_sym_getinfo, (void*)&info); | |
if (info.dimcount != 2) { | |
object_error(x, "Error converting Jitter matrix: invalid dimension count."); | |
return; | |
} | |
if (info.type == _jit_sym_char) { | |
type = CV_MAKETYPE(CV_8U, info.planecount); | |
} else if (info.type == _jit_sym_long) { | |
type = CV_MAKETYPE(CV_32S, info.planecount); | |
} else if (info.type == _jit_sym_float32) { | |
type = CV_MAKETYPE(CV_32F, info.planecount); | |
} else if (info.type == _jit_sym_float64) { | |
type = CV_MAKETYPE(CV_64F, info.planecount); | |
} else { | |
object_error(x, "Error converting Jitter matrix: invalid data type."); | |
return; | |
} | |
object_method(jitMat, _jit_sym_getdata, (void*)&data); | |
//cvInitMatHeader(mat, info.dim[1], info.dim[0], type, data, info.dimstride[1] ); | |
mat = cv::Mat(info.dim[1], info.dim[0], type, data, info.dimstride[1]); | |
} | |
void cvMat2Jitter(t_object *x, cv::Mat &mat, t_object *jitMat) { | |
t_jit_matrix_info info; | |
if (!jitMat) { | |
object_error(x, "Error converting to Jitter matrix: invalid pointer."); | |
return; | |
} | |
object_method(jitMat, _jit_sym_getinfo, (void*) &info); | |
info.dimcount = 2; | |
info.planecount = CV_MAT_CN(mat.type()); | |
info.dim[0] = mat.cols; | |
info.dim[1] = mat.rows; | |
switch (CV_MAT_DEPTH(mat.type())) { | |
case CV_8U: | |
info.type = _jit_sym_char; | |
info.dimstride[0] = sizeof(char); | |
break; | |
case CV_32S: | |
info.type = _jit_sym_long; | |
info.dimstride[0] = sizeof(long); | |
break; | |
case CV_32F: | |
info.type = _jit_sym_float32; | |
info.dimstride[0] = sizeof(float); | |
break; | |
case CV_64F: | |
info.type = _jit_sym_float64; | |
info.dimstride[0] = sizeof(double); | |
break; | |
default: | |
object_error(x, "Error converting to Jitter matrix: unsupported data type."); | |
return; | |
} | |
info.dimstride[1] = mat.step; | |
info.size = mat.step * mat.rows; | |
info.flags = JIT_MATRIX_DATA_REFERENCE | JIT_MATRIX_DATA_FLAGS_USE; | |
object_method(jitMat, _jit_sym_setinfo_ex, (void*)&info); | |
object_method(jitMat, _jit_sym_data, mat.data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment