I have to find the contours of boxes.
Some boxes have diagonal inside of them. I try to remove diagonal but I think it isn't answer.
Here are the images those I preprocessing and contour result. Only the contour of the box with diagonal can't be found.



(Edit. I attach original image, Thanks for comment)
Here is the code
def extract_blocks(img, debug_mode: bool = False):
if debug_mode:
img = cv2.imread(img)
new_w = 710
new_h = 710
img = cv2.resize(img, (new_w, new_h))
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gau = cv2.GaussianBlur(gray_img, (5, 5), 0)
laplacian_gau = cv2.Laplacian(gau,cv2.CV_8U,ksize=5)
blank = np.ones_like(img) * 255
contours, hierarchy = cv2.findContours(laplacian_gau, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
print(f"Number of Contours: {len(contours)}")
blocks = []
for cnt in contours:
area = cv2.contourArea(cnt)
if area < 1900: # Skip too small contour
continue
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int32(box)
w, h = rect[1] # width, height
if 50 < min(w, h) < 80 and 50 < max(w, h) < 80:
blocks.append(box)
cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
cv2.drawContours(blank, [box], 0, (0, 0, 255), 2)
cv2.imshow('laplacian_gau', laplacian_gau)
cv2.imshow("Detected Blocks", img)
cv2.imshow("Detected Blocks (White Background)", blank)
cv2.waitKey(0)
cv2.destroyAllWindows()
I tried to remove red colors to white, but I can't remove diagonal. I tried to make morphology, but it isn't answer.
The box in the image can be rotated.
