Python/C++ numerical differences in erode function. Bug?

Hello,
I am experiencing an issue while porting a Python application to C++ because erode gives slightly different results on the very same input.
For example, on this image taken from Google (for your convenience, already converted to jpg):
https://transfer.sh/n3BPNQPpPg/gatto.jpg

I have checked all the intermediate values that ultimately gets into erode and they are all the same (an accurate check is performed by comparing pixels one by one).

It seems the problem is at the boundaries, as you can see by comparing the two output images (cpp vs py):

I do some pre-processing that is needed for my use case. The repro snippets are here below.

Python:

import sys

import cv2

crop_perc = 0.1

image_path = "cat.jpg"
img = cv2.imread(image_path, cv2.IMREAD_COLOR)
orig_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(orig_gray, 5)
thresholded = 255 - cv2.adaptiveThreshold(
    gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 33, 4
)
thresholded = thresholded[
    int(thresholded.shape[0] * crop_perc) : int(thresholded.shape[0] * (1 - crop_perc)),
    :,
]
erosion_size = 3
element = cv2.getStructuringElement(
    cv2.MORPH_ELLIPSE,
    (1 * erosion_size + 1, 2 * erosion_size + 1),
    (erosion_size, erosion_size),
)
thresholded_morph = cv2.erode(thresholded, element)

cv2.imshow("thresholded_morph", thresholded_morph)
cv2.waitKey(0)
cv2.imwrite("thresholded_morph_py.png", thresholded_morph)

And C++:

auto img = cv::imread("cat.jpg");
cv::Mat gray, original_gray;
cvtColor(img, original_gray, cv::COLOR_BGR2GRAY);
medianBlur(original_gray, gray, 5);

cv::Mat thresholded;
adaptiveThreshold(gray, thresholded, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 33, 4);
thresholded = 255 - thresholded;

const auto crop_perc = 0.1;
thresholded = thresholded(cv::Range(static_cast<int>(thresholded.size[0] * crop_perc), static_cast<int>(thresholded.size[0] * (1 - crop_perc))), cv::Range::all());

const auto erosion_size = 3;
const auto element = getStructuringElement(cv::MORPH_ELLIPSE, { 1 * erosion_size + 1, 2 * erosion_size + 1 }, { erosion_size, erosion_size });
cv::Mat thresholded_morph;
erode(thresholded, thresholded_morph, element);

imwrite("thresholded_morph_cpp.png", thresholded_morph);

Do you have any ideas what is causing the issue? Clearly, I am using the very same version of OpenCV for both Python and C++.
Also, it seems the error is portable to other platforms (e.g. MacOS).

Many thanks,

Marco

Hi,

Make a new program :

  • use imread to read an image
  • save image in yml format

Now modify python and c++ program to read yml image and check if there is still a diffence

clearly? what version? how do you check?

you mentioned “other platforms”. did you compare on the same platform?

The version is checked by printing and comparing cv::getVersionString() in C++ and cv2.__version__in Python. I have tested these versions: 4.5.4, 4.5.5, 4.6.0, 4.7.0, 4.7.8.

I mentioned other platforms as I conducted an extra test, but the primary issue initially surfaced on x64 Windows. The C++ program is built with Visual Studio 2022.

@laurent.berger I will try, thanks for the tip.

@laurent.berger writing and then loading from yml gives the very same issue.

use cout << getBuildInformation() in c++ to check opencv configuration and print(cv.getBuildInformation()) in python

I don’t know if it’s relevant, but some immediate differences are (left is C++, right is Python):

The python library is installed as pip install opencv-python, the C++ one is downloaded from the official mirror and linked dynamically.

For completeness, both the logs follow.

C++:

