首页 > 解决方案 > 将 cv::Mat 存储在 Swift 变量中

问题描述

我有一个 c++ opencv 函数,它获取图像的灰度并将其存储到数组中。

+(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 将其存储在变量中时

快捷功能:

@IBAction func convertImage(_ sender: UIButton) {
    let grayMat = OpenCVWrapper.makeGray(photoImageView.image)
}

整个程序只是冻结而不会抛出错误。

当我返回 UIImage 时,swift 函数效果很好:

+(UIImage *) 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 MatToUIImage(grayMat);
}

但我希望返回 3darray 而不是图像。

标签: c++iosswiftopencv

解决方案


推荐阅读