Python and OpenCV. How do I detect all (filled)circles/round objects in an image?

后端 未结 3 1971
鱼传尺愫
鱼传尺愫 2020-12-23 15:19

I am trying to make a program which opens an image, scans it for circles/round shapes and returns the coordinates so that I can use the cv.Circle function to dr

3条回答
  •  醉话见心
    2020-12-23 15:48

    Have a look at my answer to this question for some working source code (it's C but I used a C++ compiler cause it's more lenient).

    First, I cropped your image (to get something convenient to work with) and applied a threshold to your image to separate the foreground from the background:

    enter image description here

    Then I directly applied the source code to the thresholded image. Here is the text output:

    center x: 330 y: 507 A: 13 B: 4
    center x: 78 y: 507 A: 22 B: 4
    center x: 270 y: 503 A: 8 B: 8
    center x: 222 y: 493 A: 21 B: 17
    center x: 140 y: 484 A: 17 B: 18
    center x: 394 y: 478 A: 17 B: 15
    center x: 311 y: 468 A: 8 B: 8
    center x: 107 y: 472 A: 12 B: 12
    center x: 7 y: 472 A: 6 B: 19
    center x: 337 y: 442 A: 10 B: 9
    center x: 98 y: 432 A: 10 B: 10
    center x: 357 y: 421 A: 7 B: 7
    center x: 488 y: 429 A: 22 B: 23
    center x: 411 y: 400 A: 13 B: 12
    center x: 42 y: 400 A: 11 B: 12
    center x: 365 y: 391 A: 14 B: 13
    center x: 141 y: 396 A: 19 B: 19
    center x: 9 y: 379 A: 8 B: 18
    center x: 192 y: 365 A: 10 B: 9
    center x: 347 y: 340 A: 20 B: 20
    center x: 8 y: 305 A: 7 B: 13
    center x: 95 y: 308 A: 23 B: 24
    center x: 318 y: 297 A: 15 B: 15
    center x: 159 y: 285 A: 10 B: 10
    center x: 412 y: 291 A: 26 B: 27
    center x: 504 y: 278 A: 6 B: 16
    center x: 233 y: 277 A: 20 B: 20
    center x: 459 y: 256 A: 15 B: 15
    center x: 7 y: 239 A: 6 B: 9
    center x: 377 y: 239 A: 14 B: 14
    center x: 197 y: 228 A: 12 B: 12
    center x: 302 y: 237 A: 12 B: 22
    center x: 98 y: 224 A: 24 B: 23
    center x: 265 y: 203 A: 18 B: 18
    center x: 359 y: 202 A: 22 B: 22
    center x: 149 y: 201 A: 20 B: 21
    center x: 219 y: 169 A: 7 B: 9
    center x: 458 y: 172 A: 20 B: 20
    center x: 497 y: 157 A: 13 B: 21
    center x: 151 y: 125 A: 18 B: 17
    center x: 39 y: 109 A: 9 B: 10
    center x: 81 y: 116 A: 20 B: 19
    center x: 249 y: 104 A: 14 B: 13
    center x: 429 y: 76 A: 23 B: 24
    center x: 493 y: 33 A: 11 B: 10
    center x: 334 y: 26 A: 12 B: 14
    

    And here is the output image:

    enter image description here

    The main problem is that circles that have merged together have not been detected at all. The code was originally written for detecting filled ellipses only, so you can probably deal with this issue by tweaking the code.

提交回复
热议问题