Files
tell/script/imgproc/CoverDetection.py
gac-S_Changer bdf5049f96 Creation
2018-12-03 12:17:40 +01:00

99 lines
3.1 KiB
Python

###################################################################################################
# Procedure to detect the cover orientation
###################################################################################################
assert_imaging_enabled()
#Parameters
FRAMES_INTEGRATION = 3
STEP_SIZE = 2
POSITION_NAMES = [ 'A','B','C','D', 'E', 'F']
#POSITION_ANGLES = [ 330, 30, 90, 150, 210, 270 ]
POSITION_ANGLES = [ 0, 60, 120, 180, 240, 300 ]
POSITION_TOLERANCE = 3
MINIMUM_CONFIDENCE = 3
DEBUG = cover_detection_debug
#REFERENCE_IMG = "ref2"
REFERENCE_IMG = "ref1"
BORDER = 7
#Load reference image
ref = load_image(str("{images}/cover/" + REFERENCE_IMG + ".png") , title="Line")
#Pre-process camera image
#ip = load_image("{images}/cover/Cover_000" + str(index) + ".png", title="Img")
ip = integrate_frames(FRAMES_INTEGRATION)
ip = grayscale(ip, True)
smooth(ip)
#bandpass_filter(ip, 30, 1000)
edges(ip)
auto_threshold(ip, method = "MaxEntropy")
#binary_erode(ip, True)
#binary_dilate(ip, True)
ip.getProcessor().erode(1, 255)
cx,cy = int(ip.width/2), int(ip.height/2)
ip = sub_image(ip, cx-ref.width/2, cy-ref.height/2, ref.width, ref.height)
if BORDER>0:
sip = sub_image(ip, BORDER,BORDER, ref.width-2*BORDER, ref.height-2*BORDER)
ip = pad_image(sip, BORDER, BORDER, BORDER, BORDER, fill_color=Color.WHITE)
#Show ROI of pre-processed image
if DEBUG:
image_panel = show_panel(ip.bufferedImage)
#Calculate correlation between image and reference, rotating the reference from 0 to 360
import ch.psi.pshell.imaging.Utils.integrateVertically as integrateVertically
ydata = []
xdata = range (0,360,STEP_SIZE)
for i in xdata:
r = ref.duplicate()
r.getProcessor().setBackgroundValue(0.0)
r.getProcessor().rotate(float(i))
op = op_fft(r, ip, "correlate")
bi = op.getBufferedImage()
p = integrateVertically(bi)
ydata.append(sum(p))
#Calculate angle of the highest correlation, and confidence level
peaks = estimate_peak_indexes(ydata, xdata, (min(ydata) + max(ydata))/2, 25.0)
peaks_x = map(lambda x:xdata[x], peaks)
peaks_y = map(lambda x:ydata[x], peaks)
if len(peaks_x) > 1:
#remoce close peaks between 350 deg and 10 deg
if ((peaks_x[0]<10) and (peaks_x[1]>350)) or ((peaks_x[1]<10) and (peaks_x[0]>350)):
peaks.pop(1)
peaks_x.pop(1)
peaks_y.pop(1)
confidence = None if len(peaks_x)<2 else int(((float(peaks_y[0])/peaks_y[1])-1) * 1000)
angle = (None if len(peaks_x)==0 else peaks_x[0])
#From angle and confidence level estimate hexiposi position
position = None
if angle is not None:
for i in range(len(POSITION_NAMES)):
if abs(POSITION_ANGLES[i] - angle) <= POSITION_TOLERANCE:
position = POSITION_NAMES[i]
#Plot the correlations values agains angle
if DEBUG:
plot(ydata, xdata=xdata)
#Output results
if DEBUG:
print "Peaks", peaks
print "Peak indexes: " + str(peaks_x)
print "Peak values: " + str(peaks_y)
print "Angle: " , angle
print "Position: " , position
print "Confidence: " , confidence
#Set return value
set_return ([position, angle, confidence])