Skip to content

Instantly share code, notes, and snippets.

@sriharijayaram5
Created March 26, 2020 19:50
Show Gist options
  • Save sriharijayaram5/b4ad7d1dca4f03aebd69ec611eb0a90d to your computer and use it in GitHub Desktop.
Save sriharijayaram5/b4ad7d1dca4f03aebd69ec611eb0a90d to your computer and use it in GitHub Desktop.
Zeeman effect - image intensity line
import cv2
from matplotlib import pyplot as plt
import numpy as np
from skimage import draw
from scipy.signal import find_peaks
w = int(3648/4)
h = int(2736/4)
# Actual mouse callback function
def print_coords(event, x, y, flags, param):
# Global variables needed
global image, image_copy, r_start, c_start, data
# If left mouse button is clicked, start of line
if (event == cv2.EVENT_LBUTTONDOWN):
# r_start = 505
# c_start = 347
r_start = x
c_start = y
# If left mouse button is clicked, end of line; plot intensity profile
if (event == cv2.EVENT_LBUTTONUP):
r_end = 0#int(x+(w/3))
c_end = 0#int(y+(h/3))
image = cv2.line(image_copy.copy(), (r_start, c_start), (r_end, c_end), (255, 255, 255), 2)
line = np.transpose(np.array(draw.line(r_start, c_start, r_end, c_end)))
data = image_copy.copy()[line[:, 1], line[:, 0], :]
plt.close()
plt.figure('Intensity profile')
plt.xlabel('px from centre', fontname='Calibri')
plt.ylabel('Intensity', fontname='Calibri')
# plt.plot(data[:, 0], 'b', data[:, 1], 'g', data[:, 2], 'r')
plt.plot(data[:, 2], 'r-')
# a=find_peaks(data[:, 2],height=5,distance=10)[0]#indices of peaks
# plt.plot(data[:, 2][a], 'bo')
plt.draw()
plt.pause(0.001)
plt.legend(['Red'])
plt.ylim((0, 255))
# Read an image
loc = 'Transverse Mag field/CIMG4147.JPG'
image = cv2.imread(loc, cv2.IMREAD_COLOR)
image = cv2.resize(image, (w,h))
image_copy = image.copy()
# Set up window and mouse callback function
cv2.namedWindow("image")
cv2.setMouseCallback("image", print_coords)
# Loop until the 'c' key is pressed
while True:
# Display image; wait for keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
# If 'c' key is pressed, break from loop
if key == ord("c"):
break
# plt.plot(data[:, 2], 'r')
# a=find_peaks(data[:, 2],height=5,distance=10)[0]#indices of peaks
# plt.plot(data[:, 2][a], 'bo')
# plt.draw()
# plt.show()
plt.close()
y = data[:,2]
x = range(len(y))
plt.xlabel('px from centre', fontname='Calibri')
plt.ylabel('Intensity', fontname='Calibri')
plt.plot(x, y, 'b-o', markersize = 1.2)
indices=find_peaks(y,height=5,distance=10)[0]#indices of peaks
x1 = []
x1[:] = [x[i] for i in indices]
y1 = []
y1[:] = [y[i] for i in indices]
for i,j in enumerate(x1):
# plt.text(j,y1[i]+10,f'{j}')
pass
plt.plot(x1, y1, 'ro', markersize = 1.0)
plt.show()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment