def get_length_from_mediatrix(image_name,image_dir='', method="medium",alpha=1,near_distance=(sqrt(2)/2)): """ Function to calculate length of an object using the Mediatrix Decomposition. Input: - image_name <array> : the image file name. - method <string> : possible values are 'medium' or 'brightest'. - alpha <float> : the factor alpha=l_i/w. - near_distance <float> : the distance to consider a point near to the perpendicular bisector. Output: - <Float> : the object length """ image,hdr = getdata(image_dir+image_name, header = True ) pixels=where(image>0) E1,E2=get_extrema_2loops( pixels[0], pixels[1], 0 ) Area=len(pixels[1]) p1=[pixels[0][E1],pixels[1][E1]] p2=[pixels[0][E2],pixels[1][E2]] keypoints=[p1,p2] keypoints=find_keydots(p1,p2,pixels,image,keypoints,Area, method="brightest",alpha=alpha,near_distance=near_distance) L_f=get_length(keypoints) W=width_ellipse(L_f,Area) return L_f, W
def choose_near_point(theta,c,object_pixels,image,method,near_distance): """ This function choose the Mediatrix points from a perpendicular bisector parameters in an object. Input: - theta <float> : straight line angle with x axis. - c <float> : linear coeficient the line. - object_pixels <list> : list of object's points coordidates. - image_name <array> : the image matrix. - method <string> : possible values are 'medium' or 'brightest'. - near_distance <float> : the distance to consider a point near to the perpendicular bisector. Output: - <float> : the chosen point x coordinate. - <float> : the chosen point y coordinate. - <int> : a Flag error. if Flag=1, it was not possible to choose a point. """ nearXs= [] nearYs= [] for i in range(0,len(object_pixels[1])): pixel=[object_pixels[0][i],object_pixels[1][i]] D=get_distance_from_line_to_point(pixel,theta,c) if(near_distance>=D): nearXs.append(object_pixels[0][i]) nearYs.append(object_pixels[1][i]) if (len(nearXs)<1): FlagErr=1 chosenX=0 chosenY=0 return chosenX, chosenY, FlagErr if method=='brightest': chosenX=nearXs.pop() chosenY=nearYs.pop() FlagErr=0 while len(nearXs)>=1: chosenAuxX=nearXs.pop() chosenAuxY=nearYs.pop() if (image[chosenX][chosenY])<(image[chosenAuxX][chosenAuxY]): chosenX=chosenAuxX chosenY=chosenAuxY elif method=='medium': i,j=get_extrema_2loops( nearX, nearY, 0 ) chosenX=float(nearX[i]+nearX[j])/2. chosenY=float(nearY[i]+nearY[j])/2. FlagErr=0 else: FlagErr=1 chosenX=0 chosenY=0 return chosenX,chosenY,FlagErr
def print_mediatrix_Object_circle_graph_old(mediatrix_data,image_dir='', keydots=False, colors= {'object': "g", 'vector': "b", 'keydots': "k"}, mediatrix_vectors=False, save=True, save_dir=''): """ Make a plot presenting the object, keydots and mediatrix vectors. Input: - mediatrix_data <list> : the output from mediatrix_decomposition. - image_dir <str> : the image directory. If it is on the same directory directory=''. - keydots <bool> : 'True' if you want to display the keydots and 'False' if you do not. - colors <dic> : set the plot colors. The possible keys are 'object', 'vector' and 'keydots'. Output: <bool> """ image_name=mediatrix_data['id'] image,hdr = getdata(image_dir+image_name, header = True ) pixels=where(image>0) A = subplot(111) for i in range (0,len(pixels[0])): xy=[pixels[1][i]-0.5,pixels[0][i]-0.5] rec=Rectangle(xy, 1, 1, ec=colors['object'], fc=colors['object'], zorder=100) A.add_patch(rec) #A.scatter(pixels[1], pixels[0], s=200, c='b', marker='s', edgecolors='none') #A.plot(pixels[1],pixels[0],colors['object']) Length=0 for i in range(0,len(mediatrix_data['origin'])): origin_x=mediatrix_data['origin'][i][0] origin_y=mediatrix_data['origin'][i][1] end_x=mediatrix_data['end'][i][0] end_y=mediatrix_data['end'][i][1] Length_aux=(origin_x - end_x)**2 + (origin_y - end_y)**2 Length=Length+ sqrt(Length_aux) if mediatrix_vectors==True: d_x= end_x - origin_x d_y= mediatrix_data['end'][i][1] - mediatrix_data['origin'][i][1] arr = Arrow(origin_y, origin_x, d_y, d_x, width=0.05*Length, fc=colors['vector'], ec='none',zorder=1000) A.add_patch(arr) if keydots==True: E1,E2=get_extrema_2loops(pixels[0], pixels[1], 0 ) Area=len(pixels[1]) p1=[pixels[0][E1],pixels[1][E1]] # the extreme points p_1 and p_2 p2=[pixels[0][E2],pixels[1][E2]] keydots=[p1,p2] keydots=find_keydots(p1,p2,pixels,image,keydots,Area, method="brightest",alpha=1) keyX=[] keyY=[] for j in range(0,len(keydots)): keyX.append(keydots[j][0]) keyY.append(keydots[j][1]) A.plot(keyY,keyX,colors['keydots']+'.',zorder=500)
def mediatrix_decomposition_on_matrix_c(image, method="medium",alpha=1,near_distance=(sqrt(2)/2), max_level=1000): """ Function to perform the mediatrix decomposition method on a given object. Input: - image_name <str> : the image file name. - image_dir <str> : the image directory. If it is on the same directory, directory=''. - method <string> : possible values are 'medium' or 'brightest'. - alpha <float> : the factor alpha=l_i/w to stop the bisection. - near_distance <float> : the distance to consider a point near to the perpendicular bisector. Output: - <dic> : Dictionary structure. Each list item is a dictionary with information of corresponding to a mediatrix vector. The keys are 'theta' for the angle with x axis, 'linear_coefficient' for the linear coefficient from the line in the vector direction, 'origin' the point (x,y) of the vector origin, 'end' the point (x,y) of the vector, 'modulus' for vector modulus. The first item from the list has two extra keys 'id' wich contains the image_name and 'center' that keeps the objet center defined by the first mediatrix point in the first mediatrix level. """ #image,hdr = getdata(image_dir+image_name, header = True ) pixels=where(image>0) E1,E2=get_extrema_2loops(pixels[0], pixels[1], 0 ) Area=len(pixels[1]) p1=pixels[0][E1]+pixels[1][E1]*1j # the extreme points p_1 and p_2 p2=pixels[0][E2]+pixels[1][E2]*1j keydots=[p1,p2] keydots=find_keydots_c(p1,p2,pixels,image,keydots,Area, method=method,alpha=alpha,near_distance=near_distance,max_level=max_level,level=0) #print keydots mediatrix_vectors=find_mediatrix_vectors_c(keydots) #mediatrix_vectors['id']=image_name medium=int(float(len(keydots))/2) mediatrix_vectors['center']=keydots[medium] L=get_length_c(keydots) W=(len(pixels[0]))/(atan(1)*L) mediatrix_vectors['L/W']=L/W mediatrix_vectors['L']=L #x=[pixels[0][E1],mediatrix_vectors['center'].real,pixels[0][E2]] #y=[pixels[1][E1],mediatrix_vectors['center'].imag,pixels[1][E2]] p1_vec=[pixels[0][E1],pixels[1][E1]] # the extreme points p_1 and p_2 p2_vec=[pixels[0][E2],pixels[1][E2]] p3_vec=[mediatrix_vectors['center'].real,mediatrix_vectors['center'].imag] x_c,y_c,r=three_points_to_circle(p1_vec,p3_vec,p2_vec) circle_center=x_c+y_c*1j mediatrix_vectors['circle_params']=[circle_center,p1,p2] return mediatrix_vectors
def print_mediatrix_Object_graph_old(mediatrix_data,image_dir='', keydots=False, colors= {'object': "g", 'vector': "b", 'keydots': "k"}, save=True, save_dir=''): """ Make a plot presenting the object, keydots and mediatrix vectors. Input: - mediatrix_data <list> : the output from mediatrix_decomposition. - image_dir <str> : the image directory. If it is on the same directory, directory=''. - keydots <bool> : 'True' if you want to display the keydots and 'False' if you do not. - colors <dic> : set the plot colors. The possible keys are 'object', 'vector' and 'keydots'. Output: <bool> """ image_name=mediatrix_data['id'] image,hdr = getdata(image_dir+image_name, header = True ) pixels=where(image>0) A = subplot(111) for i in range (0,len(pixels[0])): xy=[pixels[1][i]-0.5,pixels[0][i]-0.5] rec=Rectangle(xy, 1, 1, ec=colors['object'], fc=colors['object'], zorder=100) A.add_patch(rec) #A.scatter(pixels[1], pixels[0], s=200, c='b', marker='s', edgecolors='none') #A.plot(pixels[1],pixels[0],colors['object']) Length=0 if keydots==True: E1,E2=get_extrema_2loops(pixels[0], pixels[1], 0 ) Area=len(pixels[1]) p1=[pixels[0][E1],pixels[1][E1]] # the extreme points p_1 and p_2 p2=[pixels[0][E2],pixels[1][E2]] keydots=[p1,p2] keydots=find_keydots(p1,p2,pixels,image,keydots,Area, method="brightest",alpha=1) keyX=[] keyY=[] for j in range(0,len(keydots)): keyX.append(keydots[j][0]) keyY.append(keydots[j][1]) A.plot(keyY,keyX,colors['keydots']+'.',zorder=500) #A.scatter(keyY, keyX, s=20, c='b', marker='s') for i in range(0,len(mediatrix_data['origin'])): origin_x=mediatrix_data['origin'][i][0] origin_y=mediatrix_data['origin'][i][1] end_x=mediatrix_data['end'][i][0] end_y=mediatrix_data['end'][i][1] Length_aux=(origin_x - end_x)**2 + (origin_y - end_y)**2 Length=Length+ sqrt(Length_aux) d_x= end_x - origin_x d_y= mediatrix_data['end'][i][1] - mediatrix_data['origin'][i][1] arr = Arrow(origin_y, origin_x, d_y, d_x, width=0.05*Length, fc=colors['vector'], ec='none',zorder=1000) A.add_patch(arr) xmin, xmax = xlim() ymin, ymax = ylim() min_inc_axis=40 #x_axis_length=(xmax+1*Length)-(xmin-1*Length) #y_axis_length=(ymax+1*Length)-(ymin-1*Length) #if x_axis_length<min_inc_axis A.axis("equal") A.set_xlim(xmin-1*Length,xmax+1*Length) A.set_ylim(ymin-1*Length,ymax+1*Length) ylabel("Y") xlabel("X") #A.axis("equal") title("Mediatrix Decomposition applied") if save==True: savefig(save_dir+image_name+"_mediatrixGraph.png") A.clear() return True else: return A
def plot_mediatrix_circle(mediatrix_data,ps_name, keydots=False, colors= {'object': "g", 'vector': "b", 'keydots': "k"}, mediatrix_vectors=False, save=True, plot_title="Mediatrix Plot", out_image=""): """ Make a plot presenting the object, keydots and mediatrix vectors. Input: - mediatrix_data <list> : the output from mediatrix_decomposition. - image_dir <str> : the image directory. If it is on the same directory directory=''. - keydots <bool> : 'True' if you want to display the keydots and 'False' if you do not. - colors <dic> : set the plot colors. The possible keys are 'object', 'vector' and 'keydots'. Output: <bool> """ if out_image=='': out_image=ps_name.replace(".fits","")+"_mediatrix_circle.png" image,hdr = getdata(ps_name, header = True ) pixels=where(image>0) A = subplot(111) for i in range (0,len(pixels[0])): xy=[pixels[1][i]-0.5,pixels[0][i]-0.5] rec=Rectangle(xy, 1, 1, ec=colors['object'], fc=colors['object'], zorder=100) A.add_patch(rec) #A.scatter(pixels[1], pixels[0], s=200, c='b', marker='s', edgecolors='none') #A.plot(pixels[1],pixels[0],colors['object']) Length=0 for i in range(0,len(mediatrix_data['origin'])): origin_x=mediatrix_data['origin'][i].real origin_y=mediatrix_data['origin'][i].imag end_x=mediatrix_data['end'][i].real end_y=mediatrix_data['end'][i].imag Length_aux=(origin_x - end_x)**2 + (origin_y - end_y)**2 Length=Length+ sqrt(Length_aux) if mediatrix_vectors==True: d_x= end_x - origin_x d_y= mediatrix_data['end'][i].imag - mediatrix_data['origin'][i].imag arr = Arrow(origin_y, origin_x, d_y, d_x, width=0.05*Length, fc=colors['vector'], ec='none',zorder=1000) A.add_patch(arr) if keydots==True: E1,E2=get_extrema_2loops(pixels[0], pixels[1], 0 ) Area=len(pixels[1]) p1=pixels[0][E1]+ pixels[1][E1]*1j # the extreme points p_1 and p_2 p2=pixels[0][E2]+ pixels[1][E2]*1j keydots=[p1,p2] keydots=find_keydots_c(p1,p2,pixels,image,keydots,Area, method="brightest",alpha=1) keyX=[] keyY=[] for j in range(0,len(keydots)): keyX.append(keydots[j].real) keyY.append(keydots[j].imag) A.plot(keyY,keyX,colors['keydots']+'.',zorder=500) #A.scatter(keyY, keyX, s=20, c='b', marker='s') last=len(mediatrix_data['origin'])-1 x=[pixels[0][E1],mediatrix_data['center'].real,pixels[0][E2]] y=[pixels[1][E1],mediatrix_data['center'].imag,pixels[1][E2]] p1_vec=[pixels[0][E1],pixels[1][E1]] # the extreme points p_1 and p_2 p2_vec=[pixels[0][E2],pixels[1][E2]] p3_vec=[mediatrix_vectors['center'].real,mediatrix_vectors['center'].imag] x_c,y_c,r=three_points_to_circle(p1_vec,p3_vec,p2_vec) if r>0: xy=[y_c,x_c] cir=Circle(xy,r,fc='none',ec='m', zorder=501) A.add_patch(cir) else: print "impossible to define a circle " xmin, xmax = xlim() ymin, ymax = ylim() min_inc_axis=40 #x_axis_length=(xmax+1*Length)-(xmin-1*Length) #y_axis_length=(ymax+1*Length)-(ymin-1*Length) #if x_axis_length<min_inc_axis A.axis("equal") A.set_xlim(xmin-1*Length,xmax+1*Length) A.set_ylim(ymin-1*Length,ymax+1*Length) ylabel("Y") xlabel("X") #A.axis("equal") title(plot_title) if save==True and r>0: savefig(out_image) A.clear() return True else: return A
def plot_mediatrixapl(image_name,_id='', keydots=False,circle=True, save=True, out_image='', args={}): """ Make a plot presenting the object, keydots and mediatrix vectors. If the input image name is not a postage stamp the code will read from the sextractor segmentation image the object position with given id and pixels intensity from sextractor objects image. The function assumes that the segmentation and objects images names are *original_image_name*_seg.fits and *original_image_name*_obj.fits respectively. Input: - mediatrix_data <list> : the output from mediatrix_decomposition_on_matrix. - image_dir <str> : the image directory. If it is on the same directory, directory=''. - keydots <bool> : 'True' if you want to display the keydots and 'False' if you do not. - colors <dic> : set the plot colors. The possible keys are 'object', 'vector' and 'keydots'. - type <str> : cutout or object Output: <bool> """ opt={'increase': 2, 'relative_increase': True,'connected': False,'object_centered':True, 'out_type':'cutout', 'vmin':0 , 'invert':True ,'out_title': 'Mediatrix Decomposition', 'keys_color': "r" ,'alpha': 1 ,'max_level': 1000, 'near_distance': sqrt(2)/2, 'max_level': 1000, 'method':"brightest"} opt.update(args) #image_seg_hdu=pyfits.open(image_segname) #image_obj_hdu=pyfits.open(image_objname) if opt['out_type']=='cutout': opt['object_centered']=False #else: # opt['object_centered']=True type_arg=type(image_name) is str if type_arg: if out_image=='': out_image=image_name.replace(".fits","")+"_mediatrix_plot.png" if _id=='': image_ps,hdr=getdata(image_name, header = True ) else: image_segname=image_name.replace(".fits","")+"_seg.fits" image_objname=image_name.replace(".fits","")+"_obj.fits" image_seg,hdr = getdata(image_segname, header = True ) image_obj,hdr = getdata(image_objname, header = True ) image_ps,hdr=imcp.segstamp(segimg=image_seg, objID=_id, objimg=image_obj, hdr=hdr, increase=opt['increase'], relative_increase=opt['relative_increase'], connected=opt['connected'], obj_centered=opt['object_centered']) else: image_ps=image_name.copy() if out_image=='': time_id=time.time() out_image=str(time_id)+"_mediatrix_plot.png" #image_ps,hdr=imcp.segstamp(segimg=image_seg, objID=_ids[i], objimg=image_obj, hdr=hdr, increase=2, relative_increase=True, connected=False, obj_centered=True) mediatrix_data=mediatrix_decomposition_on_matrix_c(image_ps, method=opt['method'],alpha=opt['alpha'],near_distance=opt['near_distance'],max_level=opt['max_level']) if opt['out_type']=='cutout': img,hdr=getdata(image_name, header = True ) else: img=image_ps #print "depois" #for j in range(0,len(img[0])): # print "\n" # for i in range(0,len(img[1])): # print img[j][i] IDtime=str(time.time()) #pyfits.writeto(ID+".test.fits",img.astype(float),header=None) pixels=where(image_ps>0) FitsPlot = aplpy.FITSFigure(img) smallest = numpy.amin(img) biggest = numpy.amax(img) #if opt['vmax']=='Max': opt['vmax']=biggest #for i in range(0,100): # print opt['invert'] # FitsPlot.show_grayscale(pmin=i*0.01, pmax=1,invert=False) # FitsPlot.save("mediatrix_aplpy_withcuts/"+ID+"scaleMin"+str(i*0.01)+"Max"+str(1)+".png") FitsPlot.show_grayscale(vmin=opt['vmin'], vmax=opt['vmax'],invert=opt['invert']) #print biggest FitsPlot.save("mediatrix_aplpy_withcuts/"+IDtime+"scaleMin"+str(0)+"Max"+str(biggest)+".png") Length=0 if keydots==True: E1,E2=get_extrema_2loops(pixels[0], pixels[1], 0 ) Area=len(pixels[1]) p1=pixels[0][E1]+ pixels[1][E1]*1j # the extreme points p_1 and p_2 p2=pixels[0][E2]+ pixels[1][E2]*1j keydots=[p1,p2] keydots=find_keydots_c(p1,p2,pixels,image_ps,keydots,Area, method=opt['method'],alpha=opt['alpha'],near_distance=opt['near_distance'],max_level=opt['max_level']) keyX=[] keyY=[] for j in range(0,len(keydots)): keyX.append(keydots[j].real) keyY.append(keydots[j].imag) FitsPlot.show_markers(keyY,keyX,c=opt['keys_color'],marker='.',zorder=500) if circle==True: if keydots==False: E1,E2=get_extrema_2loops(pixels[0], pixels[1], 0 ) x=[pixels[0][E1],mediatrix_data['center'].real,pixels[0][E2]] y=[pixels[1][E1],mediatrix_data['center'].imag,pixels[1][E2]] FitsPlot.show_markers([mediatrix_data['center'].imag],[mediatrix_data['center'].real],c='g',marker='D',zorder=500) #print "as coordenadas sao y, x" #print mediatrix_data['center'].imag #print mediatrix_data['center'].real p1_vec=[pixels[0][E1],pixels[1][E1]] # the extreme points p_1 and p_2 p2_vec=[pixels[0][E2],pixels[1][E2]] p3_vec=[mediatrix_data['center'].real,mediatrix_data['center'].imag] x_c,y_c,r=three_points_to_circle(p1_vec,p3_vec,p2_vec) if r>0: xy=[y_c,x_c] FitsPlot.show_circles(y_c, x_c, r, layer=False, zorder=499) else: print "impossible to define a circle " #A.scatter(keyY, keyX, s=20, c='b', marker='s') for i in range(0,len(mediatrix_data['origin'])): origin_x=mediatrix_data['origin'][i].real origin_y=mediatrix_data['origin'][i].imag end_x=mediatrix_data['end'][i].real end_y=mediatrix_data['end'][i].imag Length_aux=(origin_x - end_x)**2 + (origin_y - end_y)**2 Length=Length+ sqrt(Length_aux) d_x= end_x - origin_x d_y= mediatrix_data['end'][i].imag - mediatrix_data['origin'][i].imag #arr = Arrow(origin_y, origin_x, d_y, d_x, width=0.05*Length, fc=colors['vector'], ec='none',zorder=1000) # print "vectors" # print origin_x # print origin_y FitsPlot.show_arrows(origin_y, origin_x, d_y, d_x,zorder=502 ) #xmin, xmax = xlim() #ymin, ymax = ylim() #min_inc_axis=40 #x_axis_length=(xmax+1*Length)-(xmin-1*Length) #y_axis_length=(ymax+1*Length)-(ymin-1*Length) #if x_axis_length<min_inc_axis #A.axis("equal") #A.set_xlim(xmin-1*Length,xmax+1*Length) #A.set_ylim(ymin-1*Length,ymax+1*Length) #ylabel("Y") #xlabel("X") #A.axis("equal") #title(out_title) if save==True: FitsPlot.save(out_image) return True else: return FitsPlot, mediatrix_data, image_ps
def plot_mediatrix(mediatrix_data,image_name,_id, keydots=False, colors= {'object': "g", 'vector': "b", 'keydots': "k"}, out_title="Mediatrix Decompostion", save=True, out_image=''): """ Make a plot presenting the object, keydots and mediatrix vectors. Input: - mediatrix_data <list> : the output from mediatrix_decomposition_on_matrix. - image_dir <str> : the image directory. If it is on the same directory, directory=''. - keydots <bool> : 'True' if you want to display the keydots and 'False' if you do not. - colors <dic> : set the plot colors. The possible keys are 'object', 'vector' and 'keydots'. Output: <bool> """ if out_image=='': out_image=image_name.replace(".fits","")+"_mediatrix_plot.png" image_segname=image_name.replace(".fits","")+"_seg.fits" image_objname=image_name.replace(".fits","")+"_obj.fits" image_seg,hdr = getdata(image_segname, header = True ) image_obj,hdr = getdata(image_objname, header = True ) image_ps,hdr=imcp.segstamp(segimg=image_seg, objID=_id, objimg=image_obj, hdr=hdr, increase=2, relative_increase=True, connected=False) pixels=where(image_ps>0) A = subplot(111) for i in range (0,len(pixels[0])): xy=[pixels[1][i]-0.5,pixels[0][i]-0.5] rec=Rectangle(xy, 1, 1, ec=colors['object'], fc=colors['object'], zorder=100) A.add_patch(rec) #A.scatter(pixels[1], pixels[0], s=200, c='b', marker='s', edgecolors='none') #A.plot(pixels[1],pixels[0],colors['object']) Length=0 if keydots==True: E1,E2=get_extrema_2loops(pixels[0], pixels[1], 0 ) Area=len(pixels[1]) p1=pixels[0][E1]+ pixels[1][E1]*1j # the extreme points p_1 and p_2 p2=pixels[0][E2]+ pixels[1][E2]*1j keydots=[p1,p2] keydots=find_keydots_c(p1,p2,pixels,image_ps,keydots,Area, method="brightest",alpha=1) keyX=[] keyY=[] for j in range(0,len(keydots)): keyX.append(keydots[j].real) keyY.append(keydots[j].imag) A.plot(keyY,keyX,colors['keydots']+'.',zorder=500) #A.scatter(keyY, keyX, s=20, c='b', marker='s') for i in range(0,len(mediatrix_data['origin'])): origin_x=mediatrix_data['origin'][i].real origin_y=mediatrix_data['origin'][i].imag end_x=mediatrix_data['end'][i].real end_y=mediatrix_data['end'][i].imag Length_aux=(origin_x - end_x)**2 + (origin_y - end_y)**2 Length=Length+ sqrt(Length_aux) d_x= end_x - origin_x d_y= mediatrix_data['end'][i].imag - mediatrix_data['origin'][i].imag arr = Arrow(origin_y, origin_x, d_y, d_x, width=0.05*Length, fc=colors['vector'], ec='none',zorder=1000) A.add_patch(arr) xmin, xmax = xlim() ymin, ymax = ylim() min_inc_axis=40 #x_axis_length=(xmax+1*Length)-(xmin-1*Length) #y_axis_length=(ymax+1*Length)-(ymin-1*Length) #if x_axis_length<min_inc_axis A.axis("equal") A.set_xlim(xmin-1*Length,xmax+1*Length) A.set_ylim(ymin-1*Length,ymax+1*Length) ylabel("Y") xlabel("X") #A.axis("equal") title(out_title) if save==True: savefig(out_image) A.clear() return True else: return A
def Evaluate_S_Statistic_on_matrix_c(mediatrix_data,obj_stamp, sigma_out=True,sigma=1,sigma_pre=0.5,Area_out=True): """ Function to calculate the S estatistic measurements. Input: - mediatrix_data <list> : a list of dictionary structure. Each list item is a dictionary with information corresponding to a mediatrix vector. The keys are 'theta' for the angle with x axis, 'linear_coefficient' for the linear coefficient from the line in the vector direction, 'origin' the point (x,y) of the vector origin, 'end' the point (x,y) of the vector, 'modulus' for vector modulus. The first item from the list has an extra key 'id' wich contains the image file name. It is the output from Mediatrix_Decomposition. Output: - <dic> : dictionary with the S statistic measurements. The keys are """ guess=[mediatrix_data['center'].real,mediatrix_data['center'].imag] L=0 theta=[] linear=[] L=mediatrix_data['L'] #print " E o L eh" #print L for i in range(0,len(mediatrix_data['origin'])): origin=mediatrix_data['origin'][i] end=mediatrix_data['end'][i] delta=end-origin if delta.real!=0: a=float((delta.imag ))/delta.real theta.append(atan(a)) b=end.imag-a*(end.real) linear.append(b) else: theta.append(2*atan(1)) linear.append(end.imag-origin.imag) minM_r=fmin(M_function,guess,args=(theta,linear),maxiter=1000, disp=0) minM_val=M_function(minM_r,theta,linear) minM=minM_r[0]+minM_r[1]*1j R=abs(mediatrix_data['center']-minM) #print "esse e o ponto em que m eh minima" #print minM circle_center=mediatrix_data['circle_params'][0] #print "Este e o centro do circulo" #print circle_center center_comparison=abs(circle_center-minM) alpha=Find_angle_from_circle_section_c(mediatrix_data['circle_params'][0],mediatrix_data['circle_params'][1],mediatrix_data['circle_params'][2]) S_output={'MinM_pos': minM} try: S_output['MinM_norm']=minM_val/(L*L) except: print " Impossible to define a lenght "#for "+str(mediatrix_data['id']) S_output['MinM_norm']=-1 if R!=0: S_output['L/R']=L/R else: print "Impossible to define a radius and curvature"# for "+str(mediatrix_data['id']) S_output['L/R']=-1 if R!=0 and alpha!=0: S_output['curvature_comparison']=(L/R)/alpha else: print "Impossible to compare curvature from circle section and mediatrix S"# for "+str(mediatrix_data['id']) S_output['curvature_comparison']=-1 try: S_output['center_comparison']=center_comparison/L except: print "Impossible to compare center from circle method and mediatrix S"# for "+str(mediatrix_data['id']) S_output['curvature_comparison']=-1 if sigma_out==True: search=np.array([]) rc_center=minM sigma_points=[] step=sigma_pre lim=step sigma_points=find_rc_region_c(sigma_points,rc_center,step,lim=lim,theta=theta,linear=linear,minM=minM_val,sigma=sigma) sigma_points=np.array(sigma_points) sigma_points=np.unique(np.round(sigma_points.real,0)+np.round(sigma_points.imag,0)*1j) E1,E2=get_extrema_2loops(sigma_points.real, sigma_points.imag, 0 ) Extreme=[sigma_points[E1],sigma_points[E2]] sigma_lenght=get_length_c(Extreme) try: S_output['sigma_lenght']=(sigma_lenght)/L except: print "Impossible to define sigma_lenght for "+str(mediatrix_data['id']) sigma_minor_axis=len(sigma_points)/(4*atan(1)*sigma_lenght) #verificar essa eq. #print "Area de sigma, complex" #print sigma_points try: S_output['sigma_exc']=(sigma_lenght)/sigma_minor_axis except: print "Impossible to define sigma_exc for "+str(mediatrix_data['id']) if Area_out==True: pixels=np.where(obj_stamp>0) pixels_c=np.array(pixels[0]+pixels[1]*1j) intersection_points=np.intersect1d(pixels_c,sigma_points) S_output['intersection']=len(intersection_points) return S_output
def Evaluate_S_Statistic_on_matrix(mediatrix_data,obj_stamp, sigma_out=True,sigma=1,sigma_pre=0.5,Area_out=True): """ Function to calculate the S estatistic measurements. Input: - mediatrix_data <list> : a list of dictionary structure. Each list item is a dictionary with information corresponding to a mediatrix vector. The keys are 'theta' for the angle with x axis, 'linear_coefficient' for the linear coefficient from the line in the vector direction, 'origin' the point (x,y) of the vector origin, 'end' the point (x,y) of the vector, 'modulus' for vector modulus. The first item from the list has an extra key 'id' wich contains the image file name. It is the output from Mediatrix_Decomposition. Output: - <dic> : dictionary with the S statistic measurements. The keys are """ guess=mediatrix_data['center'] L=0 theta=[] linear=[] for i in range(0,len(mediatrix_data['origin'])): origin_x=mediatrix_data['origin'][i][0] origin_y=mediatrix_data['origin'][i][1] end_x=mediatrix_data['end'][i][0] end_y=mediatrix_data['end'][i][1] Length_aux=(origin_x - end_x)**2 + (origin_y - end_y)**2 L=L+ sqrt(Length_aux) delta_x=float((end_x-origin_x )) if delta_x!=0: a=float((end_y-origin_y ))/delta_x theta.append(atan(a)) b=end_y-a*(end_x) linear.append(b) else: theta.append(2*atan(1)) linear.append(end_y-origin_y) MinM=fmin(M_function,guess,args=(theta,linear),maxiter=1000, disp=0) MinM_val=M_function(MinM,theta,linear) R=(guess[0]-MinM[0])**2 + (guess[1]-MinM[1])**2 R=sqrt(R) #print "R real" #print R center_comparison=(mediatrix_data['circle_params'][0][0]-MinM[0])**2 + (mediatrix_data['circle_params'][0][1]-MinM[1])**2 center_comparison=sqrt(center_comparison) alpha=Find_angle_from_circle_section(mediatrix_data['circle_params'][0],mediatrix_data['circle_params'][1],mediatrix_data['circle_params'][2]) S_output={'id': mediatrix_data['id']} try: S_output['MinM_norm']=MinM_val/(L*L) except: print " Impossible to define a lenght for "+str(mediatrix_data['id']) S_output['MinM_norm']=-1 try: S_output['L/R']=L/R except: print "Impossible to define a radius and curvature for "+str(mediatrix_data['id']) S_output['L/R']=-1 try: S_output['curvature_comparison']=(L/R)/alpha except: print "Impossible to compare curvature from circle section and mediatrix S for "+str(mediatrix_data['id']) S_output['curvature_comparison']=-1 try: S_output['center_comparison']=center_comparison/L except: print "Impossible to compare center from circle method and mediatrix S for "+str(mediatrix_data['id']) S_output['curvature_comparison']=-1 if sigma_out==True: lim=round((L+2.)/2.,2) sigma_X=[] sigma_Y=[] X_search=arange(round(MinM[0],2)-lim, round(MinM[0],2)+lim,sigma_pre*1) Y_search=arange(round(MinM[1],2)-lim, round(MinM[1],2)+lim,sigma_pre*1) #print "Real lenght for sigma search x and y" #print len(X_search) #print len(Y_search) for i in range(0,len(X_search)): for j in range(0,len(Y_search)): p=[X_search[i],Y_search[j]] M= M_function(p,theta,linear) - MinM_val if M<=sigma: X_aux=round(X_search[i],0) Y_aux=round(Y_search[j],0) p_aux=[X_aux,Y_aux] rep=0 for k in range(0,len(sigma_X)): if sigma_X[k]==X_aux and sigma_Y[k]==Y_aux: rep+=1 if rep==0: sigma_X.append(X_aux) sigma_Y.append(Y_aux) E1,E2=get_extrema_2loops(sigma_X, sigma_Y, 0 ) Extreme_sigma1=[sigma_X[E1],sigma_Y[E1]] Extreme_sigma2=[sigma_X[E2],sigma_Y[E2]] Extreme=[Extreme_sigma1,Extreme_sigma2] sigma_lenght=get_length(Extreme) try: S_output['sigma_lenght']=(sigma_lenght)/L except: print "Impossible to define sigma_lenght for "+str(mediatrix_data['id']) sigma_minor_axis=len(sigma_X)/(4*atan(1)*sigma_lenght) #print "Area de sigma, real" #for i in range(0,len(sigma_X)): # for j in range(0,len(sigma_Y)): # print sigma_X[i],sigma_Y[j] try: S_output['sigma_exc']=(sigma_lenght)/sigma_minor_axis except: print "Impossible to define sigma_exc for "+str(mediatrix_data['id']) if Area_out==True: pixels=np.where(obj_stamp>0) sigma_points=[sigma_X,sigma_Y] intersection=Area_intersection(pixels,sigma_points) S_output['intersection']=intersection return S_output