Class Cv2
OpenCV's cv2.resize for uint8 and float32 images, bit for bit: InterArea, the interpolation
Gymnasium's ResizeObservation uses, and InterLinear, the default the vector
HumanRendering uses. Use it wherever Gymnasium calls cv2.resize; Gym.NET carries no native OpenCV.
public static class Cv2
- Inheritance
-
Cv2
- Inherited Members
Remarks
A C# translation of the 8-bit paths of OpenCV 4.13.0's cv::resize (decision DR-23): the integer-factor
area average (resizeAreaFast_, with the 2×2 kernel's own rounding), the general area decimation
(resizeArea_ with computeResizeAreaTab's weights, in float32), and the fixed-point bilinear kernel
(resizeGeneric_ with 11-bit coefficients), which INTER_AREA also takes, with its own coefficients,
when either axis grows. The arithmetic follows OpenCV's statement for statement: float32 where OpenCV computes in
float, round-half-to-even where it calls cvRound, and the integer shifts of its fixed-point kernels, so
the bytes equal the pinned cv2's, which the parity suite checks against live OpenCV.
uint8 and float32 images are supported (DR-46; Gymnasium's own callers require uint8). The float32 paths are
OpenCV's float instantiations of the same kernels: the area-fast mean (its 2×2 vector kernel for 1 or 4 channels
summing pairwise over whole groups of four outputs, the generic sum in groups of four, OpenCV's unrolling), the
general area decimation copied out unrounded, and the float bilinear kernel. Footgun, float32
InterLinear only: the cv2 wheels hand it to Intel IPP when IPP is on (the default, measured
on 2026-11-10: 255 of 400 random cases differ from OpenCV's own kernel, by up to 4.4e-4), and IPP's algorithm is
not published; this method gives OpenCV's own result, what cv2 computes with cv2.ipp.setUseIPP(False)
or on a build without IPP. InterArea is the same either way.
Like cv2, a single-channel image comes back two-dimensional, whether it was given as (H, W) or
(H, W, 1). Each call allocates its result and a few tables; it is single-threaded, where OpenCV splits
rows across threads (which does not change its results).
Third-party notice: OpenCV is © Intel Corporation, Willow Garage Inc., Itseez Inc. and contributors, licensed under the Apache License 2.0; see THIRD-PARTY-NOTICES.md. Reference: modules/imgproc/src/resize.cpp.
Fields
InterArea
cv2.INTER_AREA: area averaging when shrinking (both axes), a bilinear variant otherwise.
public const int InterArea = 3
Field Value
InterLinear
cv2.INTER_LINEAR: bilinear interpolation, OpenCV's default.
public const int InterLinear = 1
Field Value
Methods
Resize(NDArray, (int Width, int Height), int)
cv2.resize(src, dsize, interpolation=interpolation) for a uint8 or float32 image: a new
(height, width) or (height, width, channels) image of the same dtype.
public static NDArray Resize(NDArray src, (int Width, int Height) dsize, int interpolation = 1)
Parameters
srcNDArrayThe image,
(H, W)or(H, W, C)uint8 or float32, of any layout; it is not changed.dsize(int Width, int Height)The target size, OpenCV's order:
(width, height).interpolationintInterArea or InterLinear.
Returns
- NDArray
A new array of
src's dtype the caller owns; two-dimensional for a single channel, ascv2returns it.
Remarks
Reference: modules/imgproc/src/resize.cpp (cv::resize).
Exceptions
- ArgumentNullException
Thrown when
srcis null.- NotSupportedException
Thrown for a dtype other than uint8 and float32, an interpolation other than the two, or more than 4 channels on the general area path (OpenCV's
cn <= 4assertion).- ValueError
Thrown for an image that is not 2-D or 3-D or is empty, or a target size that is not positive.