I want to extract two circles and want to find out this circle is concentric or not.
I made some code by referring some sites and chatGPT, however, there is limitation.
I want to draw the 'CIRCLE', not an 'Ellipse'.
Below is the picture (it is the ring shape red laser beam) and my algorithm.
import cv2
import numpy as np
# Load the Image
img = cv2.imread("covered_2.jpg", cv2.IMREAD_GRAYSCALE)
# Blur the Image
img_blur = cv2.GaussianBlur(img, (5, 5), 0)
# Image Binerize
_, img_thresh = cv2.threshold(img_blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Set the kernal size for closing
kernel = np.ones((15, 15), np.uint8)
#Closing the Image
img_close = cv2.morphologyEx(img_thresh, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(img_close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
max_contour = max(contours, key=cv2.contourArea)
# Extract the outer ellipse
ellipse = cv2.fitEllipse(max_contour)
center, axes, angle = ellipse
major_axis, minor_axis = axes
img_ellipse = np.zeros_like(img)
cv2.ellipse(img_ellipse, ellipse, 255, 2)
# Finding the end points of the major axis(?)
cos_angle = np.cos(np.deg2rad(angle))
sin_angle = np.sin(np.deg2rad(angle))
x1 = int(center[0] - 0.5 * major_axis * sin_angle)
y1 = int(center[1] + 0.5 * major_axis * cos_angle)
x2 = int(center[0] + 0.5 * major_axis * sin_angle)
y2 = int(center[1] - 0.5 * major_axis * cos_angle)
# Finding the end points of the minor axis(?)
x3 = int(center[0] - 0.5 * minor_axis * cos_angle)
y3 = int(center[1] - 0.5 * minor_axis * sin_angle)
x4 = int(center[0] + 0.5 * minor_axis * cos_angle)
y4 = int(center[1] + 0.5 * minor_axis * sin_angle)
# Draw lines on the new image
img_lines = np.zeros_like(img)
cv2.line(img_lines, (x1, y1), (x2, y2), 255, 2)
cv2.line(img_lines, (x3, y3), (x4, y4), 255, 2)
cv2.line(img, (x1, y1), (x2, y2), 255, 2)
cv2.line(img, (x3, y3), (x4, y4), 255, 2)
cv2.ellipse(img, ellipse, 255, 2)
# Show image in the new windows
cv2.imshow('Input Image', img)
cv2.imshow('Binary Image', img_thresh)
cv2.imshow('Closed Image', img_close)
cv2.imshow('Fitted Ellipse', img_ellipse)
cv2.imshow('Extracted Lines', img_lines)
# Save the Image
cv2.imwrite('edge_detect_circle.jpg', img)
cv2.imwrite('edge_detect_circle_close.jpg', img_close)
cv2.imwrite('edge_detect_ellipse_close.jpg', img_ellipse)
cv2.imwrite('edge_detect_lines_close.jpg', img_lines)
cv2.waitKey()
cv2.destroyAllWindows()
The final goal is compensate this speckled image and draw the 2 circles and figure out they are concentric or not.
Original Image is this: ring shape laser beam, speckled
I tried to designate some area and make its color inverted to find the inner ellipse. However, It did not work.
Additionally, I tried some Hough Circle transformation, but it also did not work.
As the last method, I used Canny edge detection to find out the center of the drawn line, which didn’t work either.