I've been using this piece of code from DJI dev guide (search for section "Cropping the Panorama"). https://dji-dev.gitbooks.io/mobile-sdk-tutorials/content/en/iOS/PanoDemo/PanoDemo_en.html#stitching-photos
The algorithm does not seem very robust at first glance, but so far it has proven to be good enough for my stitching use cases. However, I believe the code got a couple of bugs on these two lines
bool isBottomNotBlack=checkBlackRow(gray, roiRect.y+roiRect.height,roiRect);
bool isRightNotBlack=checkBlackColumn(gray, roiRect.x+roiRect.width,roiRect);
which should be changed to
bool isBottomNotBlack=checkBlackRow(gray, roiRect.y+roiRect.height-1,roiRect);
bool isRightNotBlack=checkBlackColumn(gray, roiRect.x+roiRect.width-1,roiRect);
Otherwise, you may get non-systematic C's bad excess error.
Hope that helps!