Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Commit

Permalink
Added API doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kyamagu committed Jun 25, 2015
1 parent 0429d1c commit f792430
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The class inherits from the `SegmentAnnotator` class.
new PreSegmentAnnotator(imageURL, options)

A class object to generate an annotation canvas from the given image URL. It
internally calls `PreSegmentation` to retrieve a segmentation mask from a PNG
internally calls `PreSegmentation` to retrieve a segmentation mask from a PNG
file.

* `imageURL` - URL of an image to annotate. (Caution: do not use a large
Expand Down Expand Up @@ -255,10 +255,11 @@ based on the VLFeat implementation. The function takes the following options.

PreSegmentation(imageURL, options)

Javascript implementation of an image segmentation mask loader.
Existing mask loader.

The function takes the following options.

* `annotation` - Existing annotation URL (required).
* `toDataURL` - Callback function to receive the result as a data URL.
* `callback` - Function to be called on finish. The function takes a single
argument of result object that contains following fields.
Expand Down
68 changes: 34 additions & 34 deletions pre-segmentation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* Javascript import of a pre-segmentation algorithm from PNG annotated images.
* Pre-segmentation from PNG annotated images.
*
* API
* ---
*
* SLICSegmentation(imageURL, options)
* PreSegmentation(imageURL, options)
*
* The function takes the following options.
* * `annotation` - URL of the annotation PNG data.
* * `toDataURL` - Callback function to receive the result as a data URL.
* * `callback` - Function to be called on finish. The function takes a single
* argument of result object that contains following fields.
Expand All @@ -22,23 +23,20 @@
* Jonathan Passerat-Palmbach 2015.
*/
(function() {

// Compute Pre-Segmentation.
function computePreSegmentation(imageData, options) {

var d = imageData.data;
var numPixels = imageData.width * imageData.height;
var segmentation = new Int32Array(numPixels);

// can skip with a stride of 4 since labels' RGB components are all the same
// can skip with a stride of 4 since labels' RGB components are all the
// same.
for (var i = 0; i < numPixels; i++) {
// TODO would be great to rewrite this ugly loop
// TODO would be great to rewrite this ugly loop.
segmentation[i] = d[i*4];
}

return segmentation;
}

// Remap label indices.
function remapLabels(segmentation) {
var map = {},
Expand All @@ -51,16 +49,14 @@
}
return index;
}

// retrieve segmentation from label map
function retrieveSegmentation(annotationsImageData, imageData, options) {

// Retrieve segmentation from label map.
function retrieveSegmentation(annotationsImageData, imageData, options) {
var segmentation = computePreSegmentation(annotationsImageData, options);

// numSegments is not necessarilly the biggest label as there might be a gap in labels presence in the image ex: [0, 1, 3, 4]
// still fine as the export preserves the original labels \o/
// numSegments is not necessarilly the biggest label as there might be a
// gap in labels presence in the image ex: [0, 1, 3, 4] still fine as the
// export preserves the original labels \o/
var numSegments = remapLabels(segmentation);

if (options.callback) {
var rgbData = new Uint8Array(imageData.data);
options.callback({
Expand All @@ -71,10 +67,8 @@
rgbData: rgbData
});
}

if (options.toDataURL) {
var dataurl = getDataURL(imageData.width, imageData.height, indexMap, options);
}
if (options.toDataURL)
getDataURL(imageData.width, imageData.height, indexMap, options);
}

// Convert to Data URL.
Expand All @@ -101,17 +95,16 @@
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');

// draw actual image
// draw actual image.
context.drawImage(image, 0, 0);
var imageData = context.getImageData(0, 0, image.width, image.height);

var annotations = new Image();
annotations.src = options.annotation;
annotations.crossOrigin = null;
annotations.onerror = function() { onErrorImageLoad(annotations); };
annotations.onload = function() { onSuccessAnnotationLoad(annotations, imageData, options); };

annotations.onload = function() {
onSuccessAnnotationLoad(annotations, imageData, options);
};
}

// When annotations are loaded.
Expand All @@ -120,15 +113,20 @@
canvas.width = annotations.width;
canvas.height = annotations.height;
var context = canvas.getContext('2d');
// draw actual image

// Draw actual image.
context.drawImage(annotations, 0, 0);
var annotationsImageData = context.getImageData(0, 0, annotations.width, annotations.height);

// fill in `segmentation` with retrieved annotations
segmentation = retrieveSegmentation(annotationsImageData, imageData, options);
var annotationsImageData = context.getImageData(0,
0,
annotations.width,
annotations.height);

// Fill in `segmentation` with retrieved annotations.
segmentation = retrieveSegmentation(annotationsImageData,
imageData,
options);
}

// When image is invalid.
function onErrorImageLoad(image) {
alert('Failed to load an image: ' + image.src);
Expand All @@ -137,12 +135,14 @@
// Public API.
window.PreSegmentation = function(imageURL, options) {
if (typeof options === 'undefined') options = {};

if (options.annotation)
throw "Annotation URL missing";

var image = new Image();
image.src = imageURL;
image.crossOrigin = null;
image.onerror = function() { onErrorImageLoad(image); };
image.onload = function() { onSuccessImageLoad(image, options); };

};
}).call(this);

0 comments on commit f792430

Please sign in to comment.