Open libaineu2004 opened 4 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();
}
#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?
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.
Could you please write an example? Use lua to implement this C++ source code. Thank you!