very promissing fiducial detection
This commit is contained in:
@@ -14,17 +14,50 @@ import numpy as np
|
||||
_log=logging.getLogger(__name__)
|
||||
|
||||
import numpy as np
|
||||
|
||||
import cv2 as cv
|
||||
import PIL.Image
|
||||
from scipy import ndimage,signal
|
||||
import glob
|
||||
import glob, os
|
||||
from pyqtgraph.Qt import QtCore, QtGui
|
||||
import pyqtgraph as pg
|
||||
pg.setConfigOptions(imageAxisOrder='row-major')
|
||||
|
||||
|
||||
def set_fiducial(pic):
|
||||
# fiducial test
|
||||
f=np.array(((0, 0, 0, 0, 0),
|
||||
(0, 1, 1, 1, 0),
|
||||
(0, 1, 0, 0, 0),
|
||||
(0, 1, 1, 0, 0),
|
||||
(0, 1, 0, 0, 0),
|
||||
(0, 0, 0, 0, 0),), pic.dtype)
|
||||
pic[0:6, 0:5]=f*pic.max()
|
||||
|
||||
def wtestimages():
|
||||
fn=os.path.expanduser('~/Documents/prj/SwissFEL/epics_ioc_modules/ESB_MX/python/SwissMX/scratch/fiducial/image{idx:03d}.png')
|
||||
img=np.kron(np.array([[1, 0] * 5, [0, 1] * 5] * 6,dtype=np.uint8), np.ones((100, 100),dtype=np.uint8)*255)
|
||||
set_fiducial(img)
|
||||
pil_img=PIL.Image.fromarray(img)
|
||||
pil_img.save(fn.format(idx=2))
|
||||
|
||||
img=np.ndarray((1000,1200),dtype=np.uint8)
|
||||
img[:]=255
|
||||
#img=np.zeros((1200,1000),dtype=np.uint8)
|
||||
fid=np.array([[0, 1] * 2, [1, 0] * 2] * 2,dtype=np.uint8)[:3,:3]
|
||||
fid=np.kron(fid, np.ones((210, 210),dtype=np.uint8)*255)
|
||||
img[100:100+fid.shape[0],200:200+fid.shape[1]]=fid
|
||||
set_fiducial(img)
|
||||
pil_img=PIL.Image.fromarray(img)
|
||||
pil_img.save(fn.format(idx=3))
|
||||
|
||||
img=np.ndarray((1200,1000))
|
||||
|
||||
wtestimages()
|
||||
|
||||
|
||||
class fiducial(QtGui.QMainWindow):
|
||||
def __init__(self, parent = None):
|
||||
super(autofocus, self).__init__(parent)
|
||||
super(fiducial, self).__init__(parent)
|
||||
self.resize(800, 1500)
|
||||
self.setWindowTitle('pyqtgraph example: DataSlicing')
|
||||
cw=QtGui.QWidget()
|
||||
@@ -33,7 +66,7 @@ class fiducial(QtGui.QMainWindow):
|
||||
cw.setLayout(l)
|
||||
|
||||
#self._imgLst=imgLst=sorted(glob.glob("../scratch/autofocus1/image*.png"))
|
||||
self._imgLst=imgLst=sorted(glob.glob("../scratch/autofocus2/image*.png"))
|
||||
self._imgLst=imgLst=sorted(glob.glob("../scratch/fiducial/*.png"))
|
||||
self._metrics=mtr=np.ndarray(shape=(len(imgLst), 5))
|
||||
mtr[:]=0
|
||||
self._sld=sld=QtGui.QSlider(QtCore.Qt.Horizontal)
|
||||
@@ -69,8 +102,10 @@ class fiducial(QtGui.QMainWindow):
|
||||
|
||||
self._imv1.setHistogramRange(0, 100)
|
||||
self._imv1.setLevels(0, 40)
|
||||
self._imv2.setHistogramRange(0, 100)
|
||||
self._imv2.setLevels(0, 40)
|
||||
#self._imv2.setHistogramRange(0, 100)
|
||||
#self._imv2.setLevels(0, 40)
|
||||
self._imv2.setHistogramRange(0, 1)
|
||||
self._imv2.setLevels(0, 1)
|
||||
|
||||
def cb_sld_change(self,val,auto=False):
|
||||
i=self._sld.value()
|
||||
@@ -78,53 +113,88 @@ class fiducial(QtGui.QMainWindow):
|
||||
fn= self._imgLst[i]
|
||||
img=PIL.Image.open(fn)
|
||||
img=np.asarray(img)
|
||||
#slb=ndimage.sobel(img)
|
||||
img16=np.array(img,np.int16)
|
||||
s1=np.array(((1,0,-1),(2,0,-2),(1,0,-1)),np.int16)
|
||||
sb1=signal.convolve2d(img16, s1, mode='same', boundary='fill', fillvalue=0)
|
||||
sb2=signal.convolve2d(img16, s1.T, mode='same', boundary='fill', fillvalue=0)
|
||||
sb=np.abs(sb1)+np.abs(sb2)
|
||||
#pil_img=PIL.Image.fromarray(img)
|
||||
#pil_img.save('../scratch/fiducial/image002.png')
|
||||
|
||||
#remove irrelevant low values
|
||||
idx=sb[:]<20
|
||||
sbLut=sb*1
|
||||
sbLut[idx]=0
|
||||
#import numpy as np
|
||||
#import matplotlib.pyplot as plt
|
||||
mx=sb.max()+1
|
||||
lut=(np.sin(np.arange(mx)/mx*np.pi-np.pi/2)+1)*128
|
||||
#lut=((np.arcsin(np.arange(mx)/(mx-1)*2-1)/np.pi)+.5)*255
|
||||
#lut=np.array(lut*255,np.uint16)
|
||||
#sbLut=lut[sb]
|
||||
#import matplotlib.pyplot as plt
|
||||
#plt.plot(lut);plt.show()
|
||||
#img2=ndimage.grey_dilation(sb,size=(5,5)) #, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
|
||||
img2=ndimage.grey_closing(sb,size=(25,25)) #, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
|
||||
#fn='/home/zamofing_t/Documents/prj/SwissFEL/epics_ioc_modules/ESB_MX/python/SwissMX/scratch/fiducial/zoom0400.png'
|
||||
#pil_img=PIL.Image.open(fn+'_')
|
||||
#img=np.asarray(pil_img)
|
||||
#img=np.array(img,dtype=np.uint8)
|
||||
#pil_img=PIL.Image.fromarray(img)
|
||||
#pil_img.save(fn)
|
||||
|
||||
|
||||
#fft=np.log(np.abs(np.fft.fft2(sb)))
|
||||
#img16=np.array(img,np.int16)
|
||||
#fft=np.log(np.abs(np.fft.fft2(img)))
|
||||
#fft=np.fft.fftshift(fft)
|
||||
# fft[300:700,400:800]=0
|
||||
# v[i,1]=fft.sum()
|
||||
|
||||
self._imv1.setImage(sb,autoRange=auto,autoLevels=auto,autoHistogramRange=auto)
|
||||
#self._imv2.setImage(sb,autoRange=auto,autoLevels=auto,autoHistogramRange=auto)
|
||||
self._imv2.setImage(sbLut,autoRange=auto,autoLevels=auto,autoHistogramRange=auto)
|
||||
mtr=self._metrics
|
||||
fid=np.array([[0, 1]*2, [1, 0]*2]*2, dtype=np.uint8)[:3, :3]
|
||||
fid=np.kron(fid, np.ones((100, 100), dtype=np.uint8)*255)
|
||||
#res = cv.matchTemplate(img,fid,cv.TM_CCORR_NORMED )
|
||||
|
||||
mtr[i, 0]=img.std()
|
||||
mtr[i, 1]=sb.sum()
|
||||
mtr[i, 2]=sb.std()
|
||||
mtr[i, 3]=sbLut.sum()#sb.std()
|
||||
mtr[i, 4]=sbLut.std()#img2.sum()
|
||||
#fid=np.ones((250,250),dtype=np.uint8)*255
|
||||
#fid[20:230,20:230]=0
|
||||
|
||||
mx=mtr.max(0)
|
||||
mn=mtr.min(0)
|
||||
mtr=(mtr-mn)/(mx-mn)
|
||||
sz=(90, 90); brd=(20, 20) # zoom 001
|
||||
#sz=(130, 130); brd=(20, 20) # zoom 200
|
||||
#sz=(210, 210); brd=(20, 20) # zoom 400
|
||||
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
|
||||
|
||||
_log.debug(f'{i} {mtr[i,:]}')
|
||||
for i in range(mtr.shape[1]):
|
||||
self._plt[i].setData(mtr[:,i])
|
||||
|
||||
#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)
|
||||
corr_u8=np.array(res*255,np.uint8)
|
||||
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
|
||||
res[p[0],p[1]]=0
|
||||
|
||||
corr_u8=np.array(res*255,np.uint8)
|
||||
|
||||
ctr=mtr.mean(0,dtype=np.int32)+(fw2,fh2)
|
||||
try:
|
||||
img=img*1
|
||||
img[ctr[0]-5:ctr[0]+5,ctr[1]-5:ctr[1]+5]=255
|
||||
img[ctr[0],:]=0
|
||||
img[:,ctr[1]]=0
|
||||
except IndexError as e:
|
||||
pass
|
||||
self._imv1.setImage(img,autoRange=auto,autoLevels=auto,autoHistogramRange=auto)
|
||||
self._imv2.setImage(res,autoRange=auto,autoLevels=auto,autoHistogramRange=auto)
|
||||
pos=mtr.mean(0)[::-1]+(fw2,fh2)
|
||||
_log.debug(f'position: {pos} correlation:{corr.mean()}')
|
||||
|
||||
|
||||
#mtr=self._metrics
|
||||
|
||||
#mtr[i, 0]=img.std()
|
||||
#mtr[i, 1]=sb.sum()
|
||||
#mtr[i, 2]=sb.std()
|
||||
#mtr[i, 3]=sbLut.sum()#sb.std()
|
||||
#mtr[i, 4]=sbLut.std()#img2.sum()
|
||||
|
||||
#mx=mtr.max(0)
|
||||
#mn=mtr.min(0)
|
||||
#mtr=(mtr-mn)/(mx-mn)
|
||||
|
||||
#_log.debug(f'{i} {mtr[i,:]}')
|
||||
#for i in range(mtr.shape[1]):
|
||||
# self._plt[i].setData(mtr[:,i])
|
||||
|
||||
## Start Qt event loop unless running in interactive mode.
|
||||
if __name__=='__main__':
|
||||
|
||||
Reference in New Issue
Block a user