I have an image inside of an HTML5 canvas with the size of 28x28 pixels. I get the imageData of the canvas as an array of RGBA (red, green, blue, alpha) values using this code:
canvas = document.getElementById('canvas');
ctx = canvas.getContext("2d");
imgData = ctx.getImageData(0, 0, 28, 28);
Now I want to grayscale the image, so that I get an array of 784 values (28x28 pixels) where each pixel has one value (instead of four).
I've found a lot of different formulas for grayscaling, some are multiplying the rgb values, some are just calculating the average - I really don't know which of them to use...
I'm also stuck at getting 784 values - it's always 3136 (because of the 4 channels)...
Thanks in advance!
You can directly set a filter to the rendering context. It will be simpler than calculate every pixel color.
For this you should use
ctx.filter = 'grayscale(1)';