Skip to content

Commit

Permalink
[Plugin] Added imgproc 9 functions to opencvmini plugin. (WasmEdge#2794)
Browse files Browse the repository at this point in the history
* Added imgproc's 9 functions.

Added BilateralFilter, BoxFilter, MiniDilate, MiniErode, GaussianBlur, MiniLaplacian, MedianBlur, PyrDown, PyrUp

Kernel is now added through function.

Added WasmEdgeOpenCVMiniEmptyMat

Signed-off-by: wck-iipi <21dcs006@nith.ac.in>

* Changed Expect in opencvmini_func.h

Signed-off-by: wck-iipi <21dcs006@nith.ac.in>

* Changed OpenCVMiniEmptyMat in opencvmini_func.cpp

Signed-off-by: wck-iipi <21dcs006@nith.ac.in>

* Updated tests for opencvmini

Signed-off-by: wck-iipi <21dcs006@nith.ac.in>

---------

Signed-off-by: wck-iipi <21dcs006@nith.ac.in>
  • Loading branch information
Wck-iipi authored Oct 6, 2023
1 parent dd276ee commit 630fb80
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 1 deletion.
107 changes: 107 additions & 0 deletions plugins/wasmedge_opencvmini/opencvmini_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

#include "opencvmini_func.h"
#include "common/defines.h"
#include "common/errcode.h"

#include <cstdint>
#include <opencv2/core/mat.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
Expand Down Expand Up @@ -67,6 +71,109 @@ Expect<uint32_t> WasmEdgeOpenCVMiniBlur::body(const Runtime::CallingFrame &,
return Env.insertMat(Dst);
}

Expect<uint32_t>
WasmEdgeOpenCVMiniBilateralFilter::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey, uint32_t D,
double SigmaColor, double SigmaSpace) {
cv::Mat Dst;
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::bilateralFilter(*Src, Dst, D, SigmaColor, SigmaSpace);
}
return Env.insertMat(Dst);
}

Expect<uint32_t>
WasmEdgeOpenCVMiniBoxFilter::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey, uint32_t Ddepth,
uint32_t KernelWidth, uint32_t KernelHeight) {
cv::Mat Dst;
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::boxFilter(*Src, Dst, Ddepth, cv::Size(KernelWidth, KernelHeight));
}
return Env.insertMat(Dst);
}

Expect<uint32_t>
WasmEdgeOpenCVMiniEmptyMat::body(const Runtime::CallingFrame &) {
cv::Mat Kernel;
return Env.insertMat(Kernel);
}

Expect<uint32_t> WasmEdgeOpenCVMiniDilate::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey,
uint32_t KernelMatKey) {
cv::Mat Dst;
auto Kernel = Env.getMat(KernelMatKey);
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::dilate(*Src, Dst, *Kernel);
}
return Env.insertMat(Dst);
}

Expect<uint32_t> WasmEdgeOpenCVMiniErode::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey,
uint32_t KernelMatKey) {
cv::Mat Dst;
auto Kernel = Env.getMat(KernelMatKey);
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::erode(*Src, Dst, *Kernel);
}
return Env.insertMat(Dst);
}

Expect<uint32_t>
WasmEdgeOpenCVMiniGaussianBlur::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey, uint32_t KernelWidth,
uint32_t KernelHeight, double SigmaX) {
cv::Mat Dst;
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::GaussianBlur(*Src, Dst, cv::Size(KernelWidth, KernelHeight), SigmaX);
}
return Env.insertMat(Dst);
}

Expect<uint32_t>
WasmEdgeOpenCVMiniLaplacian::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey, uint32_t Ddepth) {
cv::Mat Dst;
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::Laplacian(*Src, Dst, Ddepth);
}
return Env.insertMat(Dst);
}

Expect<uint32_t>
WasmEdgeOpenCVMiniMedianBlur::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey, uint32_t Ksize) {
cv::Mat Dst;
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::medianBlur(*Src, Dst, Ksize);
}
return Env.insertMat(Dst);
}

Expect<uint32_t> WasmEdgeOpenCVMiniPyrDown::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey,
uint32_t KernelWidth,
uint32_t KernelHeight) {
cv::Mat Dst;
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::pyrDown(*Src, Dst, cv::Size(KernelWidth, KernelHeight));
}
return Env.insertMat(Dst);
}

Expect<uint32_t> WasmEdgeOpenCVMiniPyrUp::body(const Runtime::CallingFrame &,
uint32_t SrcMatKey,
uint32_t KernelWidth,
uint32_t KernelHeight) {
cv::Mat Dst;
if (auto Src = Env.getMat(SrcMatKey); Src) {
cv::pyrUp(*Src, Dst, cv::Size(KernelWidth, KernelHeight));
}
return Env.insertMat(Dst);
}

Expect<void> WasmEdgeOpenCVMiniImwrite::body(const Runtime::CallingFrame &Frame,
uint32_t TargetFileNamePtr,
uint32_t TargetFileNameLen,
Expand Down
92 changes: 92 additions & 0 deletions plugins/wasmedge_opencvmini/opencvmini_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class WasmEdgeOpenCVMiniBlur
uint32_t KernelWidth, uint32_t KernelHeight);
};

