1 | initial version |
What's happening is that your borders are different. In technique 1, Sobel uses BORDER_REFLECT101 to reflect the edges over the boundary. In technique 2, you replace the surroundings by zeros. That produces a lot of big gradients at the edge of your ROI, which are then inside the masked area.
The two ways of fixing this are to change the border options for technique 1 to BORDER_CONSTANT, or to provide some context for technique 2. Either by copying over slightly more information, performing the derivative on the original image, (Neither of these is quite exact, as the extra info does not exactly match the reflection) or manually reflecting the image (Which is exact, and just what T1 does now).