我正在使用OpenCV(2.4)和Python(2.7.3)以及Thorlabs的USB摄像头(DC1545M).
我正在对视频流进行一些图像分析,我希望能够从我的视频流中更改一些摄像机参数.令人困惑的是,我能够改变一些相机属性而不是所有相机属性,我不确定我做错了什么.
这是代码,使用Python中的cv2绑定,我可以确认它运行:
import cv2
#capture from camera at location 0
cap = cv2.VideoCapture(0)
#set the width and height, and UNSUCCESSFULLY set the exposure time
cap.set(3,1280)
cap.set(4,1024)
cap.set(15, 0.1)
while True:
ret, img = cap.read()
cv2.imshow("input", img)
#cv2.imshow("thresholded", imgray*thresh2)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
Run Code Online (Sandbox Code Playgroud)
作为参考,cap.set()命令中的第一个参数引用了相机属性的枚举,如下所示:
0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video …Run Code Online (Sandbox Code Playgroud) 我试图在个人网站上静态嵌入散景图,并遇到一些我不理解的行为.基本上,我使用散景生成一个情节如下:
import bokeh.plotting as bplt
import numpy as np
x=np.random.random(100)
y=np.random.random(100)
bplt.output_file("t.html")
plot=bplt.line(x,y)
##the following line refers to the bokeh installed on my home computer
print plot.create_html_snippet(
static_path='/usr/local/lib/python2.7/site-packages/bokeh/server/static/')
##the following line refers to the bokeh installed on my remote computer
#print plot.create_html_snippet(
# static_path='/opt/anaconda/lib/python2.7/site-packages/bokeh/server/static/')
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.这会生成一个看起来像的文件(random garbage).embed.js,并打印一个包含html语法的字符串,我手动将其复制到我正在调用的html文件中testembed.html,我在下面转载:
<html>
<body>
<h2>Simple Embed Example</h2>
<p>This is where my plot should be:</p>
<p>
<!--The next 4 lines are the output of the print statement from the python code-->
<script src="ccbd451a-6995-4dd2-b99c-e4140b362997.embed.js" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Lomb-Scargle周期图(LSP)和FFT-Power频谱创建一些计算均匀和不均匀采样数据的功率谱的例程.我遇到的问题是,当在scipy中使用LSP实现时,我会遇到使用均匀采样数据的崩溃.
下面的代码工作,并产生几乎相同(和正确)的输出,据我所知.但是,我被迫在Lomb-Scargle功能中插入一个kludge来为频率添加抖动,因此它们与FFT的不完全匹配.当我注释掉那一行时,我得到一个被零除错误.
这是scipy中Lomb-Scargle实现的问题,还是我不应该将它与均匀采样数据一起使用?提前致谢.
import numpy as np
import scipy.signal as sp
import matplotlib.pyplot as plt
def one_sided_fft(t,x):
full_amplitude_spectrum = np.abs(np.fft.fft(x))/x.size
full_freqs = np.fft.fftfreq(x.size, np.mean(np.ediff1d(t)))
oneinds = np.where(full_freqs >=0.0)
one_sided_freqs = full_freqs[oneinds]
one_sided_amplitude_spectrum=2*full_amplitude_spectrum[oneinds]
return one_sided_freqs, one_sided_amplitude_spectrum
def power_spectrum(t,x):
onef, oneamps = one_sided_fft(t,x)
return onef, oneamps**2
def lomb_scargle_pspec(t, x):
tstep = np.mean(np.ediff1d(t))
freqs = np.fft.fftfreq(x.size, tstep)
idxx = np.argsort(freqs)
one_sided_freqs = freqs[idxx]
one_sided_freqs = one_sided_freqs[one_sided_freqs>0]
#KLUDGE TO KEEP PERIODOGRAM FROM CRASHING
one_sided_freqs = one_sided_freqs+0.00001*np.random.random(one_sided_freqs.size)
#THE FOLLOWING LINE CRASHES WITHOUT THE KLUDGE
pgram …Run Code Online (Sandbox Code Playgroud) 我在Python中有两个数组(numpy数组):
a=array([5,7,3,5])
b=array([1,2,3,4])
Run Code Online (Sandbox Code Playgroud)
我希望创建第三个数组,其中每个元素都来自新数组中的b出现a次数,如下所示:
c=array([1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,4,4,4,4,4])
Run Code Online (Sandbox Code Playgroud)
是否有一种快速,numPythonic的方式来做到这一点,最少的循环?我需要在一个相当大的数组中循环使用这个操作数千次,所以我希望它尽可能快.
干杯,迈克
我在 wxpython 中有一些令人困惑的行为。我刚刚将最新版本 (3.0.0.0) 加载到带有 Python 2.6.6 的 RHEL 6.4 中。
大多数事情似乎都有效,但我之前运行带有背景图像的 gui 的代码失败了。所有按钮都可以工作,等等,但背景只是默认的灰色。
我尝试通过运行 Mike Driscoll 的 python 网站 ( http://www.blog.pythonlibrary.org/2010/03/18/wxpython-putting-a-background-image-on-a-panel /),并且遇到了同样的问题,除了默认的灰色背景之外没有其他背景显示(我之前在另一台机器上使用过他的示例,其中有旧版本的 wxpython [2.8.12.1] 没有问题):
import wx
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
self.frame = parent
sizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
for num in range(4):
label = "Button %s" % num
btn = wx.Button(self, label=label)
sizer.Add(btn, 0, wx.ALL, 5)
hSizer.Add((1,1), 1, wx.EXPAND)
hSizer.Add(sizer, 0, wx.TOP, 100)
hSizer.Add((1,1), 0, wx.ALL, 75)
self.SetSizer(hSizer) …Run Code Online (Sandbox Code Playgroud) 我正在使用numpy版本1.14.3和python 2.7.12。
引用此问题,我发现在使用np.zeros和np.empty初始化数组之间,速度有很大不同。但是,输出是相同的。
import numpy as np
r = np.random.random((50, 100, 100))
z = np.zeros(r.shape)
e = np.empty(r.shape)
np.allclose(e, z)
Run Code Online (Sandbox Code Playgroud)
这返回True。但是,计时功能%timeit给出了截然不同的结果:
%timeit z = np.zeros(r.shape)
Run Code Online (Sandbox Code Playgroud)
10000次循环,最佳3:每个循环143 µs
%timeit e = np.empty(r.shape)
Run Code Online (Sandbox Code Playgroud)
1000000循环,最佳3:每个循环1.83 µs
上面引用的先前接受的答案说,这np.zeros始终是更好的选择,并且它也更快。
为什么不使用比np.zeros快80倍并返回相同答案的np.empty?
编辑
正如user2285236指出,翻转初始化的顺序z和e将打破平等,因为它会覆盖在相同的存储区。