Demo entry 6326094

Code

   

Submitted by anonymous on Nov 22, 2016 at 16:13
Language: Python. Code size: 14.3 kB.

import arcpy
import numpy as np
import cv2

input=arcpy.GetParameterAsText(0) #the file to be analize by the chosen method



method= arcpy.GetParameterAsText(1) #the method to be used (Threshold, Combination, Haar, Fusing)
output=arcpy.GetParameterAsText(2) #the output file analized


threshold_value=arcpy.GetParameterAsText(3)# the threshold values (to apply to the image if the Threshold or combination) method is chosen (optionnal)

classifier=arcpy.GetParameterAsText(4)# the classifier to use for the Haar method if the Haar method is chosen (optionnal)
input2=arcpy.GetParameterAsText(5) #the second file to use in case of using the fusing method with both haar (optionnal)
                     

#............................................................................#
threshold_value_highlight=0
threshold_value_shadow=0
if len(threshold_value)==0:     #automatic finding of the threshold value if no threshold values are entered: building of the histogram.
    arcpy.AddMessage("Building Histogramm")
    
    i=0

    def find_value(image):    #take each pixel's value of the image

        a=[]
        b=[]
        c=[]
        d=[]
        e=[]
        f=[]
        g=[]
        h=[]
        i=[]
        j=[]
        k=[]
        l=[]
        m=[]
        n=[]
        o=[]
        p=[]
        q=[]
        r=[]
        s=[]
        t=[]
        u=[]
        v=[]
        w=[]
        x=[]
        y=[]
        z=[]
        liste=[a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
        white=[255,255,255]
        black=[0,0,0]
        j=0
        liste_values=liste[j]
        for number in image:
              for x in number:
                  if (x != white).all() and (x != black).all()  :                   
                    try:                        
                        liste_values.append(x[1])                        
                    except:
                        j+=1
                        liste_values=liste[j]
                        liste_values.append(x[1])                       
        return liste                   
        
    values=[]
    i=1
    while i <255:
        values.append(i)
        i+=1
    x=[]
    val_images=[]

    image=cv2.imread(input)  
    x=find_value(image)   
    for value in values:
        k=0
        for liste in x:            
            for number in liste:                
                if number==value:                    
                    k+=1       
        val_images.append(k)
    i=0
    j=0
    val=[]
    for value in values:
        k=0
        i=j
        for image in liste_jpeg2:
            k+=val_images[i]
            i+=254
        val.append(k)
        j+=1

        
   


    from Tkinter import *


    fenetre = Tk()
    canvas = Canvas(fenetre, width=2000, height=500, background='white')    #build the canvas
    maximum=max(val)    #take the maximum value
    k=0
    maximum_intensity=0
    for value in val:     #find the intensity corresponding to the maximum value
        if value==maximum:
            maximum_intensity=k    
        k+=1
            
        
    j=20
    k=0
    f=0
    max_five_percent=maximum*1/20      #the 5%
    
    s=460        #value to display the canvas


    while k <len(val):
        if val[k]> max_five_percent:
            if k<maximum_intensity:
                threshold_value_highlight=k    #find the highlight threshold
            elif f>maximum_intensity and f< 255:
                threshold_value_shadow=f    #find the shadow threshold
                f=600
            ligne1 = canvas.create_line(j, 450, j, 450-(val[k]/i))
            
            
        else:
            canvas.create_line(j, 450, j, 450-(val[k]/i), fill="red")
            
        if k%5==0:
            txt = canvas.create_text(j, s, text=str(k), font="Arial 10 italic", fill="black")  #value to draw 
        if k%2==0:
            s=470
        else:
            s=460
        k+=1
        f+=1
        j+=5
        
        canvas.pack()




    line_ten_percent=canvas.create_line(20, 450-(max_ten_percent/i), 2000, 450-(max_ten_percent/i))

    line_100=canvas.create_line(20, 450-a, 2000, 450-a)

    line_50=canvas.create_line(20, 450-a/2, 2000, 450-a/2)
    line_25=canvas.create_line(20, 450-a/4, 2000, 450-a/4)

    txt2 = canvas.create_text(10, 450-a, text="100", font="Arial 10 italic", fill="black")
    txt3= canvas.create_text(10, 450-a/2, text="50", font="Arial 10 italic", fill="black")
    txt4= canvas.create_text(10, 450-a/4, text="25", font="Arial 10 italic", fill="black")
    txt4= canvas.create_text(10, 450-a/20, text="5", font="Arial 10 italic", fill="black")
    canvas.pack()


    fenetre.mainloop()

    threshold_value=[]
    threshold_value.append(threshold_value_highlight)
    threshold_value.append(threshold_value_shadow)          #list of the two threshold, that will be used later

#.............................................................................#
else:

    
    split=threshold_value.split(",")
    
    threshold_value=[]
                                #transform the string of threshold values in integer (if the values are entered)
    threshold_value.append(int(split[0]))
    threshold_value.append(int(split[1]))
    
#..........................................................................................#
if method=="Threshold":  #apply Threshold method
    arcpy.AddMessage("Doing Threshold")
    im = cv2.imread(input,0)         

    ret,thresh = cv2.threshold(im,threshold_value[0],255,0) #apply threshold to the image (highlights)
    image, contours, hierarchy = cv2.findContours(thresh,1,cv2.CHAIN_APPROX_SIMPLE) #create contours
    
    i=0
    a=[]
    for cnt in contours:      #filter of contours
        if len(cnt)<10:
            a.append(i)
        i+=1
    contours2=np.delete(contours,a) #delete contours filtered
    im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)    
    im=cv2.drawContours(im, contours2, -1, (0,255,0), 1) #draw the contours on the image
    cv2.imwrite(output,im)      #save the analyzed image
