Skip to content

Commit a660fde

Browse files
committed
Convert to binary and make basic call to findContours
1 parent 4205656 commit a660fde

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "emscripten-opencv",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Web app calling into C++ WebAssembly code that uses OpenCV",
55
"scripts": {
66
"build:wasm": "./build-wasm.sh",

src/hello.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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;
810
using namespace cv;
911

1012
#ifdef __cplusplus
@@ -13,11 +15,34 @@ extern "C" {
1315

1416
EMSCRIPTEN_KEEPALIVE
1517
uchar *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

Comments
 (0)