At first glance, I was very surprised when I came across this issue. Moreover, I did not think that finding the edge of Canny would be so deceiving. So I took a similar image and applied a canny to it. To my surprise, I ran into the same problem you encountered. Why is this so?
After switching to the documentation, I came across many operations that took place under the hood.
The documentation claims that Gaussian filtering is performed to reduce noise. Well that's true. But this blurs the existing edges present in the image. Therefore, when you blur a perfect square / rectangle, it has curved corners.
After Gaussian filtering, the next step is to find the edge gradient. As already mentioned, by now the ideal edge of the square / rectangle is gone due to blurring (Gaussian filtering). There are rounded / curved edges. Finding the intensity of the gradients on rounded / curved edges will never produce a perfect square / rectangular edge. I may be mistaken, but I assume that this is the main reason why we do not get perfect edges when doing Canny edge detection.
If you want the perfect edge, my suggestion would be to try to find the contours (as suggested by Mickey) and draw a bounding box.
source share