11#include < cstring>
2+ #include < vector>
23#include < opencv2/core/mat.hpp>
34#include < opencv2/imgcodecs.hpp>
45#include < opencv2/imgproc.hpp>
56#include < opencv2/opencv.hpp>
67#include < emscripten.h>
78
9+ using namespace std ;
810using namespace cv ;
911
1012#ifdef __cplusplus
@@ -13,11 +15,34 @@ extern "C" {
1315
1416EMSCRIPTEN_KEEPALIVE
1517uchar *processImage (uchar *array, int width, int height) {
18+
1619 Mat mat (height, width, CV_8UC4, array);
20+
1721 cvtColor (mat, mat, COLOR_RGBA2GRAY);
22+
23+ auto ksize = Size (5 , 5 );
24+ auto sigmaX = 0 ;
25+ GaussianBlur (mat, mat, ksize, sigmaX);
26+
27+ auto maxValue = 255 ;
28+ auto adaptiveMethod = ADAPTIVE_THRESH_GAUSSIAN_C;
29+ auto thresholdType = THRESH_BINARY_INV;
30+ auto blockSize = 19 ;
31+ auto C = 3 ;
32+ adaptiveThreshold (mat, mat, maxValue, adaptiveMethod, thresholdType, blockSize, C);
33+
34+ vector<vector<Point>> contours;
35+ vector<Vec4i> hierarchy;
36+ auto mode = RETR_EXTERNAL;
37+ auto method = CHAIN_APPROX_SIMPLE;
38+ findContours (mat, contours, hierarchy, mode, method);
39+
40+ auto numContours = contours.size ();
41+ EM_ASM_ (console.log (`numContours: ${$0 }`), numContours);
42+
1843 const int cb = width * height * mat.channels ();
1944 uchar *array_copy = reinterpret_cast <uchar*>(malloc (cb));
20- std:: memcpy (array_copy, mat.data , cb);
45+ memcpy (array_copy, mat.data , cb);
2146 return array_copy;
2247}
2348
0 commit comments