1

I've an almost semicircular object in a grayscale imageOriginal image for which I intend to create a mask. I've tried some preprocessing + edge detections + filtering. however, there are still some irregularities Erroneous mask.

Any suggestions on how to improve the mask?

1

2 Answers 2

1

If we assume that you have a half circle or (axis aligned) half ellipse, then one approach would be to make a copy of the image, flip it vertically and then vertically stack the two images. Then try HoughCircle (assuming it is close to a circle) in Python/OpenCV.

Input:

enter image description here

import cv2
import numpy as np

# read the image as grayscale
img = cv2.imread('disk_blob.png', cv2.IMREAD_GRAYSCALE)
hh, ww = img.shape[:2]

# remove 4 px bright border at top and left
img2 = img[4:hh, 4:ww]

# make copy, flip vertically and vstack
img3 = img2.copy()
img3 = np.flipud(img3)
img3 = np.vstack((img2,img3))

# apply hough circle
circles = cv2.HoughCircles(img3, cv2.HOUGH_GRADIENT, 1, minDist=ww, param1=150, param2=10, minRadius=ww//4, maxRadius=ww//2)

# draw circle on copy of img3 in red
result = img3.copy()
result = cv2.merge([result,result,result])

for circle in circles[0]:
    # draw the circle in the output image, then draw a rectangle
    # corresponding to the center of the circle
    (x,y,r) = circle
    print("xcent:",x,"ycent:",y,"radius:",r)
    x = int(x)
    y = int(y)
    r = int(r)
    cv2.circle(result, (x, y), r, (0, 0, 255), 2)

# save results
cv2.imwrite('disk_blob_circle.png', result)

# show results
cv2.imshow('img3', img3)
cv2.imshow('result', result)
cv2.waitKey(0)

Result:

enter image description here

Circle Information:

xcent: 687.5 ycent: 848.5 radius: 491.9
Sign up to request clarification or add additional context in comments.

Comments

0

You may have heard of the Hough transform for identifying straight lines in an image. There is a variation on this idea, the Circle Hough Transform, that finds circles. It works for semicircles, too, and may be a good approach for this use case. Run denoising and edge detection first, then run the circle Hough transform on the edge image. There is a cv.HoughCircles() function in OpenCV to apply this in C++ or Python.

A limitation is that the circle Hough transform is expensive to compute. If this is a problem, it may be helpful to take a multiscale approach. First find the rough location of the semicircle on a downscaled version of the image. Then fine tune the search at full resolution.

1 Comment

It is an ellipse not a circle and OpenCV does not have Hough Ellipse. So one has to get the edge of the region and use cv2.fitEllipse. But first extend the image so that the fit will not be cut off.

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.