Auto-focus capabilities based on saliency maps
npm install salient-autofocusAuto-focus capabilities based on saliency maps.
Extract a 640x480 region from a 1024x768 image using salient auto-focus
``
const autoFocus = require('salient-autofocus');
const salientMatrix = [[0, 0.25, 0.5], [0, 0, 0.25], [0, 0.25, 0.75]]; // custom 3x3 matrix
const region = autoFocus.getRegionFromSalientMatrix(salientMatrix, { imageWidth: 1024, imageHeight: 768, regionWidth: 640, regionHeight: 480 });
// { left, top, right, bottom, width, height }
`
Extract a 640x480 region from a 1024x768 image using salient auto-focus
`
const autoFocus = require('salient-autofocus');
const salientMatrix = mySalientMap.getDataAsArray(); // works with opencv or opencv4nodejs packages
const region = autoFocus.getRegionFromSalientMatrix(salientMatrix, { imageWidth: 1024, imageHeight: 768, regionWidth: 640, regionHeight: 480 });
// { left, top, right, bottom, width, height }
`
The real power of this package is the metadata that it extracts from saliency data, which can be stored or cached, allowing subsequent auto-focus requests to be lightning fast! No need to re-create salient maps, store salient maps, or re-process salient maps.
`
const autoFocus = require('salient-autofocus');
const salientMatrix = mySalientMap.getDataAsArray(); // works with opencv or opencv4nodejs packages
const salientMeta = autoFocus.getMetaFromSalientMatrix(salientMatrix);
// TODO: store meta for subsequent auto-focus requests
const region = autoFocus.getRegionFromMeta(salientMeta, { imageWidth: 1024, imageHeight: 768, regionWidth: 640, regionHeight: 480 }); // SUPA FAST!
// { left, top, right, bottom, width, height }
`
That's out of the scope of this project as there are many models for generating salient maps based on TONS of research. However you're in luck,
because I just happen to author one of those solutions: https://github.com/asilvas/salient-maps
The following examples are visual representations of the original image side by side with the salient map,
overlayed by the regions of saliency density to illustrate where salient-autofocus will focus on
depending on the available size of the region requested. The more accurate the saliency map, the better
salient-autofocus will perform.






Data returned by getRegionFromSalientMatrix or getRegionFromMeta to indicate the region
to focus on.
``
{ left, top, right, bottom, width, height }
| Property | Type | Info |
| --- | --- | --- |
| left | number | Left-most edge of region on a 0 to {imageWidth} scale in columns (typically pixels) |number
| top | | Top-most edge of region on a 0 to {imageHeight} scale in rows (typically pixels) |number
| width | | Width of region on a 0 to {imageWidth} scale in columns (typically pixels) |number
| height | | Height of region on a 0 to {imageHeight} scale in rows (typically pixels) |number
| right | | Right-most edge of region added for convenience (left+width) |number
| bottom | | Bottom-most edge of region added for convenience (top+height) |
If extracting meta from salient data via getMetaFromSalientMatrix (as you should for production usage),
below is the structure of that data for reference.
``
{
v,
c: {
x,
y
},
r25th,
r40th,
r50th,
r75th,
r90th
}
| Property | Type | Info |
| --- | --- | --- |
| v | number | Version of the data structure. If this version ever changes, you should consider the old meta invalid |object
| c | | Center point based on average saliency. Typically ignored unless no regions are identified, but still can be useful |number
| c.x | | Center x dimension from 0 (left) to 1 (right) |number
| c.y | | Center y dimension from 0 (top) to 1 (bottom) |MetaRegion
| r25th | | MetaRegion that encompasses ~25% of all saliency |MetaRegion
| r40th | | MetaRegion that encompasses ~40% of all saliency |MetaRegion
| r50th | | MetaRegion that encompasses ~50% of all saliency |MetaRegion
| r75th | | MetaRegion that encompasses ~75% of all saliency |MetaRegion
| r90th | | MetaRegion that encompasses ~90% of all saliency |
The meta region structure used for caching, not to be confused by the Region returned by getRegionFromSalientMatrixgetRegionFromMeta
or .
``
{ l, t, w, h }
| Property | Type | Info |
| --- | --- | --- |
| l | number | Left-most edge of region on a 0 to 1 scale |number
| t | | Top-most edge of region on a 0 to 1 scale |number
| w | | Width of region on a 0 to 1 scale |number` | Height of region on a 0 to 1 scale |
| h |