Skip to content

Instantly share code, notes, and snippets.

@atinfinity
Last active September 29, 2019 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atinfinity/002111e4753ca383ecb0dd0401732608 to your computer and use it in GitHub Desktop.
Save atinfinity/002111e4753ca383ecb0dd0401732608 to your computer and use it in GitHub Desktop.
G-API fluid kernel sample
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/gapi.hpp>
#include <opencv2/gapi/core.hpp>
#include <opencv2/gapi/imgproc.hpp>
#include <opencv2/gapi/fluid/gfluidkernel.hpp>
G_TYPED_KERNEL(TBitwiseNot, <cv::GMat(cv::GMat)>, "sample.fluid.bitwise_not")
{
static cv::GMatDesc outMeta(cv::GMatDesc in)
{
return in;
}
};
GAPI_FLUID_KERNEL(FBitwiseNot, TBitwiseNot, false)
{
static const int Window = 1;
static void run(const cv::gapi::fluid::View &src, cv::gapi::fluid::Buffer &dst)
{
const uint8_t* in = src.InLine<uint8_t>(0);
uint8_t* out = dst.OutLine<uint8_t>();
for (int i = 0, w = src.length(); i < w; i++)
{
out[i] = UINT8_MAX - in[i];
}
}
};
int main(int argc, const char* argv[])
{
cv::Mat src = cv::imread("graf1.png", cv::IMREAD_COLOR);
if (src.empty())
{
std::cerr << "could not load image." << std::endl;
return -1;
}
cv::GMat in;
cv::GMat gray = cv::gapi::BGR2Gray(in);
cv::GMat out = TBitwiseNot::on(gray);
cv::GComputation ac(in, out);
cv::gapi::GKernelPackage fluid_kernel = cv::gapi::kernels<FBitwiseNot>();
cv::Mat dst;
ac.apply(src, dst, cv::compile_args(fluid_kernel));
cv::imshow("dst", dst);
cv::waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment