Detect both sun and moon in eclipse shot with OpenCV and Hough Circle Transform

28 Views Asked by At

I have a set of solar eclipse photos. I want to detect where in the image the sun and moon are. To differentiate between the sun and the moon, I got diameter data from the JPL Horizons ephemeris system. This is a ring of fire eclipse so there is some difference between the diameters.

I can detect the sun just fine. The moon I can't detect at all. What happens instead is that it just detect the sun.

Sample pic: enter image description here

The image has been cropped from the original to make it easier to see what is going on. The detection circle is blue-green because it is a blue and a green circle nearly on top of each other.

Link to full size tiff Full Size Tiff

The code as it exists now:

import numpy as np
import cv2

image = cv2.imread("./002 PIPP/IMG_5515/IMG_5515.tif",0)
output = cv2.imread("./002 PIPP/IMG_5515/IMG_5515.tif",1)

cv2.namedWindow("Original Image",cv2.WINDOW_NORMAL)
cv2.imshow("Original Image", image)
cv2.waitKey()
blurred = cv2.GaussianBlur(image,(11,11),0)
cv2.namedWindow("Blurred Image",cv2.WINDOW_NORMAL)
cv2.imshow("Blurred Image", blurred)
cv2.waitKey()
# Finds Sun circles in a grayscale image using the Hough transform
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 0.5, 100,
                             param1=100,param2=30,minRadius=80,maxRadius=82)
# cv2.HoughCircles function has a lot of parameters, so you can find more about it in documentation
# or you can use cv2.HoughCircles? in jupyter nootebook to get that 
# Check to see if there is any detection
if circles is not None:
    # If there are some detections, convert radius and x,y(center) coordinates to integer
    circles = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circles:
        print(x,y,r)
        # Draw the circle in the output image
        cv2.circle(output, (x, y), r, (0,255,0), 3)
        # Draw a rectangle(center) in the output image
        cv2.rectangle(output, (x - 2, y - 2), (x + 2, y + 2), (0,255,0), -1)
# Finds moon circles in a grayscale image using the Hough transform
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 0.5, 100,
                             param1=100,param2=30,minRadius=76,maxRadius=79)
# cv2.HoughCircles function has a lot of parameters, so you can find more about it in documentation
# or you can use cv2.HoughCircles? in jupyter nootebook to get that 
# Check to see if there is any detection
if circles is not None:
    # If there are some detections, convert radius and x,y(center) coordinates to integer
    circles = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circles:
        print(x,y,r)
        # Draw the circle in the output image
        cv2.circle(output, (x, y), r, (255,0,0), 3)
        # Draw a rectangle(center) in the output image
        cv2.rectangle(output, (x - 2, y - 2), (x + 2, y + 2), (0,255,0), -1)
cv2.namedWindow("Detections",cv2.WINDOW_NORMAL)
cv2.imshow("Detections",output)
cv2.imwrite("CirclesDetection.jpg",output)
cv2.waitKey()

What I am expecting is a circle clearly around the sun, and a different circle clearly around the moon.

I have tried some tuning of the moon circle search parameters, but that didn't really help.

0

There are 0 best solutions below