kunitoki / LuaBridge3

A lightweight, dependency-free library for binding Lua to C++
https://kunitoki.github.io/LuaBridge3/Manual
Other
253 stars 40 forks source link

Can LuaBridge3 be bind to opencv? #192

Open libaineu2004 opened 4 days ago

libaineu2004 commented 4 days ago

https://opencv.org/

I want to use LuaBridge3 to bind the opencv library. But I don't know how to implement it? For example, reading and binarizing images.

cv::Mat src = cv::imread("demo.png", IMREAD_GRAYSCALE);
if (src.empty())
{
return;
}
cv::Mat dst;
cv::threshold(src, dst, 128, 255, cv::THRESH_BINARY);
cv::namedWindow("binary", cv::WINDOW_NORMAL);
cv::imshow("binary", dst);

Could you please write an example? Use lua to implement this C++ source code. Thank you!

kunitoki commented 3 days ago

Should be pretty easy, untested code from gpt:

void luaBindOpenCV(lua_State* L) {
    using namespace luabridge;

    getGlobalNamespace(L)
        .beginNamespace("cv")
        .beginClass<cv::Mat>("Mat")
        .addConstructor<void (*)()>()
        .addFunction("empty", &cv::Mat::empty)
        .endClass()
        .addFunction("imread", [](const std::string& filename, int flags) -> cv::Mat {
            return cv::imread(filename, flags);
        })
        .addFunction("threshold", [](const cv::Mat& src, cv::Mat& dst, double thresh, double maxval, int type) {
            cv::threshold(src, dst, thresh, maxval, type);
        })
        .addFunction("imshow", [](const std::string& winname, const cv::Mat& mat) {
            cv::imshow(winname, mat);
        })
        .endNamespace();
}
libaineu2004 commented 3 days ago
#include <iostream>
#include <lua.hpp>
#include <opencv2/opencv.hpp>
#include <LuaBridge3/LuaBridge.h>

using namespace std;
using namespace cv;

void luaBindOpenCV(lua_State *L)
{
    using namespace luabridge;

    getGlobalNamespace(L)
        .beginNamespace("cv")
        .beginClass<cv::Mat>("Mat")
        .addConstructor<void (*)()>()
        .addFunction("empty", &cv::Mat::empty)
        .endClass()
        .addFunction("imread", [](const std::string &filename, int flags) -> cv::Mat {
            return cv::imread(filename, flags);
        })
        .addFunction("threshold", [](const cv::Mat &src, cv::Mat &dst, double thresh, double maxval, int type) {
            cv::threshold(src, dst, thresh, maxval, type);
        })
        .addFunction("imshow", [](const std::string &winname, const cv::Mat &mat) {
            cv::namedWindow(winname, cv::WINDOW_NORMAL);
            cv::imshow(winname, mat);
        })
        .endNamespace();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaBindOpenCV(L);

    int ret = luaL_dostring(L, R"(
    src = cv.Mat()
    dst = cv.Mat()
    src = cv.imread('d:/0.jpg', 0)
    if (src.empty)
    then
    print('null image')
    return
    end
    cv.threshold(src, dst, 30, 255, 0)
    cv.imshow('Image', dst)
    )");

    if (ret != LUA_OK)
    {
        std::cerr << "Failed to execute Lua code: "
                  << lua_tostring(L, -1) << std::endl;
    }

    lua_close(L);

    return a.exec();
}

compile ok, but run error: if (src.empty) --- This conditional statement is always true why?