#............................................................................#

elif method=="Combination":       #apply combination method
    arcpy.AddMessage("Doing Combination")
    im = cv2.imread(input,0)
        
    ret,thresh = cv2.threshold(im,threshold_value[0],255,0)    #apply threshold to the image (highlights)
    ret2,thresh2 = cv2.threshold(im,threshold_value[1],255,cv2.THRESH_BINARY_INV) #inversed binary because we want value above (shadows) 
       
    image, contours, hierarchy = cv2.findContours(thresh,1,cv2.CHAIN_APPROX_SIMPLE)#create contours highlights
    image2, contours2, hierarchy2 = cv2.findContours(thresh2,1,cv2.CHAIN_APPROX_SIMPLE)#create contours shadows
    
    i=0
    a=[]
    for cnt in contours:    #filter of highlights contours
        if len(cnt)<10 or len(cnt)>200:
            a.append(i)
        i+=1
    contours=np.delete(contours,a)  #delete contours filtered
    
    b=[]
    i=0
    for cnt in contours2:    #filter of  shadows contours
        if len(cnt)<10 or len(cnt)>200:
            b.append(i)
        i+=1
    contours2=np.delete(contours2,b)   #delete contours filtered
    im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB) 
    k=0
 
    l=1
    length=len(contours)
    length2=len(contours2)
    arcpy.AddMessage("Contours highlight:"+str(length)+"\nContours Shadow:"+str(length2))
    for cnt in contours:


        x,y,w,h = cv2.boundingRect(cnt)  #gives the x,y, width and height of the bouding rectangle of the contour
        
        if w <300 and h<300 and w >10 and h>10 :    #don't apply it to rectangle bigger than 30m (irrelevant)
            for cnt in cnt:     #give coordinate of each point of the contour
                for cnt2 in contours2:
                    x2,y2,w2,h2 = cv2.boundingRect(cnt2)  #gives the x,y, width and height of the bouding rectangle of the contour
                    if w2 <300 and h2<300 and w2 >10 and h2>10:    #don't apply it to rectangle bigger than 30m (irrelevant)
                        for cnt2 in cnt2:
                            
                            if np.allclose([cnt],[cnt2],0,4)==True: #condition of proximity (between highlight and shadow)
                                
                                im = cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),1) #draw on image
                   
        if k==int(length*l/100):                #give the progression in % to the user
           arcpy.AddMessage(str(l)+"%")
           l+=1
           

        k+=1
                            
                
                    
                            
                               
    cv2.imwrite(output,im)        #save the analyzed image            
    
#.....................................................................................................#

