用程式找出被掩星的亮度
Code is here
### python code by cG #####
import cv2
import numpy as np
from astropy.io import fits
import csv
from PIL import Image
import rawpy
import imageio
# Load the FITS image
hdulist = fits.open('Light_V450_Scuti_5.0s_Bin2_533MC_gain360_20230505-032425_-5.0C_0041-RGB-session_1-cal.fits')
image_data = hdulist[0].data
# Define the coordinates of the region to crop
x1, y1 = 628, 691
x2, y2 = 1025, 1036
# Crop the image
image_data = image_data[y1:y2, x1:x2]
# Convert the image data into a format that OpenCV can understand
image_8bit = cv2.convertScaleAbs(image_data, alpha=(255.0/65535.0)) # assuming 16-bit FITS image
# Use OpenCV to detect the stars in the image
# You might need to adjust the parameters of the HoughCircles function to suit your specific needs
circles = cv2.HoughCircles(image_8bit, cv2.HOUGH_GRADIENT, dp=1, minDist=10, param1=20, param2=4, minRadius=1, maxRadius=7)
j=0
# Initialize the CSV writer
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Star ID", "Center (x, y)", "Radius", "FWHM Intensity"])
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1]) # Center of the circle
radius = i[2] # Radius of the circle
# Draw the circle on the image in red
cv2.circle(image_8bit, center, radius, (0, 0, 225), 2)
# Create a mask for the star
mask = np.zeros_like(image_data, dtype=np.uint8)
cv2.circle(mask, center, radius, 1, thickness=-1)
masked_star = cv2.bitwise_and(image_data, image_data, mask=mask)
# Calculate the median intensity of the star
median_intensity = np.median(masked_star[masked_star > 0])
# Calculate the FWHM intensity
fwhm_intensity = median_intensity * 0.5
# Write the star profile and FWHM intensity to the CSV file
writer.writerow([i, center, radius, fwhm_intensity])
j=j+1
print('total ',j, 'stars')
# Show the image with the detected circles
cv2.imshow('Detected Circles', image_8bit)
cv2.waitKey(0) # Wait for a key press before moving on to the next image
cv2.destroyAllWindows() # Close the image window
#### end code ####### |