How it works
This page explains the calculations K-Space Paint performs behind the scenes, using the same definitions as the actual program.
K-Space Paint is a teaching tool for learning how the Fourier transform relates images to k-space. It does not reproduce how an MRI scanner acquires signals or reconstructs images, and it must not be used for medical decisions (see “Differences from real MRI” below).
1. Grid and coordinates
Both image space and k-space are grids ().
- Image pixels: . The origin is the top-left corner of the image, and increases downward.
- k-space frequencies: . The center of the k-space on screen is (the DC component), and frequencies get higher toward the edges. Positive is to the right and positive is downward.
2. Fourier transform
Drawing an image (forward transform)
The app computes the 2D discrete Fourier transform of the image you draw, (white = 1, black = 0).
Drawing in k-space (inverse transform)
The app computes the 2D inverse discrete Fourier transform of the k-space you draw, (complex-valued).
The normalization factor is omitted. Because the display divides by the maximum value, the result looks the same on screen with or without it. The calculation uses a fast Fourier transform (radix-2 FFT), but the results match the definitions above (see “8. Verification”).
Because both transforms are discrete, the image wraps around periodically: the top and bottom edges, and the left and right edges, are treated as neighbors.
3. Display processing
Raw results are hard to see, so the following processing is applied before converting them to brightness values from 0 to 1. What you see is a processed version of the result, so brightness is not proportional to the magnitude of the values.
| What is shown | Displayed value |
|---|---|
| Reconstructed image |
The absolute value (magnitude) is divided by its maximum and brightened slightly (gamma 0.85). |
| k-space (result of the Fourier transform, or k-space taken from an image) |
The values are compressed logarithmically so that the center (DC component) does not drown out everything else (the views labeled “log scale”). Phase is not displayed. |
| k-space you drew yourself | is shown as is (0 to 1). |
| Tiny points of 1–4 cells in k-space | A single cell of the 256×256 grid is only a few pixels on screen and easy to miss. So for any group of nonzero cells connected horizontally, vertically or diagonally that has 4 cells or fewer and no other points or lines within 2 cells, each cell is drawn as a 5×5 block in the display only (e.g. the “Center point”, “Symmetric pair” and “Random” presets). The data used in the calculation is unchanged, and the reconstructed image is not affected. |
Example: why the stripes look finer with “Symmetric pair”
The “Symmetric pair” preset puts the value 1 at and . This gives . Since the screen shows the absolute value , the negative parts are also bright, so you see twice as many bright stripes as there are cosine periods.
4. Brush
Each brush dab adds a smooth Gaussian-shaped value centered on your finger (or mouse). The amount added to a cell at offset from the center is:
- : brush radius. From the “Size” slider value (0.6 to 4), in k-space and in image space (in cells)
- : the “Strength” slider value (0.2 to 1) for the pen, and for the eraser
| Where and which tool | How the value changes |
|---|---|
| Image space, pen / eraser | |
| k-space, pen | is added to the real part. If the magnitude exceeds 1, it is clamped to 1 while keeping the phase. |
| k-space, eraser | Only the magnitude is reduced; the phase is kept. |
Symmetry mode (Hermitian symmetry)
When “Symmetry” is on, whatever you draw at is also drawn at the point mirrored through the center, . k-space drawn in symmetry mode (and k-space taken from an image) satisfies
(the complex conjugate), so the inverse transform is real-valued. This gives a natural look, closer to an image of a real object. Note that the column and the row (the left and top edges of the screen) pair with themselves, because their counterparts would lie outside the grid.
5. Loading images
- Head phantom (preset): the Modified Shepp-Logan phantom, widely used to test MRI and CT reconstruction (10 overlapping ellipses, values from 0 to 1), is created in image space and brought into k-space the same way as “Edit in k-space” below.
- Insert image: the image is scaled so that its shorter side fits, and the center is cropped to a 256×256 square. It is converted to grayscale with .
- Edit in k-space (the same happens when you insert an image): the image is forward-transformed, everything is divided so that the largest magnitude becomes 1, and both magnitude and phase are brought into k-space. If you change nothing, the inverse transform gives back the original image.
6. Undersampling (parallel imaging and half Fourier)
With “Undersampling” in k-space mode, you can reconstruct as if some lines of k-space had not been acquired. The phase-encoding direction is vertical on screen (), and each horizontal row of k-space (all cells with the same ) is treated as one line obtained in one acquisition. Lines that are not acquired are set to 0 before the inverse transform in section 2. The k-space you drew is not changed; the undersampling is applied only for display and reconstruction, so choosing “None” brings everything back.
| Undersampling | Lines acquired |
|---|---|
| 1/2, 1/3, 1/4 | Lines where is a multiple of (128 lines for , 85 for , 64 for ) |
| Half | The 160 lines (5/8) with : all of the lower half plus 32 lines above the center |
1/2, 1/3 and 1/4: aliasing (fold-over)
When only 1 of every lines is acquired, the inverse transform gives the original image overlapped with copies of itself shifted vertically by (because the image wraps around periodically, parts that stick out reappear on the opposite side).
For , is not an integer, so the shifted copies fall between grid points and appear interpolated, slightly smeared. In real parallel imaging, several receiver coils with different sensitivities acquire simultaneously, and those differences are used to unfold the overlapping copies (SENSE, GRAPPA, etc.). When “Unfold with SENSE” is off, K-Space Paint assumes a single receiver coil and shows the folded image before unfolding. The calculation when it is on is explained in section 7.
Half Fourier: fill with 0 / fill by symmetry
- Fill with 0 (“Fill by symmetry” off): the missing lines are left at 0 for the inverse transform. One side of the high frequencies is missing, so the image blurs vertically and stripes appear around edges.
- Fill by symmetry: the missing lines are filled with the complex conjugate of the values at the point-symmetric position. If the image is real-valued (zero phase), this is exactly the Hermitian symmetry of section 4, so the original k-space is restored. The top line (), however, pairs with itself and was not acquired, so it stays 0.
Photos and the head phantom are real-valued images, so filling by symmetry restores them almost perfectly. In real MRI images, the phase varies from pixel to pixel (due to field inhomogeneity and other causes), so this filling alone does not restore the image. Real scanners estimate the phase from the symmetrically acquired lines near the center and correct for it (e.g. the homodyne method).
Display: lines that are not acquired are black, and lines filled by symmetry are shown at half brightness.
7. SENSE (receiver coil model)
Turning on “Unfold with SENSE” in the “Acquisition” tab simulates reception with multiple receiver coils and unfolds the aliasing with SENSE. Real results depend on the number, shape and arrangement of the coils, so a fixed model case is used here. It is available only with “None”, “1/2” and “1/4” undersampling (when is divisible by ).
Coil model
In coordinates where the image width and height run from −1 to 1, coils (4, 8 or 16) are placed at equal intervals on a circle of radius 1.2, starting at the top and going clockwise. The sensitivity of coil is:
- : distance from the coil. Sensitivity is strongest near the coil and falls off smoothly. With more coils each coil is smaller, so its reach is narrower.
- : phase. The coil's angle plus 2 radians × the position along the circle, so it rotates from place to place.
- : a factor that makes the root-sum-of-squares of all coil sensitivities equal to 1 at the image center.
and the phase rotation were chosen so that the g-factor for 8 coils at is similar to that of a real head coil. Over the region covered by the head phantom, the g-factor is:
| Coils | g-factor at 1/2 (mean, max) | g-factor at 1/4 (mean, max) |
|---|---|---|
| 4 ch | 1.06, 1.16 | 3.82, 8.88 |
| 8 ch | 1.05, 1.13 | 2.88, 5.59 |
| 16 ch | 1.04, 1.07 | 2.28, 3.00 |
Reception and undersampling
The image obtained by inverse-transforming the k-space you drew (before undersampling) is treated as the object , scaled so that its largest magnitude is 1. The image each coil gives when only 1 of every lines is acquired is, as in section 6, a sum of copies shifted vertically by :
To keep the calculation light, this sum is built directly in image space instead of going through k-space (it was confirmed to match undersampling in k-space and inverse-transforming; see section 8). The noise has the same properties as adding equal noise to every k-space point and then undersampling: it is independent for each pixel and coil, and Gaussian with standard deviation () in both the real and imaginary parts. Because only of the lines are acquired, the noise grows by . The same random numbers are used every time, so the noise does not flicker when the image is redrawn.
Unfolding (SENSE) and the g-factor
For each pixel, the overlapping values are found from the values of the coils by least squares. is the matrix of .
Without undersampling (), this is the same as combining the coil images weighted by their sensitivities. The g-factor tells how much more the noise grows on top of the caused by undersampling:
The g-factor is larger where the coil sensitivities differ less between the overlapping positions, and when approaches the number of coils. The small map in the sidebar shows the g-factor (black = 1, white = 4 or more), with blue dots marking the coil positions. Without noise and without regularization, the original image is restored exactly in theory.
Why the noise splits into bands at 1/4
At 1/4, four points 64 pixels apart vertically (for example ) overlap as one set. Within a set, the points near the top and bottom coils have clearly different sensitivities and a small g-factor, while the middle points, far from every coil, have a large one. With 8 coils, the g-factor along the center column of the image is:
| Vertical position | 0–63 (top 1/4) | 64–191 (middle half) | 192–255 (bottom 1/4) |
|---|---|---|---|
| g-factor | about 1.85 | about 2.7–3.0 | about 1.85 |
The sudden change at the boundaries happens because a partner jumps. The set for is {63, 127, 191, 255}, with one partner at the very bottom edge, but the set for is {0, 64, 128, 192}, and that partner moves to the very top edge. The image wraps around periodically, so aliasing treats the top and bottom edges as neighbors, yet the coil sensitivities at the two edges are completely different. For the point at , the top-edge point is a hard-to-separate partner seen strongly by the same top coil, so the g-factor rises there. On real scanners, too, the g-factor map forms bands every along the phase-encoding direction, but the steps are usually less distinct because many coils are arranged in 3D, regularization is used, and the field of view leaves some margin.
Regularization
Turning on “Regularization” adds to the unfolding equation (Tikhonov regularization), which holds back the large noise amplification where sensitivities differ little and unfolding is hard.
In exchange, some aliasing is left unfolded and the signal is slightly weakened (even without noise, the original image is not restored). is the value that minimizes the total error including noise for the head phantom at 1/4, for 4, 8 and 16 coils alike. With regularization, the g-factor can drop below 1 in places (shown black in the map).
| 1/4, head phantom | g-factor (mean, max) | Error without noise (remaining aliasing) | Error with noise |
|---|---|---|---|
| 4 ch, not regularized | 3.82, 8.88 | 0% | 42.5% |
| 4 ch, regularized | 2.13, 2.96 | 20.6% | 25.9% |
| 8 ch, not regularized | 2.88, 5.59 | 0% | 19.9% |
| 8 ch, regularized | 2.17, 3.98 | 10.2% | 17.4% |
| 16 ch, not regularized | 2.28, 3.00 | 0% | 12.0% |
| 16 ch, regularized | 1.94, 2.42 | 5.1% | 11.0% |
The error is the root sum of squared differences from the original image, divided by the root sum of squares of the original image.
Limits of the model case
- The coil sensitivities are assumed to be known exactly. Real scanners estimate them from a calibration scan, and errors in that estimate degrade the image.
- Noise correlation between coils, 3D coil arrangements, and residual aliasing when the object is larger than the field of view (FOV) are not included.
- Methods that unfold in k-space, such as GRAPPA, are not simulated. The regularization is the simplest kind (pulling toward 0), without the refinements real scanners use, such as a reference image.
8. Verification
The program's FFT results were checked against values computed by directly summing the definitions above (October 3, 2026).
- For both the forward and inverse transforms, the relative error was about 1×10−7, within the rounding error of single-precision floating-point arithmetic.
- When k-space drawn in symmetry mode was inverse-transformed, the imaginary part was at most 1×10−7 times the real part (i.e. the image is real-valued).
- Undersampling (October 7, 2026): with the head phantom, the 1/2 reconstruction matched the fold-over formula above with a relative error of about 2×10−7. Half Fourier filled by symmetry matched the reconstruction from all lines except the single line with a relative error of about 6×10−8.
- SENSE (October 7, 2026, head phantom): without noise, the result matched the original image with a relative error below 1×10−14 for 4, 8 and 16 coils and . The aliasing built in image space matched undersampling in k-space and inverse-transforming with a relative error of about 2×10−7. With no object, the measured noise for 8 coils at was 0.99–1.00 times the theoretical value computed from the g-factor (also 0.99–1.00 with regularization).
9. Differences from real MRI
The “k-space” in K-Space Paint is simply the 2D discrete Fourier transform of the image. Real MRI differs in the following ways.
- In MRI, k-space is the signal received by the receiver coils while gradient fields are applied. Under ideal conditions it is related to the image by the Fourier transform, but this app does not simulate how the signal is generated or acquired at all (relaxation, pulse sequences, gradients and so on).
- Magnetic field inhomogeneity and patient motion are not included. Noise and multiple receiver coils are handled only when SENSE is used, with the model case in section 7.
- k-space is treated as a 256×256 Cartesian grid. There is no non-Cartesian sampling (radial, spiral, etc.) or filtering (window functions). Undersampling is limited to the simple patterns in section 6. Parallel imaging unfolding is limited to SENSE with the model in section 7; there is no GRAPPA and no phase correction for half Fourier.
- The reconstructed image is a magnitude image, processed for display as described in section 3.