You can try to use the scanning algorithm. For each line of the polygon (so y = min (y) .. max (y)), get l = min (x) and r = max (x). Calculate the left / right slope (deltax) and compare it with the slope of the line to. If it has changed (use some tolerance here), you are in the corner of the rectangle (or next to it). This will not work for all cases, since the slope may not be accurate due to low resolution, but for large rectangles and slopes are not too similar, this should work.
At least it works well for your example:
X0000000000X l = 0, r = 11 .00000000000 l = 1, r = 11, deltaxl = 1, deltaxr = 0 ..X000000000 l = 2, r = 11, deltaxl = 1, deltaxr = 0 .....0000000 l = 5, r = 11, deltaxl = 3, deltaxr = 0 ........0000 l = 8, r = 11, deltaxl = 3, deltaxr = 0 ...........X l = 11, r = 11, deltaxl = 3, deltaxr = 0
You start at the top of the rectangle, where you get two different values ββfor l and r, so you already have two corners. On the left side for the first three lines you will get deltax = 1, but after that you will get deltax = 3, so there is an angle (3, 3). Nothing changes on the right side, deltax = 0, so you only get a point at the end.
Note that you are collecting corners here, so if you do not have 4 corners at the end, the slopes were too similar (or you have a triangle image), and you can switch to another (more accurate) algorithm or just give an error. The same thing if you have more than 4 corners or some other strange things, such as holes in a rectangle. There seems to be some kind of image definition, so these cases can happen, right?
There are cases where a simple deltax = (x - lastx) will not work well, see this example for the left side of the rectangle:
xxxxxx xxxxx deltax = 1 dy/dx = 1/1 = 1 xxxxx deltax = 0 dy/dx = 2/1 = 2 xxxx deltax = 1 dy/dx = 3/2 = 1.5 xxxx deltax = 0 dy/dx = 4/2 = 2 xxx deltax = 1 dy/dx = 5/3 = 1.66
Sometimes deltax is 0, sometimes it is 1. It is better to use the slope of the line from the actual point to the upper left / right point (deltay / deltax). Using it, you still have to adhere to the tolerance, but your values ββwill be more accurate with each new line.