Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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