Skip to content

Instantly share code, notes, and snippets.

@amarvutha
Last active August 29, 2015 14:00
Show Gist options
  • Save amarvutha/11400062 to your computer and use it in GitHub Desktop.
Save amarvutha/11400062 to your computer and use it in GitHub Desktop.
Acquiring a webcam image and slicing it, for an atomic beam camera
# Display webcam image from beamline monitor
# version 3, 2014-04-25
# Amar
import numpy as np
import cv2
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.figure import Figure
import matplotlib.gridspec as gridspec
class Camera:
def __init__(self,channel=0):
self.capture = cv2.VideoCapture(channel)
self.success,self.image = self.capture.read()
print "Beginning acquisition ...",
def acquire(self):
# Acquire image
self.success,self.image = self.capture.read()
print ".",
def close(self):
if self.capture.isOpened(): self.capture.release()
print "released camera"
camera = Camera()
camera.capture.set(15,-3)
camera.capture.set(16,0.01)
"""
Camera parameters:
--------------------
1 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
2 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
3 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
4 CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
5 CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
6 CV_CAP_PROP_FPS Frame rate.
7 CV_CAP_PROP_FOURCC 4-character code of codec.
8 CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
9 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
10 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
11 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
12 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
13 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
14 CV_CAP_PROP_HUE Hue of the image (only for cameras).
15 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
16 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
17 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
18 CV_CAP_PROP_WHITE_BALANCE Currently unsupported
19 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
"""
# Setting up the plot
fig = plt.figure()
aspectRatio = 6
fig.canvas.set_window_title('beam camera')
gs = gridspec.GridSpec(aspectRatio,aspectRatio)
ax1 = plt.subplot(gs[0,:-1])
ax2 = plt.subplot(gs[1:,:-1])
ax3 = plt.subplot(gs[1:,-1])
plt.subplots_adjust(left=0.10,bottom=0.10,wspace=0.02, hspace=0.02)
ax1.xaxis.set_visible(False)
ax1.yaxis.set_visible(False)
ax3.xaxis.set_visible(False)
ax3.yaxis.set_visible(False)
ax1.set_xlim(0,640)
ax3.set_ylim(480,0)
### Initial plot ###
image = 0.5*camera.image[:,:,0] + 0.5*camera.image[:,:,1]
oldImage = camera.image
i,j = np.unravel_index(image.argmax(), image.shape)
print i,j
x = image[i] #x slice along maximum
y = image.T[j] #y slice along maximum
xslice, = ax1.plot(range(640),x,'b',linewidth=3,alpha=0.7)
im = ax2.imshow(image)
yslice, = ax3.plot(y[::-1],range(480,0,-1),'b',linewidth=3,alpha=0.7)
text_template = 'x0 = %i, y0 = %i'
text = ax2.text(300,40,text_template%(j,i),family='monospace',fontsize=16,weight='heavy',color='white')
#### Animation routine ###
def updatefig(*args):
camera.acquire()
image = 0.5*camera.image[:,:,0] + 0.5*camera.image[:,:,1]
if camera.success:
image = 0.5*camera.image[:,:,0] + 0.5*camera.image[:,:,1]
oldImage = camera.image
i,j = np.unravel_index(image.argmax(), image.shape)
print i,j
x = image[i] #x slice along maximum
y = image.T[j] #y slice along maximum
im.set_array(image)
xslice.set_ydata(x)
ax1.set_ylim(0,x.max())
yslice.set_xdata(y[::-1])
ax3.set_xlim(0,y.max())
text.set_text(text_template%(j,i))
else: print "error"
return (im,) + (xslice,) + (yslice,) + (text,)
def init(): #Init only required for blitting to give a clean slate.
im.set_array(np.zeros(oldImage.shape))
xslice.set_ydata(np.zeros((640,)))
yslice.set_xdata(np.zeros((480,)))
text.set_text("")
return (im,) + (xslice,) + (yslice,) + (text,)
ani = animation.FuncAnimation(fig, updatefig, init_func=init, interval=80, blit=True)
plt.ioff()
plt.show()
camera.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment