Code indexing in gitaly is broken and leads to code not being visible to the user. We work on the issue with highest priority.

Skip to content
Snippets Groups Projects
Commit abc43fe2 authored by tekin_g's avatar tekin_g
Browse files

add PSF processing for basler

parent 17886b3d
No related branches found
No related tags found
No related merge requests found
import pickle
from helpers.image import show_image,read_y10
from helpers.output import save_array,load_array
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
"""
this script is for actually determining the PSF for each measurement. For each measurement it will do:
1) It finds the brightest point in the image and cuts a square with (2*delta+1) side length around the point.
2) It will show the whole image and teh cut portion of the image. You can accept by [ENTER] or press c to cancel
3) It will show the normed version of the cropped image (L1 norm). You can accept by [ENTER] or press c to cancel
4) It will save the image
"""
delta = 3# box size
detail_display = False # display the position of the brightest point on the whole image
root = ("/home/guney/Project/Richardson-Lucy-Net/helper scripts/data/psf2")
dirs = os.listdir(root)
for i,path in enumerate(dirs): # iterate over the measurements
path = root + "/" + path+"/"
print(path)
try:
image = load_array(path+"avg_image.npz")
except FileNotFoundError:
continue
max_i = np.unravel_index(np.argmax(image),image.shape) # find the brightest point
print("Maximal Brightness at {}".format(max_i))
y_min, y_max = max_i[0]-delta,max_i[0]+delta+1 #determine the box around the point
x_min, x_max = max_i[1]-delta,max_i[1]+delta+1
top_left_point = (x_min,y_min)
bottom_right_point = (x_max,y_max)
if detail_display:
im_2 = np.copy(image)
cv2.rectangle(im_2,top_left_point,bottom_right_point,1.0,4)
show_image(im_2,"avg",norm=(0,1))
cropped = image[y_min:y_max, x_min:x_max]
#print(np.unravel_index(np.argmax(cropped),cropped.shape))
show_image(cropped,"avg_cropped",norm=(0,4))
#print(cropped.shape)
if cv2.waitKey() == ord('c'):
print("exiting")
cv2.destroyAllWindows()
exit(0)
avg_image = cropped
normed = np.zeros(np.shape(avg_image), dtype=np.float64)
cv2.normalize(avg_image,normed,1.0,0,cv2.NORM_L1) # normalize to L! norm
show_image(normed,"avg_L1",norm=(0,3))
plt.imshow(normed)
plt.savefig(path+"psf.png")
plt.show()
print("L1 norm is {}".format(np.sum(normed)))
if cv2.waitKey() == ord('c'):
print("exiting")
cv2.destroyAllWindows()
exit(0)
plt.figure()
plt.imshow(normed)
plt.savefig(path+"psf.png")
plt.show()
crop_limits = {"y_max" : y_max, "y_min" : y_min, "x_max" : x_max, "x_min" :x_min}
with open(path+'crop_limits.pickle', 'wb') as handle:
pickle.dump(crop_limits, handle, protocol=pickle.HIGHEST_PROTOCOL)
#cv2.imwrite(path+"psf.png",normed)
np.save(path+"cropped.npy",cropped)
np.save(path+"psf.npy",normed)
cv2.destroyAllWindows()
\ No newline at end of file
## How to Determine the PSF
The PSF is used to generate synthetic data to be used during the training process. Using the PSF we can model the effects of the camera on real world objects. Thus we can generate realistic input images from ground truths.
### Measurement
We did the measurements using a laser and A 1.4 um pinhole. This was barely smaller than the pixel size of the CCD. Thus we used the lens in 1:1 magnification mode at ca. 50 cm distance from the pinhole. We took 50 images per measurement and did 8 measurements with the light shining on different parts of the lens.
### Analysis
The code is divided into three parts:
1. preprocess_PSF.py: This combines the 50 pictures into a single averaged picture for each measurement. Done only once during the process.
2. PSF.py: This script actually calculates the PSF from the averaged images created in the last step. This is done by cropping the image to a (2\*delta+1 x 2\*delta+1) square around the brightest point and normalizing the output according to the L1 norm.
3. merge_PSF.py: This combines the output of the PSF.py for each measurement into a combined PSF and generates graphs.
Read the relevant scripts for more details
\ No newline at end of file
import os
import numpy as np
from matplotlib import pyplot as plt
import cv2
"""
This script is used to merge the outputs of PSF.py. It merges the outputs by averaging and plots the individual PSFs.
"""
root = ("/home/guney/Project/Richardson-Lucy-Net/helper scripts/data/psf2/")
dirs = os.listdir(root)
b = []
# fig, axs = plt.subplots(2, 4)
for i,path in enumerate(dirs):
# plot_index = np.unravel_index(i-1,axs.shape)
path = root + path+"/"
try:
a = np.load(path+"cropped.npy")
except FileNotFoundError:
continue
print(a.shape)
b.append(a)
# axs[plot_index].set_title("PSF of {}".format(i))
# axs[plot_index].imshow(a)
# fig.show()
b = np.array(b)
b = np.mean(b,axis=0)
normed = np.zeros(np.shape(b), dtype=np.float64)
cv2.normalize(b, normed, 1.0, 0, cv2.NORM_L1)
fig,ax = plt.subplots(1)
ax.set_title("Point Spread Function of the camera")
heatmap = ax.pcolor(normed)
fig.colorbar(heatmap,ax=ax)
fig.show()
print("L1 Norm is {}".format(np.sum(normed)))
np.save("avg_psf",normed)
print(b.shape)
from helpers.image import show_image,read_y10
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from helpers.output import save_array
"""
This script reads PSF measurements in Y10 format and averages the measurements for each position. I.E: It averages over all y10 images in a folder and outputs the average image as npz.
"""
path = ("/home/guney/Project/Richardson-Lucy-Net/helper scripts/data/psf2")
print(os.getcwd())
b = os.listdir(path)
for i in b:
pathi = path + "/" + i+"/"
print(pathi)
a = os.listdir(pathi)
a = [pathi +i for i in a if i.split('.')[-1] == 'bmp']
if len(a) == 0:
continue
image = cv2.imread(a[0], cv2.IMREAD_GRAYSCALE)
# show_image(image,i)
# cv2.waitKey(0)
datas = np.zeros((len(a),image.shape[0],image.shape[1]),dtype=np.float64)
for i ,path_i in enumerate(a):
loaded = cv2.imread(path_i, cv2.IMREAD_GRAYSCALE)
datas[i,:,:] = loaded / ((2**8) - 1)
#datas[i,:,:] = loaded
print(datas.shape)
avg_image = np.mean(datas,axis=0,dtype=np.float64)
print(avg_image.dtype,avg_image.shape)
print(np.max(avg_image),np.mean(avg_image))
# show_image(avg_image,"avg")
# cv2.waitKey(0)
save_array(pathi+"avg_image.npz",avg_image)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment