Trying to GetPerspective perfectly

Hello, I am alternating in an industrial vision company.

During my current project I have to, from a database do a WarpPerspective to recover only the envelope and store it in another database.
I know that my code is not complete and perfect but one thing particularly blocks me.

To obtain this Warp I decided to go through a rotation of the initial image, convert it to a shade of gray, make a blur (erode/dilate), find the corners then the extremum with argmin/max (using FindContours) . Finally, take the coordinates and perform the Warp.
However ! some envelopes may have an all-black stamp/logo.
In this case, my extremities will not be detected at the corners of the envelope, would you have a solution to help me?
I thank you in advance.

``````import imutils
import cv2
import numpy as np

Height = image.shape[0]
Width = image.shape[1]
center = (((Height//2)-800), (Width//2))
rotationMatrix = cv2.getRotationMatrix2D(center, 6, 0.8)
finalRotated = cv2.warpAffine(image, rotationMatrix,(Width, Height))
gray = cv2.cvtColor(finalRotated, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
Left = tuple(c[c[:, :, 0].argmin()][0])
Right = tuple(c[c[:, :, 0].argmax()][0])
Top = tuple(c[c[:, :, 1].argmin()][0])
Bot = tuple(c[c[:, :, 1].argmax()][0])
cv2.drawContours(finalRotated, [c], -1, (0, 255, 255), 2)
cv2.circle(finalRotated, Left, 8, (0, 0, 255), -1)
cv2.circle(finalRotated, Right, 8, (0, 255, 0), -1)
cv2.circle(finalRotated, Top, 8, (255, 0, 0), -1)
cv2.circle(finalRotated, Bot, 8, (255, 255, 0), -1)
print('left: {}'.format(Left))
print('right: {}'.format(Right))
print('top: {}'.format(Top))
print('bottom: {}'.format(Bot))
cv2.imwrite(r'C:\Users\Hugues\Desktop\Acquisition\Wrap\ToHugues20220214\thresh.jpg', thresh)
cv2.imwrite(r'C:\Users\Hugues\Desktop\Acquisition\Wrap\ToHugues20220214\Output.jpg', finalRotated)
pts1 = np.float32([[Left], [Top], [Bot], [Right]])
pts2 = np.float32([[0, 0], [Width, 0], [0, Height], [Width, Height]])
MatrixPerspective = cv2.getPerspectiveTransform(pts1, pts2)
Warp = cv2.warpPerspective(finalRotated , MatrixPerspective, (Width, Height))
cv2.imwrite(r'C:\Users\Hugues\Desktop\Acquisition\Wrap\ToHugues20220214\Warped\TestWarp.jpg', Warp)
cv2.imwrite(r'C:\Users\Hugues\Desktop\Acquisition\Wrap\ToHugues20220214\thresh.jpg', thresh)
cv2.imwrite(r'C:\Users\Hugues\Desktop\Acquisition\Wrap\ToHugues20220214\Output.jpg', finalRotated)
``````

pictures please. donâ€™t make us imagine.