Skip to main content

Binarize image object (:binaryzation)

Declaration

img = img:binaryzation({
[white_background = white_background,]
[csim_mode = similarity_mode,]
[csim_algorithm = color_similarity_algorithm,]
{color*, tolerance*},
{color*, tolerance*},
...
})
img = img:binaryzation("cx*-cox*,cx*-cox*...")

Parameters

  • white_background 20250302
    Boolean, optional. Whether to set the background to white. Default false (white foreground on black background)

  • similarity_mode 20250302
    Boolean, optional. Whether to use similarity mode. In similarity mode, tolerance* means similarity instead of color difference. Default false

  • color_similarity_algorithm 20250302
    Integer, optional. Only takes effect in similarity mode. Default 0

    Color similarity algorithms
    0 XXT default algorithm, steep gradient, similarity drops quickly with difference
    1 Manhattan distance, smooth gradient
    2 Euclidean distance, smooth gradient
  • color*, tolerance*
    Integer. Whitelist of colors. color* is the color value; tolerance* is the max deviation for that color

  • cx*-cox*
    String. Whitelist of colors. cx* is the hex string of color value; cox* is the hex string of max deviation for cx*

Returns

  • img
    Image object. Returns the image object itself after binarization

Description

Binarize an image object
Mutates the image in-place
Performance: no data copy during operation

Examples

local pic = screen.image(462, 242, 569, 272)
pic = pic:binaryzation({
{0x9D5D39, 0x0F1F26},
{0xD3D3D2, 0x2C2C2D},
})
local pic = screen.image(462, 242, 569, 272)
pic = pic:binaryzation("9D5D39-0F1F26,D3D3D2-2C2C2D")
local pic = screen.image(462, 242, 569, 272)
pic = pic:binaryzation({
csim_mode = true, -- use similarity mode
csim_algorithm = 2, -- use Euclidean algorithm
white_background = true, -- set background to white
{0x9D5D39, 90}, -- similarity 90% for color 0x9D5D39
{0xD3D3D2, 90}, -- similarity 90% for color 0xD3D3D2
})