0

My idea was to implement a simple square detection using openCV & C++ (objc++). I've already extracted the biggest areas of the image like you can see below (the colored ones) but now I'd like to extract the corner points (like TopLeft, TopRight, BottomLeft, BottomRight) of all the areas to afterwards check if the distance between the 4 corners is similar on each of them or the angle between the lines is nearly 45°.

See the images I was talking about:

Image1

Image2

However - I got to this point where I've tried to extract the areas corner points to get something like this afterwards:

Image 3

This was my first idea how to get the 4 corner points (See the steps below):

1.calculate the contours center by

for (int i=0; i<contourPoints.size(); i++) {
   avgx += contourPoints[i].x;
   avgy += contourPoints[i].y;
}
avgx/=contourPoints.size(); // centerx
avgy/=contourPoints.size(); // centery

2.loop trough all contour points to get the points with the highest distance to the center --> Probably the corners if the contour is a square/rectangle

for (int i=0; i<contourPoints.size(); i++) {
   dx = abs(avgx - contourPoints[i].x);
   dy = abs(avgy - contourPoints[i].y);
   dist = sqrt( dx*dx + dy*dy );
   distvector.push_back(dist);
}
// sort distvector > distvector and get 4 corners with highest distance to the center -> hopefully the corners.

This procedure was my idea but I'm pretty sure there must be a better way to detect squares and extract it's corner points using just the given contour coordinates.

So any help how to improve my code to get a way better & more efficient detection would be very appreciated. Thanks a million in advance, Tempi.

  • [This](https://github.com/alyssaq/opencv) might help – Dmitrii Z. Apr 09 '18 at 17:14
  • your image only shows 1 contour with 4 vertices. the first step in finding square contours is obviously to filter out any contour with less or more than 4 points. – Piglet Apr 09 '18 at 17:43
  • I won't share code. there are alread houndreds of examples online on how to detect rectangles in an image. no need to add more. all you need to do then is to check the dimensions to get squares. – Piglet Apr 09 '18 at 18:33
  • I do not use OpenCV but in low level see [bullet #2 detect square corner points](https://stackoverflow.com/a/39316776/2521214) with slight modification you might find also which corner this is. I would check pixels along circle circumference (if center pixel is set) and count set pixels for each octant. if you got 2 not neighboring or not opposing octants set high (and the rest is clear or very low) you got your corner and also know which one ... If it is something interesting for you I might generate an answer with C++ example – Spektre Apr 10 '18 at 07:25

0 Answers0