Wiki · Read the guide: Image processing: convolution, Bayer, histogram

Image Processing Lab

Three image-processing tools in one: apply convolution kernels, explore Bayer sensor demosaicing and analyse histogram and exposure — on your own image or a sample. A demonstration tool, born from our work on CMOS sensors and embedded camera pipelines.

Image processing lab tool

1 · Image

3×3 kernel
Original
Result

Three operations, one pipeline

The three modules of this tool are stages of the pipeline that turns light into a usable image. A CMOS sensor captures a raw Bayer mosaic (Demosaic module); demosaicing converts it to RGB; then convolution filters reduce noise or enhance detail (Convolution module); finally you check the histogram and exposure to see whether the scene was captured correctly (Histogram module). These are the same steps that govern the quality of an embedded camera.

What is 2D convolution?

Convolution slides a small matrix of weights — the kernel — over each pixel: a pixel’s new value is the weighted sum of itself and its neighbours. It is the basic operation of nearly every spatial filter and of the first layers of a convolutional network. With an odd-sized kernel K (3×3, 5×5…) and an image I, the output pixel at (x, y) is:

O(x,y)=∑j=−aa∑i=−aaK(i,j) ⋅ I(x+i,  y+j)O(x,y)=\sum_{j=-a}^{a}\sum_{i=-a}^{a} K(i,j)\,\cdot\,I(x+i,\;y+j)

where a = (size−1)/2. The R, G and B channels are filtered separately; the alpha channel is left unchanged.

What are the main kernels?

  • Identity — centre 1, everything else 0: the image is unchanged. The starting point for building a custom kernel.
  • Box blur / Gaussian blur — all-positive weights that average the neighbourhood: they reduce noise and detail. The Gaussian weights the centre more (1–2–1 / 1–4–6–4–1) and blurs more naturally than the box.
  • Sharpen — high centre, negative edges: amplifies local contrast, enhancing detail. It is an “unsharp” in kernel form.
  • Edge detect (Laplacian) — zero-sum, responds to the second derivative: highlights contours in every direction.
  • Sobel X / Sobel Y — directional gradient (first derivative): detects vertical or horizontal edges. The root of the sum of their squares gives the gradient magnitude, the basis of edge detection.
  • Emboss — an asymmetric kernel that simulates a side-lit relief, turning edges into an embossed effect.

Borders and normalization

Near the image boundaries, part of the kernel falls outside the image: the coordinates are clamped, i.e. the edge pixel is extended (mirror or periodic modes also exist). Normalization divides the result by the sum of the coefficients, so a blur preserves the average luminance. Zero-sum kernels (edge, Sobel) are not normalized: to make them visible an offset is added (128 here), mapping zero to mid grey.

What is the Bayer sensor?

A colour CMOS sensor does not measure RGB at every pixel. In front of the photosites sits a colour-filter array (CFA) laid out in the Bayer pattern: a repeating 2×2 grid with two greens, one red and one blue. There are twice as many green-filtered photosites as red- or blue-filtered ones, because the human eye is more sensitive to green, which carries most of the luminance information. RGGB, BGGR, GRBG and GBRG are the four possible phases of the same scheme: only the starting pixel of the grid changes.

Demosaicing: nearest-neighbor and bilinear interpolation

Since each pixel records a single channel, the two missing ones must be reconstructed by interpolating same-colour neighbours: that is demosaicing. Nearest neighbor copies the closest sample: the fastest, but it leaves aliasing and visible 2×2 blocks. Bilinear averages same-colour neighbours — smoother, but on sharp edges it can produce two typical artefacts: zippering (a zig-zag along contours) and colour fringing. The algorithms cameras use are more sophisticated: instead of averaging indiscriminately, they detect the direction of contours and interpolate along edges rather than across them, reducing these artefacts.

Histogram and luma

The histogram counts how many pixels fall in each level (0–255), separately for the three channels and for luma. Luma Y′ combines the channels with the Rec.601 weights on the values encoded in the file, that is gamma-encoded: it is not photometric luminance, which requires linearizing the channels first. On pure red the difference is 51 levels out of 255.

Y=0.299 R+0.587 G+0.114 BY = 0.299\,R + 0.587\,G + 0.114\,B

The histogram describes the level distribution: more pixels on the left correspond to darker values, on the right to brighter values. Distribution width alone does not establish good contrast or correct exposure.

Exposure and clipping

Pixels at 0 or 255 can indicate saturation, but the histogram alone does not establish lost detail. The indicators report the proportion of pixels at the extremes: also inspect the image and acquisition process to interpret them.

From the field to the code

In our work on CMOS sensors — from register-level sensor bring-up to video-stream analysis — reading a histogram, judging the demosaicing and applying a sharpening or noise-reduction filter are everyday tasks. The related repository contains the software used to bring up the sensor and inspect its image stream.

What are the tool’s limitations?

  • Demonstration tool: the algorithms (bilinear demosaicing, basic kernels) are didactic, not those of a real ISP.
  • The image is resized to keep processing responsive; very large images lose detail.
  • It always works on already-demosaiced, compressed data (JPEG/PNG): the Bayer mosaic is simulated from RGB, not a sensor RAW.
  • Convolution runs at 8-bit integer precision with clamping; it does not replace full-precision floating-point processing.