GithubHelp home page GithubHelp logo

lamtv36 / node-subimage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from slayerdf/node-subimage

0.0 0.0 0.0 3.23 MB

Fuzzy search for subimage within image

JavaScript 77.51% C++ 22.20% Python 0.28%

node-subimage's Introduction

node-subimage

Fuzzy search for subimage within image. Tolerates color drift and bad pixels.

Installation

npm install subimage

or

yarn add subimage

How to use

const fs = require('fs'),
      subimage = require('subimage')

let image = fs.createReadStream('image.png'),
    template = fs.createReadStream('template.png')

image = await subimage.util.streamParse(image)
template = await subimage.util.streamParse(template)

let results = await subimage.search(image, template)

API

search(image, template, [options])

util.streamParse(fileStream)

util.pngjsParse(pngjs)

image and template

Image object should have the following example structure:

{
  width: 10,
  height: 10,
  channels: 3,
  data: <Buffer ff ff ff ...>
}

where

  • width Number - image width
  • heightNumber - image height
  • channels Number - optional, number of color channels in an image, possible values: 1-4
  • data Buffer - image pixel data

Property channels is optional and is only used for data length validation.

Property data should be of type buffer with pixel data arranged from top-leftmost to bottom-rightmost pixel. Possible channel orders are listed bellow:

  • K (grayscale)
  • KA (grayscale + alpha)
  • RGB
  • RGBA

For example, let's say we have a 2x2 pixel image with red background and blue pixel on the bottom left corner. So the data buffer would look like this:

<Buffer ff 00 00 ff 00 00 00 00 ff ff 00 00>

options

Two options are supported:

  • colorTolerance Number - the maximum range in color difference between two matched pixels to constitute a match.
  • pixelTolerance Number - the number of not matching (bad) pixels to ignore and treat subimage as still matching.

Options colorTolerance and pixelTolerance can be used together.

Option colorTolerance is combined for all color channels. For example, if colorTolerance == 10, then the difference for R channel can be 6, G - 4, and B should match exactly, for the pixel color to be treated as matching.

result

The search function returns an array of result objects. If there were no matches of the subimage within the template, the result array will be empty. The result object has 3 properties: x, y, and accuracy. The later doesn't bear any strict meaning and is only used for ordinal comparison. The smaller the accuracy value, the more accurate the match between the template and the subimage is.

Example:

{ x: 2, y: 2, accuracy: 0 }

Under the hood

Image pixel comparison requires a lot of steps of algebraic computation which spawns large loops of few small number operations for each step. JavaScript doesn't have native SIMD support, although there are signs of promising initiatives and the situation can change eventually. As of today, there's no other way to speed things up as to use native bindings to some algebra library that supports vectorization. Since the image data can be expressed as a matrix, Eigen C++ template library is used in this project.

License

ISC

node-subimage's People

Contributors

monai avatar jballanger avatar slayerdf avatar joncys avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.