【问题标题】:How do I decode an image bytes sent between two UWP apps using OpenCV?如何使用 OpenCV 解码两个 UWP 应用程序之间发送的图像字节?
【发布时间】:2020-10-22 09:52:00
【问题描述】:

我正在使用 UWP AppService 将转换为字节 [] 的 png 图像从一个应用程序 (Unity/C#) 发送到 OpenCV 应用程序 (C++) 以进行图像处理。我在传递的 byte[] 上运行 cv::imdecode 时遇到问题,因为 Mat.data 成员总是以 NULL 结尾...我认为将字节数组从 C# 发送到 C++ 时可能存在格式问题?

        // Get bytes from webcam using Unity's Texture.EncodeToPNG() function
        Byte[] imageBytes = null;
        imageBytes = Communications.GetColorBytes();

        if (imageBytes != null)
        {
            // Convert bytes to BitMapImage to display on XAML
            BitmapImage image = await ImageFromBytes(imageBytes);
            WebcamImage.Source = image;

            //Send a message to the app service
            var inputs = new ValueSet();
            inputs.Add("data", imageBytes);
            AppServiceResponse response = await this.connection.SendMessageAsync(inputs);

            //If the service responded display the message. We're done!
            if (response.Status == AppServiceResponseStatus.Success)
            {
                Byte[] test = response.Message["result"] as Byte[];
                image = await ImageFromBytes(test);
                WebcamImageProcessed.Source = image;
                return;
            }
        }

这是解码发送字节[]的 C++ OpenCV 代码

    auto input = args->Request->Message;

    Platform::Array<unsigned char>^ inputData = safe_cast<Platform::IBoxArray<unsigned char>^>(input->Lookup("data"))->Value;
    int size = inputData->Length;

    // Create the response
    auto result = ref new ValueSet();

    // decode byte[] and display image on another screen
    auto buf = inputData->Data;
    std::vector<unsigned char> data(buf, buf + size);
    cv::Mat img_scene = cv::imdecode(data, cv::IMREAD_UNCHANGED);

    if (img_scene.data == NULL) {
        result->Insert("error", 0);
    }
    else {
        cv::namedWindow("Gray image", cv::WindowFlags::WINDOW_AUTOSIZE);
        cv::imshow("Gray image", img_scene);

        cv::waitKey(0);
    }

【问题讨论】:

  • 我正在使用C++/CX

标签: c# c++ opencv uwp c++-cx


【解决方案1】:

回答:How to come from C# stream to OpenCV Mat by using c++/cli?

cv::imdecode 必须接受另一个 Matrix 作为它的第一个参数

        auto buf = inputData->Data;
        cv::Mat img_data1(size, 1, type, buf);
        cv::Mat img_scene = cv::imdecode(img_data1, cv::IMREAD_UNCHANGED);

【讨论】:

    猜你喜欢
    • 2018-08-19
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多