Crop¶
| Fact | Value |
|---|---|
| Also called | Trim, Crop to selection |
| Status | Implemented and tested |
| Family | Utilities |
| Engine | Browser |
| Type | Linear, geometric, crop |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 0 |
| Code | web/src/core/image/transform.ts, crop |
| Operator code | web/src/core/image/transform.ts, crop |
Overview¶
Crop keeps a rectangle of the image and drops the rest. Open it from Image > Crop. When there is a selection, the rectangle starts at its bounds, so a marquee followed by Crop cuts the image to it. Without a selection, the rectangle starts at the whole image, and you type the edges. Pixel values never change, and the selection is cropped with the image.
How it works¶
- Choose the rectangle, or let the selection choose it.
- Copy every pixel inside it to the new image, as it is.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(x_0, y_0\): The left and top edges of the rectangle
- \(w, h\): The width and height of the rectangle
The output pixel at x', y' is the input pixel x_0 columns further right and y_0 rows further down. Only the w x h pixels of the rectangle are kept. The selection is cropped the same way.
A crop moves the origin of the coordinates to the corner of the rectangle and keeps only the pixels inside it. Since no value changes, cropping and then applying a point filter gives the same result as the other way round. Filters with a window are different near the new edges, where they now see the border mode instead of the old neighbors.
Kernel or operator¶
There is no kernel. Each output pixel is one input pixel.
Algorithm¶
- Cut the rectangle to the image.
- Copy each row of the rectangle into the new image.
- Crop the selection with the same rectangle.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Left x |
0 | 0 to 65535 px | The column of the left edge of the rectangle. |
Top y |
0 | 0 to 65535 px | The row of the top edge of the rectangle. |
Width width |
1 | 1 to 65535 px | The width of the rectangle. The dialog starts at the selection or the image. |
Height height |
1 | 1 to 65535 px | The height of the rectangle. The dialog starts at the selection or the image. |
Implementation¶
The code is in web/src/core/image/transform.ts. There, crop copies each row of the rectangle as 32-bit words, one per pixel, and clampCrop first cuts the rectangle to the image. cropActive in web/src/app/geometryActions.ts crops the image and the selection. It also records the step in the history. The dialog shows the size of the result before you apply it.
Worked example¶
Take an image of 3 x 2 pixels whose red values number the pixels. Crop the rectangle at x 1, y 0, with width 2 and height 2:
A rectangle at x -2, y 1 with width 4 and height 9 reaches past the image. It is cut to x 0, y 1, width 2 and height 1 first. A rectangle that starts at x 3 lies wholly outside and is refused.
Complexity¶
- Time: \(O(w h)\)
- Memory: \(O(w h)\)
w and h are the width and height of the result.
Artifacts and limitations¶
Limitations
- Only rectangles parallel to the image edges.
Artifacts
- None. Pixels are copied, not resampled.
Tests¶
The unit tests check the example above, that all four channels are copied unchanged, and that rectangles are cut to the image or refused.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018.