cv - Find contours in image (:cv_find_shapes)
Declaration
found_shapes, visualized = img:cv_find_shapes(shapes[, options])
Parameters
-
shapes
Table. One or more shapes' vertex info to search forShapes parameter structure
{
{ -- shape 1
{ -- vertex 1
["y"] = number_value,
["x"] = number_value,
},
{ -- vertex 2
["y"] = number_value,
["x"] = number_value,
},
...
},
{ -- shape 2
{ -- vertex 1
["y"] = number_value,
["x"] = number_value,
},
{ -- vertex 2
["y"] = number_value,
["x"] = number_value,
},
...
},
...
} -
options
Table, optional. Contour matching optionsOption fields
{
should_visualize = false | true, -- optional, whether to output visualization, default false
closed = false | true, -- optional, whether shapes are closed, default false
min_area = number_value, -- optional, minimum area, default -1 (no limit)
max_area = number_value, -- optional, maximum area, default -1 (no limit)
blur_size = integer_value, -- optional, blur kernel size, positive odd number only, default 3
canny_threshold1 = 100, -- optional, Canny lower threshold, default 100
canny_threshold2 = 200, -- optional, Canny upper threshold, default 200
diff_threshold = number_value, -- optional, difference threshold, smaller means more similar, default 0.001
approx_epsilon = integer_value, -- optional, contour approximation percentage of perimeter, default 2
}
Returns
-
found_shapes
TableFound shapes structure
{
{ -- list of shapes matching input shape 1
{
diff_score = number_value, -- smaller is more similar
{ -- vertex 1
["y"] = number_value,
["x"] = number_value,
},
{ -- vertex 2
["y"] = number_value,
["x"] = number_value,
},
...
},
{ -- second match for input shape 1
diff_score = number_value,
{ -- vertex 1
["y"] = number_value,
["x"] = number_value,
},
{ -- vertex 2
["y"] = number_value,
["x"] = number_value,
},
...
},
...
},
{ -- list of shapes matching input shape 2
{ -- first match
diff_score = number_value,
{ -- vertex 1
["y"] = number_value,
["x"] = number_value,
},
{ -- vertex 2
["y"] = number_value,
["x"] = number_value,
},
...
},
{ -- second match
diff_score = number_value,
{ -- vertex 1
["y"] = number_value,
["x"] = number_value,
},
{ -- vertex 2
["y"] = number_value,
["x"] = number_value,
},
...
},
...
},
...
} -
visualized
Image object. When options.should_visualize is true, this is the visualization image
Description
Find one or more shapes from an image
Standard regular shapes can be generated by functions
Example
function make_triangle(side_len)
local hf = math.sqrt(3)/2
local h = math.floor(hf * side_len)
return {
{ ["x"] = 1, ["y"] = h },
{ ["x"] = side_len, ["y"] = h },
{ ["x"] = side_len // 2, ["y"] = 1 },
}
end
function make_rectangle(side_len)
return {
{ ["x"] = 1, ["y"] = 1 },
{ ["x"] = 1, ["y"] = side_len },
{ ["x"] = side_len, ["y"] = side_len },
{ ["x"] = side_len, ["y"] = 1 },
}
end
local info, visimg = screen.image():cv_find_shapes({make_triangle(45), make_rectangle(40)}, {
min_area = 2000,
max_area = 20000,
diff_threshold = 0.005,
approx_epsilon = 1,
shape_match_mode = 3,
closed = true,
should_visualize = true,
})
nLog(info[1])
nLog(info[2])
dialog():add_image(visimg):show()