alanjiach@gmail.com

← all writing

Images, filters, and visual representations

study notescomputer vision

Part of the generative visual computing notes.

Images and filtering

An image can be treated as a function on a spatial grid. A grayscale image assigns one intensity to each location. A color image assigns a vector of channel values. A filter combines nearby values to produce a new image.

For a kernel hh, discrete convolution is

(f∗h)[i,j]=∑u,vh[u,v]f[i−u,j−v].(f*h)[i,j]=\sum_{u,v}h[u,v]f[i-u,j-v].

Correlation uses the kernel without reversing its offsets. The distinction matters for asymmetric kernels. For a symmetric Gaussian kernel, the two operations agree.

An averaging filter reduces independent noise by combining neighboring observations, but it also mixes values across boundaries. A Gaussian filter weights the center more heavily. Its standard deviation controls the spatial extent of smoothing. Normalizing the kernel to sum to one preserves constant images away from boundary effects.

A median filter is different. It chooses the middle value in a neighborhood rather than taking a weighted sum. This makes it useful for isolated extreme values such as salt-and-pepper noise. It is not a linear filter.

Edges

The image gradient records local intensity changes:

∇I=(Ix,Iy),∥∇I∥=Ix2+Iy2.\nabla I=(I_x,I_y),\qquad \|\nabla I\|=\sqrt{I_x^2+I_y^2}.

Differentiation can amplify noise, so edge detection usually begins with smoothing. Canny edge detection then computes gradients, suppresses responses that are not local maxima along the gradient direction, and uses two thresholds to connect weak responses to strong edges.

The direction used for non-maximum suppression is perpendicular to the local edge. Comparing along the edge would preserve a thick band rather than isolate its center.

Template matching compares a known pattern with image regions. Raw intensity differences can be sensitive to lighting. Normalized comparisons reduce some of that sensitivity, but they do not automatically provide invariance to rotation, scale, or deformation.

Binary structure and texture

Thresholding converts a scalar image into a binary decision. Dilation expands foreground regions according to a structuring element, while erosion contracts them. The choice of structuring element determines which spatial patterns are preserved or removed.

Texture describes repeated local structure. One representation is a bank of filter responses followed by local summary statistics. The representation need not preserve the exact location of every repeated element to distinguish two textures.

This leads to a simple synthesis idea. Given the pixels already generated around a location, search an example texture for compatible neighborhoods and sample a continuation. Patch-based methods reuse larger regions. Overlap constraints and seam selection reduce visible discontinuities between patches. Larger patches preserve more structure but provide fewer independent choices.

Learned representations

A neural network replaces fixed features with parameters fitted to a task. Backpropagation applies the chain rule through the computation. A convolutional layer shares parameters across locations, which gives it a useful spatial bias and reduces the number of independent weights.

For input width nn, kernel width kk, padding pp, and stride ss, the output width is

⌊n+2p−ks⌋+1.\left\lfloor\frac{n+2p-k}{s}\right\rfloor+1.

For example, an eight-pixel input with a three-pixel kernel, stride one, and no padding produces six output positions.

Recurrent models summarize a sequence with a hidden state. LSTM and GRU gates control how information is retained and updated. Attention provides a different mechanism: each query forms a weighted combination of value vectors using its similarity to the keys.

Attention⁡(Q,K,V)=softmax⁡(QKTdk)V.\operatorname{Attention}(Q,K,V) =\operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V.

The softmax is applied over the available keys for each query. Positional information is needed when order matters. For autoregressive prediction, a causal mask prevents access to later positions.

Worked example: smoothing and attention

Take the one-dimensional signal [0,0,8,0,0][0,0,8,0,0] and the smoothing kernel [1,2,1]/4[1,2,1]/4. Away from the boundary, the output around the impulse is [2,4,2][2,4,2]. The total stays at eight, but the peak falls from eight to four. This is why smoothing reduces isolated noise and also softens narrow edges. A median filter of width three would remove this isolated impulse entirely.

For a small attention example, suppose the scaled attention logits are [0,log⁡3][0,\log 3]. Softmax gives weights [1/4,3/4][1/4,3/4]. With scalar values [2,6][2,6], the output is 2/4+18/4=52/4+18/4=5. Attention combines the values using weights computed from the query and keys. The logits are not themselves the output.

Further reading

Attention Is All You Need introduces scaled dot-product attention. Read the attention definition alongside the numerical example above, then consider why dividing by dk\sqrt{d_k} changes the concentration of the weights.