boundingbox

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2022 License: Unlicense Imports: 4 Imported by: 0

README

Bounding box

Library for finding the minimum bounding box of an image. It does so by simply iterating the image and checking which pixels surpass certain threshold, so they are considered as data points in the image which will delimit the bounding box.

Usage

package main

import (
	"image"
	_ "image/png"
	"os"
    "github.com/flesnuk/boundingbox"
)

func main() {
    infile, _ := os.Open("0004-001.png")
    defer infile.Close()
    img, _ := image.Decode(infile)
    bbox := boundingbox.Find(img, boundingbox.NewConfig(color.Gray{30}))
}

The NewConfig helper is useful for creating a configuration with the specified threshold color. By default the function used to compare the pixels is defined and exported as PixHasDataBlackBG. This does a simple comparison of RGB values, and if all are greater than the color threshold, it returns true.

There is a few options that can be customized:

  • PixHasData: set custom function to compare when a pixel is considered as data or not.
  • Bounds: by default uses the image bounds, but can be changed with this parameter.
  • Parallel: sets the number of goroutines to process the image in parallel. Set to -1 for auto setting (use the runtime.NumCPU())

Documentation

Index

Constants

View Source
const ParallelAuto = -1

Variables

This section is empty.

Functions

func Find

func Find(img image.Image, config Config) image.Rectangle

Find the bounding box of the img using the config provided.

func PixHasDataBlackBG

func PixHasDataBlackBG(pix color.RGBA64, threshold color.RGBA64) bool

PixHasDataBlackBG is a simple function that returns true if pix has greater RGB values than threshold.

func PixHasDataWhiteBG

func PixHasDataWhiteBG(pix color.RGBA64, threshold color.RGBA64) bool

PixHasDataWhiteBG is a simple function that returns true if pix has lesser RGB values than threshold.

Types

type Config

type Config struct {
	// When a pixel is considered as the edge of the image (has meaningful data).
	Threshold color.RGBA64
	// Check if pixel surpasses the Threshold. If nil it will use a basic pix > threshold function (gray-like comparison).
	PixHasData func(pix color.RGBA64, threshold color.RGBA64) bool
	// Region to check, if empty, image bounds are used instead.
	Bounds image.Rectangle
	// Number of gorutines used if n > 1. Use ParallelAuto (-1) for using NumCPU
	Parallel int
}

Config contains the parameters used.

func NewConfig

func NewConfig(threshold color.Color) Config

NewConfig returns a default config using the supplied threshold color. The PixHasData function is nil and use a basic pix > threshold function (gray-like comparison).

func NewConfigInverse

func NewConfigInverse(threshold color.Color) Config

NewConfigInverse returns a default config using the supplied threshold color. The PixHasData function use a basic pix < threshold function. Useful when background color is white.

func (*Config) WithBounds

func (config *Config) WithBounds(bounds image.Rectangle) *Config

WithBounds sets the bounds region.

func (*Config) WithParallel

func (config *Config) WithParallel(parallel int) *Config

WithParallel sets the parallel number of goroutines.

func (*Config) WithPixHasData

func (config *Config) WithPixHasData(pixHasData func(pix color.RGBA64, threshold color.RGBA64) bool) *Config

WithPixHasData sets the pixHasData function.

func (*Config) WithThreshold

func (config *Config) WithThreshold(threshold color.RGBA64) *Config

WithThreshold sets the threshold limit.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL