1

I am trying to fill the missing pixels (as shown in image) in the circle part to make complete and clean circles. I have tried image enhancement techniques, they didn't help much. Please suggest me how to do in Matlab or provide some code to do that. Thanks in advance.

enter image description here

Rajesh
  • 221
  • 4
  • 16

2 Answers2

3

Since your tags suggest you're open to Python solutions as well, I present the following approach using OpenCV, specifically the cv2.HoughCircles method, following this tutorial.

Here's the code:

import cv2
import numpy as np

# Read input image
img = cv2.imread('images/xbHB0.jpg', cv2.IMREAD_GRAYSCALE)

# Blur input image to prevent too much false-positive detected circles
img = cv2.GaussianBlur(img, (3, 3), 0)

# Initialize outputs
clean = np.zeros(img.shape, np.uint8)
compare = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

# Detect circles using Hough transform; convert center points and radii to int
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=25, minRadius=10, maxRadius=0)
circles = np.uint16(np.around(circles))

# Draw detected circle to outputs
for i in circles[0, :]:
    cv2.circle(clean, (i[0], i[1]), i[2], 255, 1)
    cv2.circle(compare, (i[0], i[1]), i[2], (0, 255, 0), 1)
    cv2.circle(compare, (i[0], i[1]), 2, (0, 0, 255), 1)

cv2.imshow('Input', img)
cv2.imshow('Comparison', compare)
cv2.imshow('Clean output', clean)
cv2.waitKey(0)
cv2.destroyAllWindows()

The "clean" circles would look like this:

Clean circles

And, for comparison, an overlay on the original image:

Comparison

As you can see, you won't get perfect results using this method on this specific image. Parameter tuning might improve the result.

Hope that helps!

HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • Is it possible to use for loop to detect all like one, two, and three circles having their corresponding image? like as I have shown here for four? – Rajesh Jun 05 '19 at 07:40
  • @Rajesh To be honest, I don't really understand what you want to achieve. You can create separate images for each circle, of course. Just initialize an array of four black images, and in the exisiting for loop, draw the i-th circle in the i-th image. – HansHirse Jun 05 '19 at 07:45
  • Ya thanks for the reply @HansHirse! The actual problem is to detect circles and enhance their boundaries. Right now, in the question, I gave you an image which contains 4 circles and you detected using Hough transform with some parameters. Actually, I have images which contain a single circle, two circles, and three circles also. I want to detect them using single for loop. I have tried but the circles are not detecting because hough parameters need to change for every image. like that, I have a lot of images to do, so I can't set parameters manually for every image. Does any solution please? – Rajesh Jun 05 '19 at 09:23
  • @Rajesh You provided just one image in the first place. In such a case, you'll always get a limited solution. If this approach performs badly on your other images, that's most likely because they differ quite significantly from the one provided - not just in terms of number of circles!! So, provide more images, giving an impression of the "range" of possibilities, and you eventually get a more generic solution. – HansHirse Jun 05 '19 at 09:29
  • 1
    @M.D.P My suggestion is specifically for circles, thus the approach using `HoughCircles`. For irregular shapes, you could try morphological operations, but that highly depends on your actual inputs. Feel free to [ask a question](https://stackoverflow.com/questions/ask)! – HansHirse Jun 24 '19 at 04:14
  • Hi, HansHirse! Can you answer this question? https://stackoverflow.com/questions/56846801/fill-the-circular-paths-in-image – Rajesh Jul 03 '19 at 04:17
0

In case your problem is specific to circles, you can use the Hough Circles algorighm to find the circles in the image and then simply draw them. I don't know how it's done in matlab. In python you can use opencv HoughCircles

If you are looking for a more general solution, morphological operators such as dilate, erode, open, close may be of interest.

Richard K. Wade
  • 299
  • 1
  • 5