Files
x06da/script/test/TestCoverDetection.py
alexgobbo d3b931cebd
2024-09-12 16:19:14 +02:00

80 lines
2.8 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
DETECT_THRESHOLD = 10 #PIXELS
FRAMES = 1
MASK_SIZE = 0.8 # of DIAM_2
renderer=show_panel(image)
renderer.setMarker(None);
start = time.time()
#Image Loading
if FRAMES>1:
ig = integrate_frames(samples = FRAMES)
else:
ip = load_image(image.output, title="Image")
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=True, \
output_image = "outlines", minCirc = 0.8, maxCirc = 1.0)
if res2.size() == 1:
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
#it.show()
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=True, \
output_image = "outlines", minCirc = 0.8, maxCirc = 1.0)
if res1.size() == res2.size() == 1:
x1,y1 = res1.getValue("XM", 0), res1.getValue("YM", 0)
if abs(x1-x2) <= DETECT_THRESHOLD and abs(y1-y2) <= DETECT_THRESHOLD:
print("Detected: ", (x1, y1))
renderer=show_panel(image)
p = Point(int(x1),int(y1))
ov = Overlays.Crosshairs(renderer.getPenProfile(), p, Dimension(-1, -1))
renderer.setMarker(ov);
print "Processing time:", time.time()-start