OpenCVComponent.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // OpenCVComponent.cpp
  2. #include "pch.h"
  3. #include "OpenCVComponent.h"
  4. #include <opencv2\imgproc\types_c.h>
  5. #include <opencv2\core.hpp>
  6. #include <opencv2\imgproc.hpp>
  7. #include <vector>
  8. #include <algorithm>
  9. using namespace OpenCVComponent;
  10. using namespace Platform;
  11. using namespace concurrency;
  12. using namespace Windows::Foundation;
  13. using namespace Windows::Foundation::Collections;
  14. void CopyIVectorToMatrix(IVector<int>^ input, cv::Mat& mat, int size);
  15. void CopyMatrixToVector(const cv::Mat& mat, std::vector<int>& vector, int size);
  16. OpenCVLib::OpenCVLib()
  17. {
  18. }
  19. IAsyncOperation<IVectorView<int>^>^ OpenCVLib::ProcessAsync(IVector<int>^ input, int width, int height)
  20. {
  21. int size = input->Size;
  22. cv::Mat mat(width, height, CV_8UC4);
  23. CopyIVectorToMatrix(input, mat, size);
  24. return create_async([=]() -> IVectorView<int>^
  25. {
  26. // convert to grayscale
  27. cv::Mat intermediateMat;
  28. cv::cvtColor(mat, intermediateMat, COLOR_RGB2GRAY);
  29. // convert to BGRA
  30. cv::cvtColor(intermediateMat, mat, COLOR_GRAY2BGRA);
  31. std::vector<int> output;
  32. CopyMatrixToVector(mat, output, size);
  33. // Return the outputs as a VectorView<float>
  34. return ref new Platform::Collections::VectorView<int>(output);
  35. });
  36. }
  37. void CopyIVectorToMatrix(IVector<int>^ input, cv::Mat& mat, int size)
  38. {
  39. unsigned char* data = mat.data;
  40. for (int i = 0; i < size; i++)
  41. {
  42. int value = input->GetAt(i);
  43. memcpy(data, (void*) &value, 4);
  44. data += 4;
  45. }
  46. }
  47. void CopyMatrixToVector(const cv::Mat& mat, std::vector<int>& vector, int size)
  48. {
  49. int* data = (int*) mat.data;
  50. for (int i = 0; i < size; i++)
  51. {
  52. vector.push_back(data[i]);
  53. }
  54. }