################################################################################################### # Procedure to detect the cover orientation ################################################################################################### #Parameters FRAMES_INTEGRATION = 3 STEP_SIZE = 2 POSITION_NAMES = [ 'A','B','C','D', 'E', 'F'] POSITION_ANGLES = [330, 30, 90, 150, 210, 270] POSITION_TOLERANCE = 3 MINIMUM_CONFIDENCE = 10 DEBUG = cover_detection_debug REFERENCE_IMG = "ref2" #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") 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) #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) 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: p = plot(ydata, xdata=xdata)[0] #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])