very promissing fiducial detection

This commit is contained in:
2022-09-15 19:19:13 +02:00
parent 7534eec921
commit 6a7b5ab91e
4 changed files with 197 additions and 58 deletions

View File

@@ -16,10 +16,18 @@ modes:
0x02: update_optical_center
'''
import logging
import numpy as np
_log=logging.getLogger(__name__)
import numpy as np
import PIL.Image
try:
from scipy import ndimage, signal
except ImportError as e:
_log.warning(e)
try:
import cv2 as cv
except ImportError as e:
_log.warning(e)
class geometry:
@@ -255,7 +263,6 @@ class geometry:
# n number of images to take in region
# roi region of interrest to calculate sharpness
# mode mode to calculate sharpness (sum/max-min/hist? of edge detection in roi)
import PIL.Image
if mot is not None:
p0=mot.get_rbv()
else:
@@ -277,7 +284,6 @@ class geometry:
mot.move_abs(p0)
return p0
else:
from scipy import ndimage, signal
if type(cam) == list:
imgLst=cam
n=len(imgLst)
@@ -321,6 +327,45 @@ class geometry:
return p
pass
@staticmethod
def find_fiducial(cam,sz=(210,210),brd=(20,20)):
if type(cam)==str:
img=PIL.Image.open(cam)
img=np.asarray(img)
else:
img=cam._pic # get_image()
img16=np.array(img, np.int16)
fid=np.ones((sz[1]+2*brd[1],sz[0]+2*brd[0]),dtype=np.uint8)*255
fid[brd[1]:sz[1]+brd[1],brd[0]:sz[0]+brd[0]]=0
mask=np.ones((sz[1]+2*brd[1],sz[0]+2*brd[0]),dtype=np.uint8)*255
mask[2*brd[1]:sz[1],2*brd[0]:sz[0]]=0
#https://docs.opencv.org/4.5.2/d4/dc6/tutorial_py_template_matching.html
#res = cv.matchTemplate(img,fid,cv.TM_CCORR_NORMED )
res = cv.matchTemplate(img,fid,cv.TM_CCORR_NORMED,mask=mask)
h,w=img.shape
fh2,fw2=fid.shape
fw2//=2
fh2//=2
mtr=np.ndarray((5,2),np.uint16)
corr=np.ndarray((5,),np.float32)
for i in range(5):
p=np.unravel_index(res.argmax(), res.shape)
corr[i]=res[p]
mtr[i,:]=p
y0=max(p[0]-fh2,0)
y1=min(p[0]+fh2,h)
x0=max(p[1]-fw2,0)
x1=min(p[1]+fw2,w)
res[y0:y1,x0:x1]*=.5
pos=mtr.mean(0)[::-1]+(fw2,fh2)
crm=corr.mean()
_log.debug(f'position: {pos} correlation:{crm}')
return (pos,crm)
def pix2pos(self, p, zoom=None):
# returns the position m(x,y) in meter relative to the optical center at a given zoom level of the pixel p(x,y)
# if zoom=None, the last zoom value is used
@@ -627,6 +672,8 @@ if __name__=="__main__":
import glob
imgLst=sorted(glob.glob("scratch/autofocus2/image*.png"))
geometry.autofocus(imgLst,None)
if args.mode&0x10:
geometry.find_fiducial("scratch/fiducial/image001.png")
# pix2pos="[[1.0, 200.0, 400.0, 600.0, 800.0], [[[0.001182928623952055, -2.6941995127711305e-05], [-4.043716694634124e-05, -0.0011894314084263825]], [[0.0007955995220142541, -3.175003727901119e-05], [-2.0896601103372113e-05, -0.0008100805094631365]], [[0.00048302539335378367, -1.1661121407652543e-05], [-2.0673746995751222e-05, -0.0004950857899461772]], [[0.00028775903460576395, -1.3762555219494508e-05], [-9.319936861519268e-06, -0.0002889214488565999]], [[0.0001788819256630411, -6.470841493681516e-06], [-2.0336605088889967e-06, -0.0001831131753499113]]]]"