elif method=="Haar":   #apply the Haar classifier method
    arcpy.AddMessage("Doing Haar")
    art_detection=cv2.CascadeClassifier(classifier)   #the classifier to apply (an .xml file)

    
    im = cv2.imread("H:\\Dissertation\\Test\\haar_400khz_10cm\\0.jpeg")

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


    art = art_detection.detectMultiScale(gray, 1.7, 1) #apply the classifier to the image
    k=0
    l=1
    im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)
    length=len(art)
    for (x,y,w,h) in art:
        if w<300 and h <300 and w>10 and h >10: #don't apply it to rectangle bigger than 30m and smaller than 40 cm (irrelevant)
            cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)   #draw the detection
        if k==int(length*l/100):                #give the progression in % to the user
            arcpy.AddMessage(str(l)+"%")
            l+=1
           

        k+=1
    cv2.imwrite(output,img) #save the analyze image

#....................................................................................................#
    
elif method== "Fusing":     #apply the fusing method (the best result one) merging of both classifier (the two surveys) with thresholding
    arcpy.AddMessage("Doing Fusing")
    cell_x=arcpy.GetRasterProperties_management(input,"CELLSIZEX")
    cell_y=arcpy.GetRasterProperties_management(input,"CELLSIZEX")
    cell_x2=arcpy.GetRasterProperties_management(input2,"CELLSIZEY")
    cell_y2=arcpy.GetRasterProperties_management(input2,"CELLSIZEY")
    
    right_x=arcpy.GetRasterProperties_management(input,"RIGHT")
    bottom_y=arcpy.GetRasterProperties_management(input,"BOTTOM")
    right_x2=arcpy.GetRasterProperties_management(input2,"RIGHT")
    bottom_y2=arcpy.GetRasterProperties_management(input2,"BOTTOM")

    left_x=arcpy.GetRasterProperties_management(input,"LEFT")
    top_y=arcpy.GetRasterProperties_management(input,"TOP")
    left_x2=arcpy.GetRasterProperties_management(input2,"LEFT")
    top_y2=arcpy.GetRasterProperties_management(input2,"TOP")
    
    if cell_x==cell_x2 and cell_y==cell_y2 and right_x==right_x2 and bottom_y==bottom_y2 and left_x==left_x2 and top_y==top_y2:  #comparing extent of the two raster,
                                                                                                                                #it has to be the same extent and pixelsize
            
        art_detection=cv2.CascadeClassifier(classifier)


        img = cv2.imread(input)
        img2 = cv2.imread(input2)

       
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

        art = art_detection.detectMultiScale(gray, 1.7, 1)    #apply the classifier to the image

        art2 = art_detection.detectMultiScale(gray2, 1.7, 1)  #apply the classifier to the image

        art=np.concatenate((art,art2)) #merge the two detection
        img = cv2.imread(input)
        img2 = cv2.imread(input2)


        
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret,thresh = cv2.threshold(gray,threshold_value[0],255,cv2.THRESH_BINARY)
        image, contours, hierarchy = cv2.findContours(thresh,1,cv2.CHAIN_APPROX_SIMPLE)
        a=[]
        i=0
        for cnt in contours:    #filter of  shadows contours
            if len(cnt)<10:
                b.append(i)
            i+=1
        contours=np.delete(contours,a)   #delete contours filtered
        gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
        ret2,thresh2 = cv2.threshold(gray2,threshold_value[0],255,cv2.THRESH_BINARY)
        image, contours2, hierarchy2 = cv2.findContours(thresh2,1,cv2.CHAIN_APPROX_SIMPLE)
        a=[]
        i=0

        for cnt in contours2:    #filter of  shadows contours
            if len(cnt)<10:
                b.append(i)
            i+=1
        contours=np.delete(contours2,a)   #delete contours filtered
        contours=np.concatenate((contours,contours2))#concatenate both filtered
        k=0
        a=[]
        im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)
        length=len(art)
        k=0
        l=1
        for (x,y,w,h) in art:
            a=""
            if w<300 and h <300:
                for cnt in contours:                #take only Haar and Threshold detection that matches
                    x2,y2,w2,h2 = cv2.boundingRect(cnt)
                    center_x=x2+(w2/2)
                    center_y=y2+(h2/2)
                    if center_x>x and center_x<x+w and center_y>y and center_y<y+h:
                        

                        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
            if k==int(length*l/100):                    #give the progression in % to the user
                arcpy.AddMessage(str(l)+"%")
                l+=1
           

            k+=1
                        
        cv2.imwrite(output,img) #save the analyze image

    else:
        arcpy.AddMessage("The two rasters does not have the same extent or pixel's size")  #wright this message if the rasters does not have same extent and same pixel's size
    

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).