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