小编Chr*_*ris的帖子

imread in pylab vs opencv:返回完全不同的数组值

我得到的行为我不太明白:

In [1]: import cv2

In [2]: pylab_img=pylab.imread('lena.jpg')

In [3]: cv_img=cv2.imread('lena.jpg')

In [4]: pylab_img[200,200,:]
Out[4]: array([228, 197, 176], dtype=uint8)

In [5]: cv_img[200,200,:]
Out[5]: array([ 84,  48, 132], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

两个版本都imread将相同的图像读入相同数据类型的numpy数组,但值不匹配.如果这些值刚刚混淆了,我可以将其归结为opencv使用BGR,而matplotlib(pylab)使用RGB,但这似乎并不能解释这种差异.

有什么想法吗?

python opencv numpy matplotlib

9
推荐指数
1
解决办法
3534
查看次数

如何按照pandas中的中位数值对盒子图进行排序

我有一个数据帧outcome2,我用以下方式生成一个分组的boxplot:

In [11]: outcome2.boxplot(column='Hospital 30-Day Death (Mortality) Rates from Heart Attack',by='State')
        plt.ylabel('30 Day Death Rate')
        plt.title('30 Day Death Rate by State')
Out [11]:
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想做的是按照每个州的中位数对地块进行排序,而不是按字母顺序排序.不知道该怎么做.

python matplotlib boxplot pandas

8
推荐指数
1
解决办法
3203
查看次数

为python类使用'get function'有什么好处?

例如,在下面的代码中,getName函数的好处是什么?

class Node(object):
    def __init__(self, name):
        self.name = str(name)
    def getName(self):
        return self.name
    def __str__(self):
        return self.name
Run Code Online (Sandbox Code Playgroud)

python class getter-setter

7
推荐指数
2
解决办法
1459
查看次数

是什么让C比Python更快?

我知道这可能是一个非常明显的答案,而且我将自己暴露给不那么有用的讽刺评论,但我不知道答案所以这里就是这样.

如果Python在运行时编译为字节码,那么只需要花费更长时间的初始编译步骤吗?如果是这样的话,那么代码中的前期成本就不会那么小(即如果代码运行的时间很长,那么C和python之间的差异是否会减少?)

c python compilation

7
推荐指数
2
解决办法
5028
查看次数

在Autoform中使用对象作为选项

在我的Stacks架构中,我有一个dimensions定义如下的属性:

dimensions: {
    type: [String],
    autoform: {
        options: function() {
            return Dimensions.find().map(function(d) {
                return { label: d.name, value: d._id };
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这非常有效,并且使用Mongol我能够看到通过表单插入数据的尝试运行良好(在这种情况下,我选择了两个要插入的维度)

蒙古形象

然而,我真正的是存储实际维度对象而不是密钥的数据.像这样的东西:

[蒙古好[2]

要努力实现这一点,我改变了type:[String]type:[DimensionSchema]value: d._idvalue: d.这里的想法是我告诉表格我期待一个对象,现在我正在返回对象本身.

但是当我运行它时,我在控制台中收到以下错误.

Meteor当前不支持ObjectID以外的对象作为ID

稍微调整一下并type:[DimensionSchema]改为type: DimensionSchema我在控制台中看到一些新错误(可能是当它type是一个阵列时它们会被埋没

控制台图像

所以似乎autoform试图获取我想要存储在数据库中的值并尝试将其用作id.有关最佳方法的任何想法吗?

这里参考是我的 DimensionSchema

export const DimensionSchema = new SimpleSchema({
    name: {
        type: String,
        label: "Name"
    },
    value: {
        type: Number,
        decimal: true,
        label: "Value",
        min: 0

    },
    tol: …
Run Code Online (Sandbox Code Playgroud)

mongodb meteor meteor-autoform meteor-collection2 simple-schema

6
推荐指数
1
解决办法
230
查看次数

如何确保Python while循环需要特定的时间才能运行?

我正在使用while循环读取串行数据.但是,我无法控制采样率.

代码本身似乎需要花费0.2秒才能运行,所以我知道我将无法以更快的速度运行.但我希望能够准确控制我采样的速度.

我觉得我可以使用'sleep'来做到这一点,但问题是有可能在不同的点上循环本身需要更长的时间来读取(取决于通过串行数据传输的精确内容),所以代码会有弥补平衡.

例如,假设我想每1秒采样一次,并且循环需要0.2s到0.3s才能运行.我的代码需要足够智能才能睡眠0.8秒(如果循环需要0.2秒)或0.7秒(如果循环需要0.3秒).

import serial
import csv
import time

#open serial stream
    while True:

        #read and print a line
        sample_value=ser.readline()
        sample_time=time.time()-zero
        sample_line=str(sample_time)+','+str(sample_value)
        outfile.write(sample_line)
        print 'time: ',sample_time,', value: ',sample_value
Run Code Online (Sandbox Code Playgroud)

python serial-port while-loop pyserial python-2.7

5
推荐指数
1
解决办法
5228
查看次数

cv2.calcOpticalFlowPyrLK的输入图像的数据类型错误

我正在使用python绑定运行opencv 2.4.1,并且很难计算光流.

特别是这部分代码:

#calculate the opticalflow
if prev_saturation_thresh_img==None:
    prev_saturation_thresh_img=saturation_img
if i >=0:
    prev_img=prev_saturation_thresh_img
    next_img=saturation_thresh_img
    p1,  st, err = cv2.calcOpticalFlowPyrLK(prev_img,next_img,tracks_np,**lk_params)
Run Code Online (Sandbox Code Playgroud)

返回错误:

<unknown> is not a numpy array
Run Code Online (Sandbox Code Playgroud)

那么我尝试将图像转换为numpy数组:

prev_img=prev_saturation_thresh_img
next_img=saturation_thresh_img  
Run Code Online (Sandbox Code Playgroud)

现在我有一个新错误:

<unknown> data type = 17 is not supported
Run Code Online (Sandbox Code Playgroud)

在最后的努力中,我将图像转换为cvmat(来自iplimage),然后将其转换为numpy数组,只是为了看看会发生什么

error: ..\..\..\OpenCV-2.4.1\modules\video\src\lkpyramid.cpp:607: error: (-215) nextPtsMat.checkVector(2, CV_32F, true) == npoints
Run Code Online (Sandbox Code Playgroud)

所以现在我被卡住了.以下是完整的代码供参考

import cv
import cv2
import numpy as np

class Target:
    def __init__(self):
        self.capture = cv.CaptureFromFile("raw_gait_cropped.avi")

    def run(self):
        #initiate font
        font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)

        #instantiate images
        img_size=cv.GetSize(cv.QueryFrame(self.capture))
        hsv_img=cv.CreateImage(img_size,8,3) …
Run Code Online (Sandbox Code Playgroud)

python opencv opticalflow

4
推荐指数
1
解决办法
1万
查看次数

在python中拆分十六进制的最佳方法?

我对一般的十六进制很新,我有一个应用程序,需要我分割一个十六进制数.例如,给定数字0x607F,我需要返回高(0x60)或低(0x7F)字节.

这可能是实施,但感觉有点不完整.在python中有更标准的方法吗?

def byte(integer,highlow):
    assert highlow=='high' or highlow=='low'
    if highlow=='high':
        return hex(int(bin(integer)[:-8],2))
    if highlow=='low':
        return hex(int(bin(integer)[-8:],2))
Run Code Online (Sandbox Code Playgroud)

python hex

3
推荐指数
2
解决办法
1万
查看次数

使用 ctypes 将文档字符串添加到从 dll 中提取的 python 函数中

我正在尝试从 Windows dll 创建一堆 python 函数,并且希望将文档字符串附加到每个新函数。

我当前的代码:

import ctypes

lib=ctypes.WinDLL('example.dll')

VCS_OpenDevice=lib['VCS_OpenDevice']
VCS_OpenDevice.restype=ctypes.c_double
Run Code Online (Sandbox Code Playgroud)

当我在解释器中运行此脚本并尝试使用该函数时,我收到“无文档字符串”消息。

不知道在那里添加什么东西。任何帮助表示赞赏。

在此输入图像描述

python dll ctypes docstring

2
推荐指数
1
解决办法
695
查看次数

小写字母或句点的正则表达式,后跟大写字母

最初我只是想找到一个小写字母,然后是一个大写字母,在这种情况下[a-z][A-Z]效果很好。

但是,在某些情况下,小写字母由句点代替。我是regex的新手,却无法找到一种在regex中实现此显式方式的方法(我当前的解决方案通过一些if语句,感觉很不雅致。

正则表达式可以很好地处理吗?

regex

2
推荐指数
1
解决办法
1535
查看次数