Skip to content
Snippets Groups Projects
Unverified Commit cb483066 authored by HanBrady's avatar HanBrady Committed by GitHub
Browse files

Merge pull request #15 from HanBrady/laser_sim

Laser sim
parents 07086907 1d117788
No related branches found
No related tags found
No related merge requests found
# Simple script to produce graphs of laser beam profiles from images
# Read across a line of pixels and plot
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
im = Image.open('laser_beam.jpg') # read in laser data
np_data = np.asarray(im)
plot = np_data[253, :, 0]
plt.plot(plot)
print(np.size(plot))
plt.savefig('laser_profile')
plt.show()
"""Simulated laser data"""
# Generate matrices of pixels
# Gaussian power output
# can change shape
## axis = tight
import matplotlib.pyplot as plt
import numpy as np
def gauss(xx, yy):
"""Gaussian function to map pixels
Args:
xx (NDArray): meshgrid of x pixels
yy (NDArray): meshgrid of y pixels
Returns:
variable: map of pixels with gaussian function
"""
gauss = np.exp(-(xx**2 + yy**2))
return gauss
def pixels(xstart, xstop, xsteps, ystart, ystop, ysteps):
"""Creates meshgrid of pixels
Args:
start (int): Start of pixel matrix
stop (int): End of pixel matrix
steps (int): Steps between start and stop
Returns:
NDarrays: Meshgrid of pixels
"""
x = np.linspace(xstart, xstop, xsteps)
y = np.linspace(ystart, ystop, ysteps)
xx, yy = np.meshgrid(x, y)
return xx, yy
xx, yy = pixels(-5, 5, 250, -5, 5, 250)
zz = gauss(xx, yy)
plt.imshow(zz, cmap='Greys_r', aspect='equal') #, vmin=-25, vmax=25) #axis needs to remain consistant
#plt.imshow(yy)
plt.show()
plt.imsave('laser_sim.png', zz, cmap='Greys_r') # gives grey colourmap
#save image to feed to lbs
\ No newline at end of file
# Testing laserbeamsize module
import imageio.v3 as iio
import laserbeamsize as lbs
from PIL import Image
def dithered(image_in):
"""Takes image and converts to dithered black and white
Args:
image_in (string): location of image
Returns:
ndarray: b&w dithered image
"""
img = Image.open(image_in)
r = img.convert('1') # Dithered b&w
r.save('foo.png')
image = iio.imread('foo.png')
return image
def beam_width(image):
"""Uses laserbeamsize module to give beam width in pixels from a monochrome image
Args:
image (string): location of image
"""
x, y, dx, dy, phi = lbs.beam_size(image)
print("The centre of the beam ellipse is at (%.0f, %.0f)" % (x, y))
print("The ellipse diameter (closest to horizontal) is %.0f pixels" % dx)
print("The ellipse diameter (closest to vertical) is %.0f pixels" % dy)
print("The ellipse is rotated %.0f degrees ccw from the horizontal" % (phi * 180/3.1416))
image = dithered('laser_sim.png')
beam_width(image)
# Trials of monochrome images
import imageio.v3 as iio
from PIL import Image
def black_white(image_in):
img = Image.open(image_in)
thresh = 200
fn = lambda x : 255 if x > thresh else 0
r = img.convert('L').point(fn, mode='1') # bw
r.save('foo.png')
image = iio.imread('foo.png')
return image
def dithered(image_in):
img = Image.open(image_in)
r = img.convert('1') # Dithered b&w
r.save('foo.png')
image = iio.imread('foo.png')
return image
dithered('laser_sim.png')
#black_white('laser_sim.png')
\ No newline at end of file
# Testing laserbeamsize module
import imageio.v3 as iio
import laserbeamsize as lbs
from PIL import Image
#im = iio.imread("laser_pic.jpg")
im = '22.07_laserpic3.png'
#Need to convert to monochrome image
def monochrome(image_in):
"""Uses PIL to turn RGB image into monochrome (b&w)
laserbeamsize only takes monochrome images
Args:
image_in (str): name of image to convert to b&w
Returns:
read image file: image ready to be used by laserbeamsize
"""
img = Image.open(image_in)
thresh = 200
fn = lambda x : 255 if x > thresh else 0
#r = img.convert('L').point(fn, mode='1') # just black and white
r = img.convert('1') # Dithered b&w
r.save('foo.png')
image = iio.imread('foo.png')
return image
image = monochrome(im)
x, y, dx, dy, phi = lbs.beam_size(image)
print("The centre of the beam ellipse is at (%.0f, %.0f)" % (x, y))
print("The ellipse diameter (closest to horizontal) is %.0f pixels" % dx)
print("The ellipse diameter (closest to vertical) is %.0f pixels" % dy)
print("The ellipse is rotated %.0f degrees ccw from the horizontal" % (phi * 180/3.1416))
\ No newline at end of file
# Comparison of manual width measurement to lbs
# WIP
#! Currently working only with simulated data!
#! Will not work for all y axis
import matplotlib.pyplot as plt
import numpy as np
import imageio.v3 as iio
import laserbeamsize as lbs
from PIL import Image
## Read in image as numpy array
#im_in = Image.open('laser_sim.png') # read in laser data
#im = np.asarray(im_in) # Could do this in one line with cv2?
## Locate peak x and y
def beam_width_manual(image, axis_in):
"""Determine beam width from image
Args:
image (str): image of laser beam
axis_in (str): horizontal (x) or vertical (y) width
Returns:
int: Beam width in pixels
"""
im_in = Image.open(image) # read in laser data
im = np.asarray(im_in)
print(im)
if axis_in == 'x':
axis = 0
elif axis_in == 'y':
axis = 1
else:
raise ValueError('Please input either x or y for axis_in')
max_point = np.argmax(im, axis) # find coord at peak
peak = np.max(max_point)
#Plot line of pixels with max
plot = im[peak, :, 0]
plt.plot(plot)
#determine fwhm
max_val = plot[peak]
hm = int(max_val/2)
hmx = plt.axhline(hm, color = 'black')
plt.show()
index1 = np.where(plot >= hm)
index = index1[0]
width = index[-1] - index[0]
print(width)
return width
def dithered(image_in):
"""Takes image and converts to dithered black and white
Args:
image_in (string): location of image
Returns:
ndarray: b&w dithered image
"""
img = Image.open(image_in)
r = img.convert('1') # Dithered b&w
r.save('foo.png')
image = iio.imread('foo.png')
return image
def beam_width(image):
"""Uses laserbeamsize module to give beam width in pixels from a monochrome image
Args:
image (string): location of image
"""
x, y, dx, dy, phi = lbs.beam_size(image)
print("The centre of the beam ellipse is at (%.0f, %.0f)" % (x, y))
print("The ellipse diameter (closest to horizontal) is %.0f pixels" % dx)
print("The ellipse diameter (closest to vertical) is %.0f pixels" % dy)
print("The ellipse is rotated %.0f degrees ccw from the horizontal" % (phi * 180/3.1416))
beam_width_manual('laser_beam.jpg', 'x')
#beam_width_manual('22.07_laserpic3.png', 'y')
## Compare with lbs
image = dithered('laser_beam.jpg')
beam_width(image)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment