I am working with a red eye remover effect with html5 canvas. I used tracking.js to track the eyes. Check the following code.
Html
<!-- Include tracking js libraries -->
<script src="js/tracking-min.js"></script>
<script src="js/eye-min.js"></script>
<img id="sourceImg" src="red_eye1.jpg" width="202" height="187">
<canvas id="myCanvas" width="202" height="187" style="border:1px solid #d3d3d3;"></canvas>
Javascript
document.getElementById("sourceImg").onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("sourceImg");
ctx.drawImage(img, 0, 0);
// track eyes
var tracker = new tracking.ObjectTracker(['eye']);
tracker.setStepSize(1.7);
tracking.track('#sourceImg', tracker);
tracker.on('track', function(event) {
event.data.forEach(function(rect) {
// rect consists area of each eyes.
var imgData = ctx.getImageData(rect.x, rect.y, rect.width, rect.height);
// remove red eye
var i;
for (i = 0; i < imgData.data.length; i += 4) {
// find and check red intensity
var redIntensity = (imgData.data[i] / ((imgData.data[i+1] + imgData.data[i+2]) / 2));
if (redIntensity > 1.5) // 1.5 because it gives the best results
{
// reduce red to the average of blue and green
imgData.data[i] = (imgData.data[i+1] + imgData.data[i+2]) / 2;
}
}
ctx.putImageData(imgData, rect.x, rect.y);
});
});
};
I got the following result.
Here the red eye removed successfully. But it affected the color around the eye area. Because the tracking.js returned the eye area including the surrounding area. How to track the eye balls, which will solve this issue.