寻找北极星位置:look_for_the_edge_and_north_star
主函数代码
#%************** 此函数用于对原始图像进行边缘检测和北极星寻找
#%% 数据目录准备
clear;clc;tic;
addpath('E:\办公\研究生学习\研究方向\新的研究\代码\新研究用代码(matlab)\寻找北极星位置\self-complied-function\');
addpath('E:\办公\研究生学习\研究方向\新的研究\代码\新研究用代码(matlab)\寻找北极星位置\用霍夫曼方法检测圆、直线\');
addpath('E:\办公\研究生学习\研究方向\新的研究\代码\新研究用代码(matlab)\寻找北极星位置\数据处理完整流程\数据预处理\');
path='E:\办公\研究生学习\研究方向\新的研究\数据\测试'; #%找到文件夹路径
subpath=obtain_subfolder_path(path); #%获取当前文件夹下的子文件夹路径,存储在元组(即path{})中,第一个(即path)为母文件路径;
days=size(subpath,2); #%返回子文件列数,即返回子文件数量的列向量。-例size(X,2),返回矩阵X的列数
coordinate_information(days)=struct('circle_center',[],'circle_radii',[],'north_star',[],'adjust_angle',[],'note',[]); #%输出信息
#%% 利用循环求每个子文件夹的边缘的北极星位置
fp=figure;
for fd=6:days #%文件目录指针
if(exist(subpath{fd},'dir')~=0)
%cd(subpath{fd});
file= ls([subpath{fd},'*.png']);
end
filenum=size(file,1);
threshold=100;
if filenum<threshold
#% coordinate_information(fd).circle_center=[512,512];
#% coordinate_information(fd).circle_radii=500;
#% coordinate_information(fd).north_star=[0,0];
coordinate_information(fd).note='当天晚上的有效观测数据过少,无法确定北极星位置,北极星可靠性较低';
else
A=zeros(1024,1024);
counter=0;
for i=5:filenum-5
try
tempa=double(imread(strcat(subpath{fd},file(i,:))));
if size(find(tempa>=40000),1)<8000
A=A+tempa;
counter=counter+1;
end
catch
disp([strcat(subpath{fd},file(i,:)),'无法识别出错跳过']);
end
end
%tempA=medfilt2(A,[25,25]);
#% if counter<10
#% coordinate_information(fd).note='当天晚上的有效观测数据过少,无法确定北极星位置,北极星可靠性较低';
#% continue;
#% end
wk=ones(25,25)/625;tempA=imfilter(A,wk);
[x,y]=find(A<tempA);
for i=1:size(x,1)
if ((x(i)-512)^2+(y(i)-512)^2)<470^2
A(x(i),y(i))=tempA(x(i),y(i));
end
end
#% [g1,t1]=edge(A,'canny',[],2); #%输入数据为图像类数据
#% circle1=findcircle(g1,450,510,10,0.05,0.9); #%找圆心,问题不大,基本解决
#% if size(circle1,1)>1
#% circle1=fix(mean(circle1,1));
#% end
#% coordinate_information(fd).circle_center=[circle1(2),circle1(1)];
#% coordinate_information(fd).circle_radii=circle1(3);
[coordinate_information(fd).north_star,coordinate_information(fd).adjust_angle]=look_for_north_star(A,0.8,220,430);%北极星坐标为[x,y]结构
coordinate_information(fd).note='经过计算得出';
set(0,'currentFigure', fp);imshow(A,[mean(mean(A))/2,mean(mean(A))*2.5]);
hold on;
plot(coordinate_information(fd).north_star(1,1),coordinate_information(fd).north_star(1,2),'color','r','Marker','o','MarkerSize',8,'LineWidth',1);
ob_h=getframe;tem_path=strcat(path,[num2str(fd),'_','Lsa2015','_',subpath{fd}(end-4:end-1),'.png']);
imwrite(ob_h.cdata,tem_path);
end
end
toc;
需要调用的函数
主函数名:look_for_the_edge_and_north_star
obtain_subfolder_path
作用:获取当前文件夹下的子文件夹路径,存储在元组中,第一个(即path)为母文件路径
代码:subpath=obtain_subfolder_path(path);
位置:主函数第8行
运行代码
function [ path ] =obtain_subfolder_path( input_path )
#%指定文件夹的子文件夹路径
#%元包的第一个元素为母文件夹路径,其余为母文件夹下的子文件夹路径,如果母文件夹下不存在其他子文件夹则输出路径中只有母文件夹文件夹
#%input_path --输入文件地址,用时可用path代替这里,然后用path=' '来添加文件地址
p=genpath(input_path); #%获取当前文件夹中的子目录文件夹名称,保存为一行,且用‘;’号隔开
length_p=size(p,2); #%获取行的字符长度,即保存的子目录矩阵为一行多列,这里的2就是求的列数(1就是行数)
path={}; #%创建一个元包用来存放地址
temp=[];
index=1; #%地址数目指针(就是说地址数目为1?)
for i=1:1:length_p #%依次查询p中的地址字符串(从1到length_p长度,步长为1)
if p(i)~=';' #%‘~=’表示不等于的意思,p(i)即p字符串中的第i个字符
temp=[temp p(i)];
else
temp=[temp '\'];
path{index}=temp;
index=index+1;
temp=[];
end
end
clear index p temp;
end
代码逻辑
1、先搜索母文件下的所有子文件;
2、将其存储为1行N列的字符串,用p来表示;
3、用size(p,2)函数获取p字符串的长度,用length_p来表示,且表示为一个数;
4、然后用for循环,从1至length_p,步长为1,如果p(i)即p字符串中的第i个字符不等于’;’,则在temp中键入此字符,否则就键入‘\’。
5、然后将该值存储在path{}元包中,path{index}即指针加一,表示path中第几个字符(如index=2就是path中第二个字符),是用来循环扩充path{}的,最后path{}中的字符串可能为‘***’。
6、所以,该函数最后输出的就是path{}