Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从C++;斯威夫特的矩阵?_C++_Ios_Swift_Opencv - Fatal编程技术网

C++ 如何从C++;斯威夫特的矩阵?

C++ 如何从C++;斯威夫特的矩阵?,c++,ios,swift,opencv,C++,Ios,Swift,Opencv,我有一个C++函数返回一个 cv::MATValue/P> +(cv::Mat *) makeGray:(UIImage *) image { // Transform UIImage to cv::Mat cv::Mat imageMat; UIImageToMat(image, imageMat); //Transform image to grayscale cv::Mat grayMat; cv::cvtColor(imageMat, g

我有一个C++函数返回一个<代码> cv::MATValue/P>
+(cv::Mat *) makeGray:(UIImage *) image {
    // Transform UIImage to cv::Mat
    cv::Mat imageMat;
    UIImageToMat(image, imageMat);

    //Transform image to grayscale
    cv::Mat grayMat;
    cv::cvtColor(imageMat, grayMat, CV_BGR2GRAY);
    return grayMat;
}
我使用Swift调用该函数:

let grayMat = OpenCVWrapper.makeGray(photoImageView.image)

我知道Swift不会导入
cv::Mat
,那么最好的替代方案是什么,这样我就可以完整地获得阵列了?

这应该可以。。只需将
cv::Mat
转换为
malloc
'd字节数组并返回该数组

+ (uint8_t *)makeGray:(UIImage *)image count:(uint32_t *)count {
    // Transform UIImage to cv::Mat
    cv::Mat imageMat;
    UIImageToMat(image, imageMat);

    //Transform image to grayscale
    cv::Mat grayMat;
    cv::cvtColor(imageMat, grayMat, CV_BGR2GRAY);

    int size = grayMat.total() * grayMat.elemSize();
    void *bytes = malloc(size);
    memcpy(bytes, grayMat.data, size);
    *count = size;
    return bytes;
}

然后在swift端,它应该是一个带有计数的
Unsafemtablepointer
,您可以通过调用deallocate释放它或将其转换为字节数组来释放它。

感谢您的回答,当我运行函数时,我得到了以下错误:
无法初始化类型为“uint8\u t*”的返回对象(也称为“unsigned char*”)用一个类型的“Value*”<代码>看起来C++认为代码>字节< /C>是无效的,即使当我打印到St:CUT时,我得到的值是:<代码> STD::CUTH@克里斯蒂安;do:
static\u cast(malloc(大小))
。。这是因为C++没有隐式地转换Maloc的返回值。我为我的无知道歉,到目前为止,我如何分配
UnsafeMutablePointer
我拥有的
let random:UnsafeMutablePointer=OpenCVWrapper.makeGray(photoImageView.image)
@Cristian;调用
random.deallocate
取消分配它。。不过,您可能需要先调用
去初始化