################################################################################################### 
# Example of using ImageJ functionalities through ijutils.
################################################################################################### 

from ijutils import *
import java.awt.Color as Color

#Image Loading
ip1 = load_image("{images}/img1.png", title="Image1")
ip2 = load_image("{images}/img2.png", title="Image2")
ip3 = load_image("{images}/img3.png", title="Grid")
ip4 = load_image("{images}/img4.png", title="Square")


#Basic operaions creation, copying, padding, saving 
resized = resize(ip2, 300,300)
save_image(resized,context.setup.expandPath("~/resized.tiff") ,"tiff")
crop=sub_image(ip2,10,20,50,30)    
bin_im = binning(ip2,2)
new_im = new_image(256, 256, "color")
copy_image_to(bin_im, new_im, 20, 20)
pad_im = pad_image(ip2, 1, 2, 3, 4, Color.RED)
stack=create_stack([ip2,resized,crop, bin_im, new_im, pad_im], title = "Basic Functions")
save_image(stack,context.setup.expandPath("~/stack.tiff") ,"tiff")
stack.show()


#Decomposing color channels
ip1.show()
create_stack([  get_channel(ip1, "red"), 
                get_channel(ip1, "green"), 
                get_channel(ip1, "blue"), 
                get_channel(ip1, "alpha"), 
                grayscale(get_channel(ip1, "brightness"), False)], title = "Color Decomposition").show()


#Histogram
plot(get_histogram(ip1))

#Changing LUT
ip = ip1.duplicate()
ip = grayscale(ip, in_place=False)
r,g,b = [],[],[]
for i in range(256):
    r.append(0)
    g.append(0)
    b.append(i)
set_lut(ip, r, g, b)
ip.show()


#Basic functions (in_place)
ip = ip1.duplicate()
ip.show()
grayscale(ip)
gaussian_blur(ip)
invert(ip)
smooth(ip)
sharpen(ip)
#edge(ip)
#add(ip)
#noise(ip, 100)
ip.show()


#Binarization and binary operations
ip = grayscale(ip2, in_place = False)
bin = ip1.duplicate()
ip_bin = auto_threshold(ip, in_place=False)
create_stack([ ip_bin, 
               binary_outline(ip_bin, in_place=False),
               binary_dilate(ip_bin, in_place=False),
               binary_erode(ip_bin, in_place=False),
               binary_open(ip_bin, in_place=False),
               binary_close(ip_bin, in_place=False),
               binary_skeletonize(ip_bin, in_place=False),
               binary_fill_holes(ip_bin, in_place=False),
               binary_outline(ip_bin, in_place=False)], title = "Binarization").show()


#EDM, const & image operations
ip = grayscale(ip2, in_place = False)
ip_bin = auto_threshold(ip, in_place=False)
binary_fill_holes(ip_bin)

edm = edm(ip_bin, in_place=False)
ws = watershed(ip_bin, in_place=False)
up = ultimate_points(ip_bin, in_place=False)
vr = veronoi(ip_bin, in_place=False)
edm_disp = remap(edm, in_place=False)
ws_disp = grayscale(ws, False)
up_disp = enhance_contrast(up, in_place=False)
vr_disp = enhance_contrast(vr, in_place=False)
create_stack([edm_disp, ip, ip_bin, ws_disp, up_disp, vr_disp], title = "EDM Operations").show()
final = grayscale(ip_bin, in_place = False)
op_const(final,"add", -200)
op_image(final, vr_disp, 'or')
op_image(final, up_disp, 'or')
final.show()


#Correlate, convolve, deconvolve
grayscale(ip3)
grayscale(ip4)
cor = op_fft(ip3, ip4, "correlate", True)
con = op_fft(ip3, ip4, "convolve", True)
dec = op_fft(con, ip4, "deconvolve", True)

create_stack([ip3,ip4, grayscale(cor,False),grayscale(con,False),grayscale(dec,False)], title = "Correlation").show()

ip = grayscale(ip2, in_place = False)

create_stack([ ip,                
               subtract_background(ip, in_place=False),
               smooth(ip, False),
               sharpen(ip, False),
               edges(ip, False),
               bandpass_filter(ip,0, 5, in_place=False),
               bandpass_filter(ip,5, 100, in_place=False),
               op_const(ip,"and", 127, False), 
               convolve(ip, KERNEL_BLUR, False),
               convolve(ip, KERNEL_SHARPEN, False),
               convolve(ip, KERNEL_SHARPEN_2, False),
               convolve(ip, KERNEL_LIGHT, False),
               convolve(ip, KERNEL_DARK, False),
               convolve(ip, KERNEL_EDGE_DETECT, False),
               convolve(ip, KERNEL_EDGE_DETECT_2, False),
               convolve(ip, KERNEL_DIFFERENTIAL_EDGE_DETECT, False),
               convolve(ip, KERNEL_PREWITT, False),
               convolve(ip, KERNEL_SOBEL, False)
               ], title = "General Operations").show()


#Rank operators
rank_opers = []
for op in "mean", "min", "max", "variance", "median", "close_maxima", "open_maxima", "remove_outliers", "remove_nan", "despeckle":
    rank_opers.append(op_rank(ip,op, in_place=False, kernel_radius=1))
create_stack(rank_opers, title = "Rank Operations").show()


#Reslicing
#orig = load_image("{data}/img/img2.png")
orig = resize(ip2, 300,200)
grayscale(orig)
images=[]
for i in range (20):        
    images.append(orig.duplicate())    
    op_const(orig, "multiply", 0.9)
stack=create_stack(images, title = "Original Stack")
#stack.show()    
r1 = reslice(stack, start_at="Left", title="Reslice Horizontally")
r2 = reslice(stack, start_at="Top", title="Reslice Vertically")
r1.show()
r2.show()


#Particle Analysis
ip = grayscale(ip2, in_place = False)
auto_threshold(ip)
#binary_fill_holes(ip)
#ip.show()
(results,output_img)=analyse_particles(ip, 100,1000, print_table=True)
output_img.show()

