129 lines
4.0 KiB
Python
129 lines
4.0 KiB
Python
show = False
|
|
img_size = 1024
|
|
RADIUS = 512
|
|
FRAMES = 1
|
|
offset = 0
|
|
add_mark = False
|
|
#offset = 50
|
|
#add_mark = True
|
|
|
|
|
|
Image_Correlator = Context.getClassByName("Image_Correlator")
|
|
correlator = Image_Correlator()
|
|
|
|
|
|
|
|
def get_roi_img(radius, offset_x=0, offset_y=0, frames=1):
|
|
x,y,w,h = int(cover_detection.config.center_x-img_size/2 + offset_x), \
|
|
int(cover_detection.config.center_y-img_size/2 + offset_y), \
|
|
img_size, img_size
|
|
if frames>1:
|
|
ip = integrate_frames(samples = frames)
|
|
roi = ImagingUtils.copy(ip.getBufferedImage(), None, Rectangle(x,y,w,h))
|
|
else:
|
|
roi = ImagingUtils.copy(image.output, None, Rectangle(x,y,w,h))
|
|
ip = load_image(roi, title="Img")
|
|
p = ip.getProcessor()
|
|
mask = p.createProcessor(roi.getWidth(), roi.getHeight())
|
|
mask.setColor(255)
|
|
cx, cy = img_size/2 - offset_x, img_size/2 - offset_y
|
|
mask.fillOval(int(cx - radius), int(cy - radius), int(2 * radius), int(2 * radius)) # Draw the circular ROI
|
|
mask.autoThreshold() # Ensures the mask is binary (if needed)
|
|
p.copyBits(mask, 0, 0, Blitter.AND) # Apply the mask using a bitwise AND
|
|
#gaussian_blur(ip)
|
|
p.blurGaussian(1.0)
|
|
return ip.getBufferedImage()
|
|
|
|
def get_edge_img(ib):
|
|
ed = ImagingUtils.edgeDetect(ib)
|
|
ret = load_image(ed, title="Refe")
|
|
#auto_threshold(ret, dark_background=True)
|
|
#binary_dilate(ret, iterations=1, count=1)
|
|
return ret
|
|
|
|
def put_mark(ib):
|
|
for x in range(450, 470):
|
|
for y in range(450, 470):
|
|
ib.setRGB(x,y, 0)
|
|
|
|
|
|
|
|
|
|
def load_ref_img(reload = False, frames=FRAMES):
|
|
global ip_ref, ip_ref_e, ip_ref_f, ip_ref_fe
|
|
if reload or "ip_ref_fe" not in globals():
|
|
print "Loading cover reference image"
|
|
roi =get_roi_img(RADIUS, 0, 0, frames)
|
|
ip_ref = load_image(roi, title="Ref")
|
|
ip_ref_e =get_edge_img(roi)
|
|
ip_ref_f = image_fft(ip_ref)
|
|
ip_ref_fe = image_fft(ip_ref_e)
|
|
|
|
load_ref_img()
|
|
|
|
def load_cur_img(frames=FRAMES):
|
|
global ip_cur, ip_cur_e, ip_cur_f, ip_cur_fe
|
|
roi =get_roi_img(RADIUS, -offset, -offset, frames)
|
|
if add_mark:
|
|
put_mark(roi)
|
|
ip_cur = load_image(roi, title="Ref")
|
|
ip_cur_e =get_edge_img(roi)
|
|
ip_cur_f = image_fft(ip_cur)
|
|
ip_cur_fe = image_fft(ip_cur_e)
|
|
|
|
load_cur_img()
|
|
|
|
|
|
#show_panel(ip1e.getBufferedImage())
|
|
#show_panel(ip2e.getBufferedImage())
|
|
|
|
coef_img, _ = correlator.getCorrelation(ip_ref, ip_cur)
|
|
coef_ed, _ = correlator.getCorrelation(ip_ref_e, ip_cur_e)
|
|
coef_fft, _ = correlator.getCorrelation(ip_ref_f, ip_cur_f)
|
|
coef_ffte, _ = correlator.getCorrelation(ip_ref_fe, ip_cur_fe)
|
|
|
|
#show_panel(sub1)
|
|
|
|
#sub = ImagingUtils.sub(BufferedImage, BufferedImage, boolean)
|
|
|
|
#sub = load_image(sub, title="sub")
|
|
#sube = load_image(sube, title="sube")
|
|
|
|
if show:
|
|
st=create_stack([ip_ref, ip_cur, ip_ref_e, ip_cur_e, ip_ref_f, ip_cur_f, ip_ref_fe, ip_cur_fe])
|
|
st.show()
|
|
|
|
|
|
def get_min(ip1):
|
|
return ip1.getProcessor().getStatistics().min
|
|
|
|
def mean_abs_error(ip1, ip2):
|
|
sum = 0.0
|
|
for y in range(ip1.getHeight()):
|
|
for x in range(ip1.getWidth()):
|
|
p1 = ip1.getPixel(x, y)
|
|
p2 = ip2.getPixel(x, y)
|
|
sum += abs(p1 - p2)
|
|
return sum / (ip1.height * ip1.width )
|
|
|
|
def rms_error(ip1, ip2):
|
|
sum = 0.0
|
|
for y in range(ip1.getHeight()):
|
|
for x in range(ip1.getWidth()):
|
|
p1 = ip1.getPixel(x, y)
|
|
p2 = ip2.getPixel(x, y)
|
|
sum += math.pow((p1 - p2),2)
|
|
return sum / (ip1.height * ip1.width )
|
|
|
|
|
|
mean_err, mean_erre = mean_abs_error(ip_ref_f.getProcessor(), ip_cur_f.getProcessor()), mean_abs_error(ip_ref_fe.getProcessor(), ip_cur_fe.getProcessor())
|
|
rms_err, rms_erre = rms_error(ip_ref_f.getProcessor(), ip_cur_f.getProcessor()), rms_error(ip_ref_fe.getProcessor(), ip_cur_fe.getProcessor())
|
|
|
|
if show:
|
|
#print offset, coef_img, coef_ed, coef_fft, coef_ffte
|
|
print coef_fft, coef_ffte
|
|
print mean_err, mean_erre
|
|
print rms_err, rms_erre
|
|
|
|
|
|
|