import os
from astropy.io import fits
import matplotlib.pyplot as plt
"""
这个类代表了一个矩阵图像中所有的极值点. 对图像上的每个点进行判断
"""
class FindExtremePoint():
def __init__(self,fitsdata):
self.name=fitsdata
with fits.open(fitsdata) as hdul:
data = hdul[0].data
shape = data.shape
self.data= data.reshape(shape[2], shape[3])
self.observeDate=hdul[0].header['DATE-OBS']
self.plotFits()
def judgeExtremePoint(self,row,column):
"""
为了寻找极值点,首先定义一个方法,对这个方法输入图像上的一个点的行与列,通过比较这个点的值是否为
周围八个点中的最大值来判断他是否为极大值
如果这个点是局域最大值那么这个函数返回True,否则返回False.
"""
data=self.data
centerValue=data[row,column]
centerRow=row
centerColumn=column
localValuesList=[data[centerRow, centerColumn]#将中心值以及周围的八个值放进一个列表中并从大到小排序
,data[centerRow, centerColumn - 1]
,data[centerRow - 1, centerColumn - 1]
,data[centerRow - 1, centerColumn]
,data[centerRow - 1, centerColumn + 1]
,data[centerRow, centerColumn + 1]
,data[centerRow + 1, centerColumn + 1]
,data[centerRow + 1, centerColumn]
,data[centerRow + 1, centerColumn - 1]]
sortedLocalValuesList=sorted(localValuesList,reverse=True)
return sortedLocalValuesList[0]==centerValue
def judgeEveryPoint(self):
"""对图像矩阵中的每一个点进行判断"""
data=self.data
shape=data.shape
extremeRowList=[]
extremeColumnList=[]
for row in range(1,shape[0]-1):
for column in range(1,shape[1]-1):
if self.judgeExtremePoint(row,column):
extremeRowList.append(row)
extremeColumnList.append(column)
return [extremeRowList,extremeColumnList]
def plotFits(self):
extremePoint=self.judgeEveryPoint()
fig, ax = plt.subplots(figsize=(8, 8))
ax.scatter(extremePoint[1],extremePoint[0],s=2)
ax.imshow(self.data,origin='lower',cmap='nipy_spectral_r')
ax.set_title(self.observeDate)
plt.savefig(fname=self.name+'.jpg')
plt.close()
画fits图像上的极小值点
最新推荐文章于 2022-02-03 07:44:22 发布