class WasmEdgeOpenCVMiniBilateralFilter
: public WasmEdgeOpenCVMini<WasmEdgeOpenCVMiniBilateralFilter> {
public:
WasmEdgeOpenCVMiniBilateralFilter(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}

Expect<uint32_t> body(const Runtime::CallingFrame &Frame, uint32_t SrcMatKey,
uint32_t D, double SigmaColor, double SigmaSpace);
};

class WasmEdgeOpenCVMiniImwrite
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniImwrite> {
public:
Expand Down Expand Up @@ -91,6 +101,88 @@ class WasmEdgeOpenCVMiniBilinearSampling
uint32_t OutImgW, uint32_t OutImgH);
};

class WasmEdgeOpenCVMiniBoxFilter
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniBoxFilter> {
public:
WasmEdgeOpenCVMiniBoxFilter(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t Ddepth, uint32_t KernelWidth,
uint32_t KernelHeight);
};

class WasmEdgeOpenCVMiniEmptyMat
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniEmptyMat> {
public:
WasmEdgeOpenCVMiniEmptyMat(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &);
};

class WasmEdgeOpenCVMiniDilate
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniDilate> {
public:
WasmEdgeOpenCVMiniDilate(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t KernelMatKey);
};

class WasmEdgeOpenCVMiniErode
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniErode> {
public:
WasmEdgeOpenCVMiniErode(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t Kernel);
};

class WasmEdgeOpenCVMiniGaussianBlur
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniGaussianBlur> {
public:
WasmEdgeOpenCVMiniGaussianBlur(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t KernelWidth, uint32_t KernelHeight,
double SigmaX);
};

class WasmEdgeOpenCVMiniLaplacian
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniLaplacian> {
public:
WasmEdgeOpenCVMiniLaplacian(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t Ddepth);
};

class WasmEdgeOpenCVMiniMedianBlur
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniMedianBlur> {
public:
WasmEdgeOpenCVMiniMedianBlur(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t Ksize);
};

class WasmEdgeOpenCVMiniPyrDown
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniPyrDown> {
public:
WasmEdgeOpenCVMiniPyrDown(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t KernelWidth, uint32_t KernelHeight);
};

class WasmEdgeOpenCVMiniPyrUp
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniPyrUp> {
public:
WasmEdgeOpenCVMiniPyrUp(WasmEdgeOpenCVMiniEnvironment &HostEnv)
: WasmEdgeOpenCVMini(HostEnv) {}
Expect<uint32_t> body(const Runtime::CallingFrame &, uint32_t SrcMatKey,
uint32_t KernelWidth, uint32_t KernelHeight);
};

class WasmEdgeOpenCVMiniRectangle
: public WasmEdgeOpenCVMini<class WasmEdgeOpenCVMiniRectangle> {
public:
Expand Down
20 changes: 20 additions & 0 deletions plugins/wasmedge_opencvmini/opencvmini_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ WasmEdgeOpenCVMiniModule::WasmEdgeOpenCVMiniModule()

addHostFunc("wasmedge_opencvmini_blur",
std::make_unique<WasmEdgeOpenCVMiniBlur>(Env));

addHostFunc("wasmedge_opencvmini_bilateral_filter",
std::make_unique<WasmEdgeOpenCVMiniBilateralFilter>(Env));

addHostFunc("wasmedge_opencvmini_box_filter",
std::make_unique<WasmEdgeOpenCVMiniBoxFilter>(Env));
addHostFunc("wasmedge_opencvmin_dilate",
std::make_unique<WasmEdgeOpenCVMiniDilate>(Env));
addHostFunc("wasmedge_opencvmini_erode",
std::make_unique<WasmEdgeOpenCVMiniErode>(Env));
addHostFunc("wasmedge_opencvmini_gaussian_blur",
std::make_unique<WasmEdgeOpenCVMiniGaussianBlur>(Env));
addHostFunc("wasmedge_opencvmini_Laplacian",
std::make_unique<WasmEdgeOpenCVMiniLaplacian>(Env));
addHostFunc("wasmedge_opencvmini_median_blur",
std::make_unique<WasmEdgeOpenCVMiniMedianBlur>(Env));
addHostFunc("wasmedge_opencvmini_pyrDown",
std::make_unique<WasmEdgeOpenCVMiniPyrDown>(Env));
addHostFunc("wasmedge_opencvmini_pyrUp",
std::make_unique<WasmEdgeOpenCVMiniPyrUp>(Env));
addHostFunc("wasmedge_opencvmini_normalize",
std::make_unique<WasmEdgeOpenCVMiniNormalize>(Env));
addHostFunc("wasmedge_opencvmini_bilinear_sampling",
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/wasmedge_opencvmini/wasmedge_opencvmini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST(WasmEdgeOpecvminiTest, Module) {
auto *ImgMod =
dynamic_cast<WasmEdge::Host::WasmEdgeOpenCVMiniModule *>(createModule());
EXPECT_FALSE(ImgMod == nullptr);
EXPECT_EQ(ImgMod->getFuncExportNum(), 10U);
EXPECT_EQ(ImgMod->getFuncExportNum(), 19U);
EXPECT_NE(ImgMod->findFuncExports("wasmedge_opencvmini_imdecode"), nullptr);
EXPECT_NE(ImgMod->findFuncExports("wasmedge_opencvmini_imencode"), nullptr);
EXPECT_NE(ImgMod->findFuncExports("wasmedge_opencvmini_rectangle"), nullptr);
Expand Down

0 comments on commit 630fb80

Please sign in to comment.