Advertisement
Guest User

Fiji script for protein detection

a guest
Oct 23rd, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. from ij import IJ, ImagePlus
  2. from ij.process import FloatProcessor
  3. from array import zeros
  4.  
  5.  
  6. def box_maker(radius):
  7.     box = []
  8.     for i in range(2*radius):
  9.         y = []
  10.         for j in range(2*radius):
  11.             y.append(0)
  12.         box.append(y)
  13.  
  14.     box[4][4] = 1
  15.     box[4][3] = 1
  16.     box[3][4] = 1
  17.     box[5][4] = 1
  18.     box[4][5] = 1
  19.     return box
  20.  
  21. def config_img(imp):
  22.     pix = list(imp.getProcessor().getPixels())
  23.     w = imp.getWidth()
  24.     h = imp.getHeight()
  25.     new_im = []
  26.  
  27.     for i in range(h):
  28.         y = []
  29.         for j in range(w):
  30.             y.append(pix[i*h+j])
  31.         new_im.append(y)
  32.  
  33.     return new_im
  34.  
  35. def protein_find(pix, y, x, boxcent, boxedge):
  36.     centsum = 0.0
  37.     for boxx, boxy in boxcent:
  38.         centsum += pix[y + boxy][x + boxx]
  39.     centavg = centsum/5
  40.  
  41.     edgesum = 0.0
  42.     for boxx, boxy in boxedge:
  43.         edgesum += pix[y + boxy][x + boxx]
  44.     edgeavg = edgesum/36
  45.  
  46.     if centavg/edgeavg > 2.0:
  47.         return x, y
  48.     else:
  49.         return 0, 0
  50.            
  51.  
  52.  
  53. radius = 5
  54.  
  55. box = box_maker(radius)
  56. imp = IJ.getImage()
  57. h = imp.getHeight()
  58. w = imp.getWidth()
  59. IJ.run("16-bit")
  60.  
  61. #------------------------------------------------
  62. #Adaptive Histogram Equalisation
  63. blocksize = '127'
  64. histogram_bins = '256'
  65. maximum_slope = '3'
  66. mask = "*None*"
  67. fast = True
  68. process_as_composite = True
  69.  
  70. parameters = "blocksize =" + blocksize + " histogram=" + histogram_bins + \
  71.     " maximum=" + maximum_slope + " mask=" + mask
  72. if fast:
  73.   parameters += " fast_(less_accurate)"
  74.  
  75. IJ.run( "Enhance Local Contrast (CLAHE)", parameters )
  76.  
  77. #------------------------------------------------
  78.  
  79. IJ.run("Subtract Background...", "rolling=10")
  80. new_pix = config_img(imp)
  81.  
  82.  
  83. boxcent = [[4, 4], [3, 4], [4, 3], [5, 4], [4,5]]
  84. boxedge = [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9],
  85. [1, 0], [1, 9], [2, 0], [2, 9], [3, 0], [3, 9], [4, 0], [4, 9], [5, 0], [5, 9], [6, 0], [6, 9],
  86. [7, 0], [7, 9], [8, 0], [8, 9], [9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7],
  87. [9, 8], [9, 9]]
  88.  
  89. prots = []
  90. counts = 0
  91. for i in range(h):
  92.     for j in range(w):
  93.         if (i > 0 + radius) and (i < h - 2*radius) and (j > 0 + radius) and (j < w - 2*radius):
  94.             outx, outy = protein_find(new_pix, i, j, boxcent, boxedge)
  95.             if outx != 0:
  96.                 prots.append([i, j])
  97.                 counts +=1
  98.  
  99.  
  100.  
  101.  
  102. pix2 = zeros('f', w * h)
  103.  
  104. for i in range(len(prots)):
  105.     x, y = prots[i]
  106.     pix2[x*h+y] = 1.0
  107.  
  108. fp = FloatProcessor(imp.getWidth(), imp.getHeight(), pix2, None)
  109. imp3 = ImagePlus("prots", fp)
  110. imp3.show()
  111. print counts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement