Image Processing 3. Negate filter, channel extraction and inversion
2 min read June 10, 2025 #image processing #javascriptIn this article we'll split images into color components, create a negate filter, and invert individual channels. Finally, we'll manipulate RGB components!
Previous Homework Assignment#
Let's revisit the homework assignment from the previous article. The task was to implement simultaneous horizontal and vertical flipping. Here's the solution:
for ; i < dst.length; i++
Yes, it's simply reversing the array, but in the context of images, it flips pixels along both axes. See this visual example: CodePen. Image processing 2. Solution
Channel Extraction#
We'll split a full-color image into four separate images. The first will contain only red components, the second only green, and the third only blue:
// Red
for ; i < dst.length; i++
// Green
for ; i < dst.length; i++
// Blue
for ; i < dst.length; i++
Transparency, which is stored in the high-order 8 bits, is forcibly set to 255 (0xFF) here. It could also be written as:
dst = 255 << 24 | b << 16;
The fourth image will contain only the alpha channel with black colors:
for ; i < dst.length; i++
For demonstration, we'll use a photo kindly provided by vl@volk:
Since photo is predominantly green, the green channel appears brighter and more detailed. Bright highlights are visible across all channels because they close to shades of gray, and gray combines all three colors in equal proportions.
Another example (original photo):
The first noticeable thing is the red lighting on the Ferris wheel. It's clearly visible in the red channel and almost invisible in the green and the blue channels. Only where red transitions to lighter tones do green and blue appear.
The yellow reflection on the road is clearly seen in red and green channels since mixing these colors produces yellow.
Homework Assignment#
Modify the output to display red, green, and blue channels in grayscale.
Negate filter and channel inversion#
To create the negative filter, we need to invert the R, G, and B channels. Black (0, 0, 0) should become white (255, 255, 255) and vice versa.
for ; i < dst.length; i++
We'll also implement single-channel inversion using checkboxes:
;
;
;
;
for ; i < dst.length; i++