81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
import math
|
|
from ijutils import *
|
|
import ij.process.Blitter as Blitter
|
|
|
|
|
|
FILL_STRATEGY = "mask" #"mask", "bits", "none"
|
|
OPEN_STRATEGY = "both" #"black", "white", "both", "none"
|
|
DIAM_1 = 84
|
|
DIAM_2 = 550
|
|
OFFSET_THRESHOLD= 10 #PIXELS
|
|
MASK_SIZE = 0.8 # of DIAM_2
|
|
|
|
|
|
def detect(ip, print_table=False, output_image=False):
|
|
if output_image==True:
|
|
output_image= "outlines"
|
|
if ip.getProcessor().isGrayscale():
|
|
ig = ip
|
|
else:
|
|
ig = grayscale(ip, in_place=False)
|
|
#Threshoolding
|
|
it = auto_threshold(ig, dark_background=True, in_place=False)
|
|
w, h = it.getWidth(), it.getHeight()
|
|
#it.show()
|
|
if OPEN_STRATEGY in ("black", "both"): #Refine inner
|
|
binary_open(it, dark_background=True, iterations=1, count=1);
|
|
if OPEN_STRATEGY in ("white", "both"): #Refine outter
|
|
binary_open(it, dark_background=False, iterations=1, count=1);
|
|
#it.repaintWindow()
|
|
|
|
#Detect outer white circle
|
|
px = int(pow(DIAM_2/2,2) * math.pi)
|
|
(res2,out2)=analyse_particles(it, px/2, px*2, \
|
|
fill_holes = True, exclude_edges = True, print_table=print_table, \
|
|
output_image = output_image, minCirc = 0.8, maxCirc = 1.0)
|
|
|
|
if res2.size() != 1:
|
|
msg = "Outter detection error: " + ("none" if res2.size()==0 else "")
|
|
for i in range( res2.size()):
|
|
msg = msg + "(" + str(res2.getValue("XM", 0)) + ", " + str( res2.getValue("YM", 0)) + ")"
|
|
raise Exception( msg )
|
|
|
|
x2,y2 = res2.getValue("XM", 0), res2.getValue("YM", 0)
|
|
invert(it, in_place=True)
|
|
ip = it.getProcessor()
|
|
dist = DIAM_2 * (MASK_SIZE/2.0)
|
|
if FILL_STRATEGY == "bits":
|
|
for y in range(h):
|
|
for x in range(w):
|
|
distance = math.hypot(x - x2, y - y2)
|
|
if distance >dist:
|
|
ip.putPixel(x, y, 0)
|
|
elif FILL_STRATEGY == "mask":
|
|
mask = ip.createProcessor(w, h) # Creates a new blank ImageProcessor
|
|
mask.setColor(255) # Set the drawing color to white (255)
|
|
mask.fillOval(int(x2 - dist), int(y2 - dist), int(2 * dist), int(2 * dist)) # Draw the circular ROI
|
|
mask.autoThreshold() # Ensures the mask is binary (if needed)
|
|
ip.copyBits(mask, 0, 0, Blitter.AND) # Apply the mask using a bitwise AND
|
|
|
|
px = int(pow(DIAM_1/2,2) * math.pi)
|
|
(res1,out1)=analyse_particles(it, px/2, px*2, \
|
|
fill_holes = FILL_STRATEGY!="none", exclude_edges = True, print_table=print_table, \
|
|
output_image = output_image, minCirc = 0.8, maxCirc = 1.0)
|
|
|
|
if res1.size() != 1:
|
|
msg = "Inner detection error: " + ("none" if res1.size()==0 else "")
|
|
for i in range( res1.size()):
|
|
msg = msg + "(" + str(res1.getValue("XM", 0)) + ", " + str( res2.res1("YM", 0)) + "), "
|
|
raise Exception( msg )
|
|
|
|
x1,y1 = res1.getValue("XM", 0), res1.getValue("YM", 0)
|
|
|
|
if abs(x1-x2) > OFFSET_THRESHOLD or abs(y1-y2) > OFFSET_THRESHOLD:
|
|
msg = "Detection offset error: " + + "(" + str(x1-x2) + ", " + str(y1-y2) + ")"
|
|
raise Exception( msg )
|
|
|
|
return x1,y1
|
|
|
|
|
|
|