Skip to main content

cv - Convert image to contour shapes (:cv_to_shapes)

Declaration

shapes, visualized = img:cv_to_shapes([ options ])

Parameters

  • options
    Optional, table

    Option fields
    {
    should_visualize = false | true, -- optional, whether to output visualization, default false
    closed = false | true, -- optional, whether shapes are closed, default false
    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
    approx_epsilon = integer_value, -- optional, contour approximation percentage of perimeter, default 2
    }

Returns

  • shapes
    Table. List of shapes on the image

    Shapes 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,
    },
    ...
    },
    ...
    }
  • visualized
    Image object. When options.should_visualize is true, this is the visualization image

Description

Convert contours on the image into shapes (vertex lists)
Prefer simple, clear, closed shapes for better results

Example

local cv = require("image.cv")
local img = image.load_file(XXT_SCRIPTS_PATH..'/1.png')
local shapes, visimg = img:cv_to_shapes({
blur_size = 3,
approx_epsilon = 2,
closed = true,
should_visualize = true,
})
nLog(shapes)
dialog():add_image(visimg):show()