General configuration for OpenCV 4.8.1 =====================================
  Version control:               4.8.1

  Platform:
    Timestamp:                   2023-09-27T16:57:13Z
    Host:                        Windows 10.0.19045 AMD64
    CMake:                       3.23.3
    CMake generator:             Visual Studio 16 2019
    CMake build tool:            C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe
    MSVC:                        1928
    Configuration:               Debug Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (16 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (7 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (35 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (5 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe  (ver 19.28.29334.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MD /O2 /Ob2 /DNDEBUG
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MDd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP   /MD /O2 /Ob2 /DNDEBUG
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP /MDd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching video videoio world
    Disabled:                    python3
    Disabled by dependency:      -
    Unavailable:                 java python2 ts
    Applications:                apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI:
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.13)
    JPEG:                        build-libjpeg-turbo (ver 2.1.3-62)
      SIMD Support Request:      YES
      SIMD Support:              NO
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.5.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2021.8 [2021.8.0]
           at:                   C:/GHA-OCV-2/_work/ci-gha-workflow/ci-gha-workflow/build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2021.8.0)
              at:                C:/GHA-OCV-2/_work/ci-gha-workflow/ci-gha-workflow/build/3rdparty/ippicv/ippicv_win/iw
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  OpenCL:                        YES (NVD3D11)
    Include path:                C:/GHA-OCV-2/_work/ci-gha-workflow/ci-gha-workflow/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            C:/Python-3.9/python.exe

  Java:
    ant:                         C:/apache-ant-1.9.15/bin/ant.bat (ver 1.9.15)
    Java:                        NO
    JNI:                         C:/Program Files/Java/jdk-11.0.9/include C:/Program Files/Java/jdk-11.0.9/include/win32 C:/Program Files/Java/jdk-11.0.9/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    C:/GHA-OCV-2/_work/ci-gha-workflow/ci-gha-workflow/install
-----------------------------------------------------------------

Python:

General configuration for OpenCV 4.8.1 =====================================
  Version control:               4.8.1

  Platform:
    Timestamp:                   2023-09-27T14:21:39Z
    Host:                        Windows 10.0.17763 AMD64
    CMake:                       3.24.2
    CMake generator:             Visual Studio 14 2015
    CMake build tool:            MSBuild.exe
    MSVC:                        1900
    Configuration:               Debug Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (16 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (7 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (35 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24247.2)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MT /O2 /Ob2 /DNDEBUG
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MTd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP   /MT /O2 /Ob2 /DNDEBUG
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP /MTd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO  /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
    Linker flags (Debug):        /machine:x64  /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          wsock32 comctl32 gdi32 ole32 setupapi ws2_32
    3rdparty dependencies:       libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf zlib quirc ippiw ippicv

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
    Disabled:                    java world
    Disabled by dependency:      -
    Unavailable:                 python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI:                           WIN32UI
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.13)
    JPEG:                        build-libjpeg-turbo (ver 2.1.3-62)
      SIMD Support Request:      YES
      SIMD Support:              NO
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.5.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2021.8 [2021.8.0]
           at:                   D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.7/cmake-build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2021.8.0)
              at:                D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.7/cmake-build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  OpenCL:                        YES (NVD3D11)
    Include path:                D:/a/opencv-python/opencv-python/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/hostedtoolcache/windows/Python/3.7.9/x64/python.exe (ver 3.7.9)
    Libraries:                   C:/hostedtoolcache/windows/Python/3.7.9/x64/libs/python37.lib (ver 3.7.9)
    numpy:                       C:/hostedtoolcache/windows/Python/3.7.9/x64/lib/site-packages/numpy/core/include (ver 1.17.0)
    install path:                python/cv2/python-3

  Python (for build):            C:\hostedtoolcache\windows\Python\3.7.9\x64\python.exe

  Java:
    ant:                         NO
    Java:                        YES (ver 1.8.0.382)
    JNI:                         C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.382-5/x64/include C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.382-5/x64/include/win32 C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.382-5/x64/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.7/cmake-install
-----------------------------------------------------------------

THAT could maybe explain it. different hand-written optimized versions.

what CPUs are you running the C++ version on? are any of these CPUs even capable of AVX512?

I’m wondering if there’s a way to limit the capabilities selection at execution time.

I am running both the Python and C++ programs on an Intel processor i7-11850H that supports AVX512:
image

weird there is no difference when I check mat values

c++ code :

        FileStorage fs("testcpp.yml", FileStorage::WRITE);
        auto img = cv::imread("C:/Users/laurent/Desktop/cat.jpg");
        fs << "Original" << img;
        cv::Mat gray, original_gray;
        cvtColor(img, original_gray, cv::COLOR_BGR2GRAY);
        fs << "Original gray" << original_gray;
        medianBlur(original_gray, gray, 5);
        fs << "blur" << gray;

        cv::Mat thresholded, thresholded_crop;
        adaptiveThreshold(gray, thresholded, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 33, 4);
        thresholded = 255 - thresholded;
        fs << "thresholded" << thresholded;

        const auto crop_perc = 0.1;
        thresholded_crop = thresholded(cv::Range(static_cast<int>(thresholded.size[0] * crop_perc), static_cast<int>(thresholded.size[0] * (1 - crop_perc))), cv::Range::all());
        fs << "thresholded crop" << thresholded_crop;

        const auto erosion_size = 3;
        const auto element = getStructuringElement(cv::MORPH_ELLIPSE, { 1 * erosion_size + 1, 2 * erosion_size + 1 }, { erosion_size, erosion_size });
        cv::Mat thresholded_morph;
        erode(thresholded, thresholded_morph, element);
        fs << "thresholded morph" << thresholded_morph;
        fs.release();
        imwrite("thresholded_morph_cpp.png", thresholded_morph);
        imshow("test", thresholded_morph);
        waitKey();

python code and check in python

import sys
import numpy as np
import cv2

crop_perc = 0.1

image_path = r"C:\Users\laurent\Desktop\cat.jpg"
fs = cv2.FileStorage("testpy.yml", 1)
img = cv2.imread(image_path, cv2.IMREAD_COLOR)
fs.write("Original", img)

orig_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
fs.write("Original gray", orig_gray)
gray = cv2.medianBlur(orig_gray, 5)
fs.write("blur", gray)
cv2.imshow("medianBlur", gray)

thresholded = 255 - cv2.adaptiveThreshold(
    gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 33, 4
)
fs.write("thresholded", thresholded)
thresholded_crop = thresholded[
    int(thresholded.shape[0] * crop_perc) : int(thresholded.shape[0] * (1 - crop_perc)),
    :,
]
fs.write("thresholded crop", thresholded_crop)
erosion_size = 3
element = cv2.getStructuringElement(
    cv2.MORPH_ELLIPSE,
    (1 * erosion_size + 1, 2 * erosion_size + 1),
    (erosion_size, erosion_size),
)
thresholded_morph = cv2.erode(thresholded, element)
fs.write("thresholded morph", thresholded_morph)
fs.release()
cv2.imshow("thresholded_morph", thresholded_morph)
cv2.waitKey(0)
cv2.imwrite("thresholded_morph_py.png", thresholded_morph)


fscpp = cv2.FileStorage("testcpp.yml", cv2.FileStorage_READ)
orig_gray_cpp  = fscpp.getNode("Original gray").mat()
print (np.max(np.abs(orig_gray_cpp-orig_gray)))
gray_cpp  = fscpp.getNode("blur").mat()
print (np.max(np.abs(gray_cpp-gray)))
thresholded_cpp  = fscpp.getNode("thresholded").mat()
print (np.max(np.abs(thresholded_cpp-thresholded)))
thresholded_crop_cpp  = fscpp.getNode("thresholded crop").mat()
print (np.max(np.abs(thresholded_crop_cpp-thresholded_crop)))
thresholded_morph_cpp  = fscpp.getNode("thresholded morph").mat()
print (np.max(np.abs(thresholded_morph_cpp-thresholded_morph)))

There is a typo in your code, erode has to be called on thresholded_crop and not on thresholded:

thresholded_morph = cv2.erode(thresholded, element)

By replacing this above with this below:

thresholded_morph = cv2.erode(thresholded_crop, element)

And also the same in the C++ snippet:

erode(thresholded_crop, thresholded_morph, element);

the check fails on my machine, just as before.

1 Like

Ok for the typo

Now change in c++ code

thresholded_crop = thresholded(cv::Range(static_cast<int>(thresholded.size[0] * crop_perc), static_cast<int>(thresholded.size[0] * (1 - crop_perc))), cv::Range::all());

in

thresholded(cv::Range(static_cast<int>(thresholded.size[0] * crop_perc), static_cast<int>(thresholded.size[0] * (1 - crop_perc))), cv::Range::all()).copyTo(thresholded_crop);

and I think problem is solved

FileStorage fs("testcpp.yml", FileStorage::WRITE);
auto img = cv::imread("C:/Users/laurent/Desktop/cat.jpg");
fs << "Original" << img;
cv::Mat gray, original_gray;
cvtColor(img, original_gray, cv::COLOR_BGR2GRAY);
fs << "Original gray" << original_gray;
medianBlur(original_gray, gray, 5);
fs << "blur" << gray;

cv::Mat thresholded, thresholded_crop;
adaptiveThreshold(gray, thresholded, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 33, 4);
thresholded = 255 - thresholded;
fs << "thresholded" << thresholded;

const auto crop_perc = 0.1;
thresholded(cv::Range(static_cast<int>(thresholded.size[0] * crop_perc), static_cast<int>(thresholded.size[0] * (1 - crop_perc))), cv::Range::all()).copyTo(thresholded_crop);
fs << "thresholded crop" << thresholded_crop;

const auto erosion_size = 3;
const auto element = getStructuringElement(cv::MORPH_ELLIPSE, { 1 * erosion_size + 1, 2 * erosion_size + 1 }, { erosion_size, erosion_size });
cv::Mat thresholded_morph;
erode(thresholded_crop, thresholded_morph, element);
fs << "thresholded morph" << thresholded_morph;
fs << "element" << element;
fs.release();
imwrite("thresholded_morph_cpp.png", thresholded_morph);
imshow("test", thresholded_morph);
waitKey();

Many thanks, it solves the issue!

At this point I am wondering why. Both approaches essentially generate identical matrix content, especially when printed or saved. From a user’s standpoint, it does seem somewhat weird. Do you expect this?

In C++, when you use the operator () with a OpenCV matrix, data is not copied, and there are some values outside of your matrix. The erode function takes care of these values.

In Python, data is copied.

I see operator() like a subspan that adjusts the matrix boundaries (e.g. the pointers) without copying the data.
However, my expectation is that functions like erode would utilize these updated boundaries. Is this assumption wrong?

not necessarily. especially not with numpy. numpy is very good at sharing views. numpy is even more capable of handling views than cv::Mat is.