Skip to content

[cudawarping] Add align_corners option to cv::cuda::resize - #4181

Open
zhangnju wants to merge 1 commit into
opencv:5.xfrom
zhangnju:resize-align-corners
Open

[cudawarping] Add align_corners option to cv::cuda::resize#4181
zhangnju wants to merge 1 commit into
opencv:5.xfrom
zhangnju:resize-align-corners

Conversation

@zhangnju

Copy link
Copy Markdown

Summary

cv::cuda::resize with INTER_LINEAR currently uses the scale mapping src = dst * srcSize/dstSize and rounds the interpolated value to nearest. This PR adds an optional align_corners flag (INTER_LINEAR only) that instead maps src = dst * (srcSize-1)/(dstSize-1) and truncates toward zero.

This matches PyTorch's interpolate(mode="bilinear", align_corners=True) and the reference integer bilinear used by several ML image preprocessors (e.g. Qwen-VL), which is useful when a GPU preprocessing path must be numerically consistent with a CPU/reference pipeline.

Details

  • New bool align_corners = false parameter on cv::cuda::resize (default keeps existing behaviour; public InterpolationFlags enum unchanged).
  • Implemented via a new truncating-linear kernel resize_linear_trunc, selected through an internal interpolation sentinel; the default INTER_LINEAR path is untouched.
  • Verified element-wise against the CPU reference bilinear: max abs diff = 1/255 (a single quantization step), zero elements off by more than one quantum.

Pull Request Readiness Checklist

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable

cv::cuda::resize's INTER_LINEAR uses the scale mapping src = dst*srcSize/dstSize
and rounds to nearest. Add an optional align_corners flag (INTER_LINEAR only)
that instead maps src = dst*(srcSize-1)/(dstSize-1) and truncates toward zero,
matching PyTorch's interpolate(align_corners=True) and reference integer bilinear
used by ML image preprocessors (e.g. Qwen-VL). Verified element-wise (max abs
diff 1/255, zero elements off by more than a quantum) against the CPU reference.

Implemented via a new truncating-linear kernel selected through an internal
interpolation sentinel; the default path and public enum are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant