0

I need to sort an array of circles (as result of HoughCircles function) by top to bottom and left to right but I can't manage to do so.

I used the python built-in sorted method as follows:

circles = np.round(circles[0, :]).astype("int")
circles = sorted(circles, key=lambda v: [v[0], v[1]])

And I get the following result: image

For instance, in this example image it jumps from circle 61 to 64, from 96 to 100 and so on.

It wrongly sorted some circles, and it seems that it's because some circles are a pixel or two misaligned, causing these mistakes.

EDIT:

These are my raw data and sample image:

sample image

raw data of circles [[y, x, radius]]

sorting snippet

6
  • can you please explain more? if you can provide the original image before sorting, that will help to understand exactly the input before sorting, and you want to sort according to the number inside the circle? or depending o the colour of the circle?? Commented May 12, 2020 at 0:15
  • The numbers inside each circle I put just to show that the ordering is incorrect. And the colours of the circles are not relevant for this case, it's just a circle classification. The original image is an answer sheet and the green circles are the filled ones and the red are the blank ones. Commented May 12, 2020 at 9:49
  • What I understood so far is that you want to automate the correction of the answer sheet, am I right? can you please provide a raw sample without numbers or colours? so I can see the problem more clearly. Commented May 12, 2020 at 10:02
  • Yep, i'm automating the answer sheet correction. I edited the question with the raw sample. The main reason I want to sort the array is to identify the question and alternative of each circle by iterating the sorted array Commented May 12, 2020 at 11:10
  • Can you please share the whole code which gave wrong results? Commented May 12, 2020 at 12:20

1 Answer 1

1

As an alternative, after you have sorted with [v[0],v[1]] you make a new sort over a slice of the original list with the number of rows as block size. The downside of this solution is that you will be tied to the number of rows on your answer sheet.

    ...
    circles = np.round(circles[0,:]).astype("int")
    circles = sorted(circles, key=lambda v: [v[0], v[1]])

    NUM_ROWS = 30

    sorted_cols = []
    for k in range(0, len(circles), NUM_ROWS):
        col = circles[k:k+NUM_ROWS]
        sorted_cols.extend(sorted(col, key=lambda v: v[1]))

    circles = sorted_cols

    img_colored = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

    if circles is not None:
    ...

answer sheet comparison

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It did work as I wanted. I managed to write another solution but yours does it better. My solution was to find the clusters of the circles' grid and then change each circle point to its cluster's center point.
Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.