I am working on a similar project and have not yet obtained the results I’m looking for. You can at least get rid of the black space by cropping the image. You might also try playing around with the free scaling parameter when rectifying.
From https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html: “Free scaling parameter. If it is -1 or absent, the function performs the default scaling. Otherwise, the parameter should be between 0 and 1. alpha=0 means that the rectified images are zoomed and shifted so that only valid pixels are visible (no black areas after rectification). alpha=1 means that the rectified image is decimated and shifted so that all the pixels from the original images from the cameras are retained in the rectified images (no source image pixels are lost). Any intermediate value yields an intermediate result between those two extreme cases.”
I found a solution that works for my project. Perhaps it will work for yours as well. I changed my free scaling parameter from 1 to 0.5 in the matrix optimization and it almost entirely eliminated the problem.
In your code, try changing line 58 from:
new_mtxR, roiR= cv2.getOptimalNewCameraMatrix(mtxR,distR,(wR,hR),1,(wR,hR))
to:
new_mtxR, roiR= cv2.getOptimalNewCameraMatrix(mtxR,distR,(wR,hR),0.5,(wR,hR))
See if it helps your issue. BTW, it would still be a good idea to crop the output using the ROI.
Just a thought: there are separate free scaling parameters for both the calibration and for the stereo rectification. Did you try reducing the value on both?