如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?

How do I decode an image bytes sent between two UWP apps using OpenCV?

本文关键字:之间 应用 字节 图像 UWP 两个 解码 OpenCV 何使用      更新时间:2023-10-16

我正在使用UWP应用服务将转换为byte[]的png图像从一个应用程序(Unity/C#(发送到OpenCV应用程序(C++(进行图像处理。我在传递的字节 [] 上运行 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++/cli 从 C# 流到 OpenCV Mat?

cv::imdecode必须为其第一个参数接收另一个矩阵

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