welcome.
that… looks like a bug to me. corrupted memory, something writing to memory where it shouldn’t. I’m sure this happens regardless of picture content.
do you see this issue when you imshow the output right after calling the function? at this point, does the source image contain any of these defects?
please prepare a “minimal reproducible example” including necessary data.
with what version of OpenCV do you see this issue?