【发布时间】: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