Shape-changing, or morphological, transformations (e.g., erosion, dilation, median filtering, and other ranked-order filtering) on binary imagery can be obtained by optically convolving the input image with a disk or other binary spread function and thresholding the output. Gray-scale images can be processed if the input is decomposed into a sequence of binary "slices" by a variable thresholding operation (threshold decomposition). The slices undergo shape-changing transformations and are then added together to produce the output gray-scale image. Median filtering to remove "salt-and-pepper" noise from a gray-scale image is demonstrated. The use of gray-scale (as opposed to binary) convolution kernels allows extension of the method to a more general class of nonlinear filtering operations that includes stack filters.