99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
import ch.psi.pshell.imaging.ImageBuffer as ImageBuffer
|
|
|
|
offset = 0
|
|
add_mark = False
|
|
#offset = 50
|
|
#add_mark = True
|
|
|
|
|
|
Image_Correlator = get_context().getClassByName("Image_Correlator")
|
|
correlator = Image_Correlator()
|
|
|
|
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 )
|
|
|
|
|
|
|
|
def get_roi_img(img, img_size=1024, radius=512, offset_x=0, offset_y=0):
|
|
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
|
|
roi = ImagingUtils.copy(img, 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 save_ref_img(img):
|
|
path = os.path.abspath(expand_path("{images}/ip_ref_fe.png"))
|
|
roi=get_roi_img(img, offset_x=0, offset_y=0)
|
|
ImageBuffer.saveImage(roi, path, "png")
|
|
|
|
#ip_ref = load_image(roi, title="ip_ref")
|
|
#save_image(ip_ref, path ,"png")
|
|
#$ImagingUtils.
|
|
#ip_ref_e =get_edge_img(roi)
|
|
#save_image(ip_ref_e, path+"ip_ref_e.png" ,"png")
|
|
##ip_ref_f = image_fft(ip_ref)
|
|
#ip_ref_fe = image_fft(ip_ref_e)
|
|
#save_image(ip_ref_fe, path+"ip_ref_fe.png" ,"png")
|
|
|
|
|
|
def put_mark(ib):
|
|
for x in range(450, 470):
|
|
for y in range(450, 470):
|
|
ib.setRGB(x,y, 0)
|
|
|
|
def get_correlation(img):
|
|
global ip_ref_fe
|
|
if "ip_ref_fe" not in globals() or ip_ref_fe is None:
|
|
path = os.path.abspath(expand_path("{images}/ip_ref_fe.png"))
|
|
roi = ImagingUtils.newImage(path)
|
|
#ip_ref_fe=open_imagroie(path+"ip_ref_fe.png" ,"png")
|
|
#ip_ref = load_image(roi, title="ip_ref")
|
|
ip_ref_e =get_edge_img(roi)
|
|
ip_ref_fe = image_fft(ip_ref_e)
|
|
print "Opend reference image"
|
|
|
|
roi =get_roi_img(img, offset_x=-offset, offset_y=-offset)
|
|
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)
|
|
return correlator.getCorrelation(ip_ref_fe, ip_cur_fe)[0]
|
|
|
|
|