satoren/luaOpenCV

Need small example for cv.findContours

Closed this issue · 4 comments

Hello!
Can anybody show me how to use cv.findContours function in Lua, especially second parameter?

c++ prototype:
cv::findContours( InputOutputArray _image, OutputArrayOfArrays _contours,
OutputArray _hierarchy, int mode, int method, Point offset )

Thanks in advance!

I'm sorry. There is no idea right now.
I'm working on improving bind code generator.

Any luck with improving the bind code generator ? i'm also looking into how to call the findContours function from Lua but with no success.

And here is my lua implementation of it, it works all the way down to the last function where it doesnt contours is of the right kind.

cv = require("cv")
src = cv.imread(filename, 1 );

src_gray = cv.Mat()
cv.cvtColor(src, src_gray, cv.COLOR_BGR2GRAY)

canny_output = cv.Mat()
cv.Canny(src_gray, canny_output, threshold, threshold*2, 3 );

contours = cv.Mat(100, bit.lshift(4,16), cv.Point(0,0))
cv.findContours(canny_output, contours, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)


ERROR from findContours:

OpenCV Error: Assertion failed ((_contours.kind() == _InputArray::STD_VECTOR_VECTOR || _contours.kind() == _InputArray::STD_VECTOR_MAT || _contours.kind() == _InputArray::STD_VECTOR_UMAT)) in cv::findContours, file LuaOpenCV\third_party\opencv\modules\imgproc\src\contours.cpp, line 1892
LuaOpenCV\third_party\opencv\modules\imgproc\src\contours.cpp:1892: error: (-215) (_contours.kind() == _InputArray::STD_VECTOR_VECTOR || _contours.kind() == _InputArray::STD_VECTOR_MAT || _contours.kind() ==
_InputArray::STD_VECTOR_UMAT) in function cv::findContours

So basically i can't figure out the syntax to create the InputOutputArray of cv.Points that is expected for the contour to work.

C++:
using con = std::vector<std::vectorcv::Point>;
class Contour {
public:
Contour() {};
virtual ~Contour() {};
con _con;
};

function("FindContour", [](Mat src, Contour& contours) {
findContours(src, contours._con, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
});
function("DrawContour", [](Mat src, Contour& contours,Scalar color) {
drawContours(src, contours._con, -1, color);
});

from lua :
local src=cv.imread(file_name,cv.IMREAD_ANYCOLOR)
local dst_img=src:clone()
cv.cvtColor(src,src,cv.COLOR_BGR2GRAY)
local con=cv.Contour()
cv.FindContour(src,con)
cv.DrawContour(dst_img,con,cv.Scalar(0,0,255))
.......do something...