MathMath

Help › Functions

Images

imageChannels joinChannels toGray rgbToHsv hsvToRgb resizeImage rotateImage rotate90 flipRows flipCols convolve2d gaussianBlur boxBlur medianFilter sharpen sobelEdges threshold levels gammaAdjust equalize erode dilate ditherFloydSteinberg ditherAtkinson ditherJarvis ditherOrdered

imageChannels(M) MathMath’s own

Splits a color image, with its red, green and blue blocks side by side, into a vector of three matrices (R; G; B).

Mathcad's file readers and readImage give color as one matrix three times as wide as the picture: the red block, then the green, then the blue. A matrix like that looks the same as a wide gray image, so only imageChannels, toGray and rgbToHsv read it that way. Split it first with this function before blurring or sharpening, or the filter runs across the seams between the blocks.

Pick a channel out with an index: C₀, typed C[0, is red. joinChannels does the opposite.

Refused

  • A matrix whose number of columns isn't a multiple of 3.
  • Anything but a matrix of real numbers without units.
M≔(2000​100100​0200​)
C≔imageChannels(M)
C0​=(2000​)

joinChannels(R, G, B) MathMath’s own

Puts three matrices, the red, green and blue channels, together as one color image with the blocks side by side.

The answer has three times the columns of each channel, the form READRGB and readImage give and image regions show as color. Give a vector of three matrices, such as the answer of imageChannels, as the one argument instead.

Refused

  • A number of channels other than three.
  • Channels of different sizes.
  • Channels with units or complex numbers.
R≔(2000​)
G≔(100100​)
B≔(0200​)
joinChannels(R,G,B)=(2000​100100​0200​)

toGray(M) MathMath’s own

A color image as gray levels, weighting the channels 0.299 red, 0.587 green and 0.114 blue.

Takes a vector of three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. A matrix that is already gray comes back unchanged.

Refused

  • A matrix whose number of columns isn't a multiple of 3, as a color image.
  • Channels of different sizes.
M≔(100200​100200​100200​)
toGray(M)=(100200​)

rgbToHsv(M) MathMath’s own

A color image in hue, saturation and value, each scaled 0 to 255 like the channels.

The image is a vector of three matrices (R; G; B), or one matrix with the three blocks side by side; the answer is the same kind, with hue, saturation and value in place of red, green and blue. Hue 255 is a full turn round the color wheel, so red is 0, green is about 85 and blue about 170. hsvToRgb goes back.

Refused

  • A gray image: it needs three channels.
  • Channels of different sizes.
M≔(2550​0255​00​)
rgbToHsv(M)=(085​255255​255255​)

hsvToRgb(M) MathMath’s own

A color image given as hue, saturation and value, each 0 to 255, in red, green and blue.

The reverse of rgbToHsv, in the same two forms: a vector of three matrices, or one matrix with the blocks side by side. Hue 255 is a full turn, so 0 and 255 are both red.

Refused

  • A gray image: it needs three channels.
  • Channels of different sizes.
M≔(00​2550​255128​)
hsvToRgb(M)=(255128​0128​0128​)

resizeImage(M, rows, cols) MathMath’s own

The image scaled to the given number of rows and columns, by linear interpolation.

Enlarging blends neighboring pixels. Shrinking averages the pixels that fall into each new one, so none is skipped. A flat image stays flat. To pad or cut a matrix without scaling use resize. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • Rows or columns that aren't whole numbers of 1 or more.
  • Anything but a matrix of plain numbers, or three of them.
M≔(0100​100200​)
resizeImage(M,1,1)=(100​)
resizeImage(M,2,4)=(0100​25125​75175​100200​)

rotateImage(M, angle) MathMath’s own

The image turned by an angle about its center, the size kept, corners left empty as 0.

The angle is in radians, so 90·deg works as well as π/2. A positive angle turns counterclockwise as the image is shown. In between pixels the values are blended. For turns of exactly a quarter use rotate90. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • An angle that isn't a plain real number.
M≔⎝⎛​050100​50100150​100150200​⎠⎞​
rotateImage(M,2π​)=⎝⎛​100500​15010050​200150100​⎠⎞​

rotate90(M, [k]) MathMath’s own

The image turned by k quarter turns counterclockwise, one if k is left out; the size changes if the image isn't square.

A negative k turns clockwise, and 4 turns bring it back. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A number of turns that isn't a whole number.
M≔(14​25​36​)
rotate90(M)=⎝⎛​321​654​⎠⎞​
rotate90(M,2)=(63​52​41​)

flipRows(M) MathMath’s own

The image upside down: the order of its rows reversed.

flipCols mirrors it left to right. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

M≔(14​25​36​)
flipRows(M)=(41​52​63​)

flipCols(M) MathMath’s own

The image mirrored left to right: the order of its columns reversed.

flipRows turns it upside down. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

M≔(14​25​36​)
flipCols(M)=(36​25​14​)

convolve2d(M, K) MathMath’s own

The image convolved with a small kernel matrix K, the same size as the image.

K must have an odd number of rows and columns, such as 3×3, so it has a center pixel. It is flipped before use, as in a true convolution: a kernel with one 1 off center moves the image the opposite way. Beyond the edge the border pixels are taken to continue, so the size is kept. Nothing is scaled, so a kernel that doesn't sum to 1 changes the overall brightness; values can leave the range 0 to 255.

Use this to build your own filters; boxBlur, gaussianBlur, sharpen and sobelEdges are common ones ready made. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A kernel with an even number of rows or columns.
  • A kernel that isn't a plain matrix of numbers.
M≔⎝⎛​000​0900​000​⎠⎞​
K≔⎝⎛​010​101​010​⎠⎞​
convolve2d(M,K)=⎝⎛​0900​90090​0900​⎠⎞​

gaussianBlur(M, σ) MathMath’s own

The image blurred with a bell-shaped kernel of standard deviation σ pixels.

A larger σ blurs more; 0 changes nothing. The kernel reaches out to four σ and is normalized, so overall brightness is kept. Edges are treated as continuing the border pixels. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A negative σ.
M≔⎝⎛​000​000​200200200​200200200​⎠⎞​
gaussianBlur(M,1)=⎝⎛​11.71111.71111.711​60.10660.10660.106​139.894139.894139.894​188.289188.289188.289​⎠⎞​

boxBlur(M, n) MathMath’s own

The image blurred by averaging each pixel with its n×n neighborhood.

n is odd: 3, 5, 7 … so the window has a center. Edges are treated as continuing the border pixels, so the size is kept. gaussianBlur weights nearer pixels more. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • An n that isn't an odd whole number.
M≔⎝⎛​000​0900​000​⎠⎞​
boxBlur(M,3)=⎝⎛​101010​101010​101010​⎠⎞​

medianFilter(M, n) MathMath’s own

Each pixel replaced by the median of its n×n neighborhood, which removes specks without blurring edges.

n is odd. Edges are treated as continuing the border pixels. A single bright or dark pixel among its neighbors disappears. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • An n that isn't an odd whole number.
M≔⎝⎛​000​0900​000​⎠⎞​
medianFilter(M,3)=⎝⎛​000​000​000​⎠⎞​

sharpen(M, amount, [σ]) MathMath’s own

The image sharpened by adding amount times the difference between it and a blurred copy.

The blur is gaussianBlur with σ, 1 if left out. An amount of 0 changes nothing; 1 is a gentle sharpening. Results can go past 0 and 255: clip them with levels or threshold if you need to stay in range. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

M≔⎝⎛​000​000​200200200​200200200​⎠⎞​
sharpen(M,1)=⎝⎛​−11.711−11.711−11.711​−60.106−60.106−60.106​260.106260.106260.106​211.711211.711211.711​⎠⎞​

sobelEdges(M) MathMath’s own

The strength of the edges in the image: large where brightness changes quickly, 0 where it is flat.

Combines the Sobel horizontal and vertical gradients as √(gx²+gy²), so values can be beyond 255. Pair it with threshold to get a black and white outline. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

M≔⎝⎛​000​000​200200200​200200200​⎠⎞​
sobelEdges(M)=⎝⎛​000​800800800​800800800​000​⎠⎞​

threshold(M, level) MathMath’s own

Black and white: 255 where a pixel is at or above the level, 0 below it.

A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

M≔⎝⎛​050100​50100150​100150200​⎠⎞​
threshold(M,100)=⎝⎛​00255​0255255​255255255​⎠⎞​

levels(M, low, high) MathMath’s own

Stretches the range low to high over 0 to 255, clipping what falls outside.

Pixels at or below low become 0, at or above high become 255, and the ones between are spread evenly. Use it to raise the contrast of a dull image. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A high point that isn't above the low point.
M≔⎝⎛​4080120​80120160​120160200​⎠⎞​
levels(M,40,200)=⎝⎛​063.75127.5​63.75127.5191.25​127.5191.25255​⎠⎞​

gammaAdjust(M, γ) MathMath’s own

The brightness curve 255·(x/255)^γ: γ below 1 brightens the midtones and above 1 darkens them.

0 and 255 stay where they are. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A γ of 0 or less.
M≔(51153​102204​)
gammaAdjust(M,2)=(10.291.8​40.8163.2​)

equalize(M) MathMath’s own

Spreads the gray levels so they use the whole range 0 to 255 about evenly (histogram equalization).

Each level becomes the share of pixels at or below it, scaled to 255, so the darkest level present becomes 0 and the brightest 255. A flat image is left as it was. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

M≔(100110​100120​110120​)
equalize(M)=(0128​0255​128255​)

erode(M, n) MathMath’s own

Each pixel replaced by the smallest in its n×n neighborhood, which shrinks bright areas.

n is odd. Edges are treated as continuing the border pixels. dilate does the opposite. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • An n that isn't an odd whole number.
M≔⎝⎛​000​000​200200200​200200200​⎠⎞​
erode(M,3)=⎝⎛​000​000​000​200200200​⎠⎞​

dilate(M, n) MathMath’s own

Each pixel replaced by the largest in its n×n neighborhood, which grows bright areas.

n is odd. Edges are treated as continuing the border pixels. erode does the opposite. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • An n that isn't an odd whole number.
M≔⎝⎛​000​0900​000​⎠⎞​
dilate(M,3)=⎝⎛​909090​909090​909090​⎠⎞​

ditherFloydSteinberg(M, levels) MathMath’s own

The image reduced to a number of evenly spaced gray levels, spreading each rounding error to the pixels after it.

levels is 2 for black and white up to 256. The error of each pixel is passed on, in raster order, to its right and lower neighbors: 7/16 right, 3/16, 5/16 and 1/16 below. The pattern keeps the average brightness. See also ditherAtkinson and ditherJarvis. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A number of levels that isn't a whole number from 2 to 256.
M≔⎝⎛​128128128​128128128​128128128​⎠⎞​
ditherFloydSteinberg(M,2)=⎝⎛​2550255​02550​2550255​⎠⎞​

ditherAtkinson(M, levels) MathMath’s own

The image reduced to a number of gray levels, passing on only three quarters of each error to keep highlights and shadows clean.

levels is 2 to 256. One eighth of the error goes to each of six nearby pixels, so the rest is dropped: contrast goes up compared with ditherFloydSteinberg. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A number of levels that isn't a whole number from 2 to 256.
M≔⎝⎛​128128128​128128128​128128128​⎠⎞​
ditherAtkinson(M,2)=⎝⎛​25500​0255255​02550​⎠⎞​

ditherJarvis(M, levels) MathMath’s own

The image reduced to a number of gray levels, spreading each error widely over twelve nearby pixels.

levels is 2 to 256. The wide spread gives a smoother pattern than ditherFloydSteinberg, at more work. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A number of levels that isn't a whole number from 2 to 256.
M≔⎝⎛​128128128​128128128​128128128​⎠⎞​
ditherJarvis(M,2)=⎝⎛​2550255​02550​2550255​⎠⎞​

ditherOrdered(M, levels, [size]) MathMath’s own

The image reduced to a number of gray levels by comparing each pixel with a repeating Bayer pattern.

levels is 2 to 256. size is the pattern's side: 2, 4, 8 or 16, 4 if left out; a bigger pattern gives more shades in a flat area. Each pixel is decided on its own, so the same input always gives the same regular texture. A color image is three matrices (R; G; B), or one matrix with the red, green and blue blocks side by side. The filter is applied to each color on its own, and the answer is the same kind of image as the one given.

Refused

  • A number of levels that isn't a whole number from 2 to 256.
  • A size other than 2, 4, 8 or 16.
M≔(128128​128128​)
ditherOrdered(M,2,2)=(2550​0255​)