0

I am trying to detect roi in my image dataset using Otsu thresholding. While in some cases results are on the point, some cases are not that good. I'm using the code below.

import cv2 as cv
import numpy as np

path  = "./images/{}.png".format

for num in range(1000):
    filename  = path(num)
    img       = cv.imread(filename, cv.IMREAD_GRAYSCALE)
    res       = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1]
    res       = np.hstack((img, res))
    cv.imwrite(filename.replace(".png", "_.png"), res)

While these results are acceptable

enter image description here enter image description here

These definitely need to be improved

enter image description here enter image description here

How can I improve my code?

gokdumano
  • 70
  • 6
  • It's not clear what the output should be. Do you want the circle in the center only? –  Oct 09 '20 at 00:28
  • Detecting the center point is the first goal, also detecting other circles separately is what I'm trying to do. Second row shows overlapping circles which I don't want to have – gokdumano Oct 09 '20 at 00:43
  • Have you considered using an adaptive threshold? OpenCV has some implementations for it. Otherwise, I'd try spectral clustering. –  Oct 09 '20 at 03:23

1 Answers1

1

I suggest using the GRIP software to implement two image processing pipelines: One that performs thresholding to find the center point and another to find all the other circles.
These pipelines can then be generated to Python OpenCV code and you can import them into your code (which I have already done for you).

Here is the center point pipeline: It performs a simle HSV threshold followed by a find blob command
Center_Pipeline


Here is the circle pipeline:
It performs a Laplacian transform (which is a one-sided Fourier Transform to detects the contrast changes which represent the edges of the circles), then Canny edge detection to find the edges of the circles, and then finds and filters the contours of the circles. To find the number of circle, you can divide the number of contours by 2 (inner and outer circle associated with each Laplacian ring) Ring_Pipeline


Here is the link to download the GRIP software

Here is a link to all my files (including the auto-generated Python image processing pipeline)

Jacob K
  • 742
  • 6
  • 13
  • Thanks a lot, I haven't try it yet but both those pipelines and the software seem like they can help me greatly – gokdumano Oct 09 '20 at 08:39