How to close the contour

Hi, I’m stuck with one problem for me. I process a photo with an object and under certain conditions, I get a result that is reflected in the attached screenshot.

I want to close the rectangle contour by fitting it into a rectangle for the next extraction.

Can you please help me with the info, article, or any example, of how to handle this?

The result selection should be like this:

Thanks in advance.

Add a black border

I want to have the selection like this

try approxyPolyDP