小编mcl*_*lzc的帖子

从Python调用C++函数并将OpenCV Mat转换为Numpy数组

背景情况

我试图通过Python绑定使用OpenCV Stitching模块,但是我收到一个错误:

import cv2
stitcher = cv2.createStitcher(False)

imageL = cv2.imread("imageL.jpg")
imageC = cv2.imread("imageC.jpg")
imageR = cv2.imread("imageR.jpg")

stitcher.stitch((imageL, imageC))
Run Code Online (Sandbox Code Playgroud)

错误:/home/user/OpenCV3.1.0/opencv/modules/python/src2/cv2.cpp:163:错误:( - 215)数据通常应为NULL!在函数分配

类似的人遭受这种情况

手头的问题

所以我决定使用官方的C++ OpenCV拼接示例并使用Python使用Boost.Python来调用它.但是,我仍然无法弄清楚如何正确使用Boost.Python + numpy-opencv-converter来处理C++ Mat vs Numpy数组转换.

¿如何调用numpy-opencv-converter?我只有Boost.Python到位,当运行我的python函数来调用C++文件时,我得到了这个(预期的)结果:

$ python python_caller.py 
Traceback (most recent call last):
  File "python_caller.py", line 10, in <module>
    visualize(A)
Boost.Python.ArgumentError: Python argument types in
    testing.visualize(numpy.ndarray)
did not match C++ signature:
    visualize(cv::Mat)
Run Code Online (Sandbox Code Playgroud)

谢谢.

PD:我在Ubuntu 14.04,Python 2.7.4使用OpenCV 3.1.0从源代码编译并在virtualenv内部编译.


这些是我正在使用的文件.

testing.cpp:

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <boost/python.hpp>

using namespace …
Run Code Online (Sandbox Code Playgroud)

c++ python boost opencv numpy

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

将Windows中的文件传递到python脚本时,我的\ r删除了我的字符

我有一个像这样的文件:

A\r
B\n
C\r\n.
Run Code Online (Sandbox Code Playgroud)

(通过\ r,我指的是CR,而\ n是LF)

这个脚本:

import fileinput
for line in fileinput.input(mode='rU'):
    print(line)
Run Code Online (Sandbox Code Playgroud)

当我打电话时,python script.py myfile.txt我得到正确的输出:

A
B
C
Run Code Online (Sandbox Code Playgroud)

但是当我这样称呼它时type myfile.txt|python script.py,我得到了:

B
C
Run Code Online (Sandbox Code Playgroud)

你看?没有更多的“ A”。

怎么了?我认为mode='rU'它将解决所有换行问题。

编辑:在Python 3中没有这样的问题!仅在Python 2中。但这不能解决问题。

谢谢

编辑:

仅出于完整性考虑。-在Linux中也是如此。

  • Python 3对用户透明地处理每种换行类型(\ n,\ r或\ r \ n)。您的文件有哪一个都无所谓,您不必担心。
  • Python 2需要将参数mode ='rU'传递给fileinput.input,以使其能够透明地处理每个换行符。问题是,在Python 2中,当将内容传递给它时,它不能正常工作。尝试通过管道传输这样的文件:

    CR: \r
    LF: \n
    CRLF: \r\n
    
    Run Code Online (Sandbox Code Playgroud)

Python 2只是将这两行视为一行,如果您尝试使用以下代码打印每一行:

for i,line in enumerate(fileinput.input(mode='rU')):
    print("Line {}: {}".format(i,line), end='')
Run Code Online (Sandbox Code Playgroud)

它输出:

Line 0: CR:
LF:
Line 1: CRLF:
Run Code Online (Sandbox Code Playgroud)

在Python 3中不会发生这种情况。这里有2行不同。当将此文本作为文件传递时,它可以正常工作。

像这样管道数据:

LF: …
Run Code Online (Sandbox Code Playgroud)

python newline carriage-return linefeed

5
推荐指数
0
解决办法
131
查看次数

消除高度相关的列,但保留我的非数字列

我有一个包含分类变量和数值变量的数据框。我想摆脱高度相关的变量。

所以,首先我去掉了所有的分类列,制作了矩阵并找出哪些数字列可以消除。现在我想回去,也有我的分类变量列。

我对最简单的方法感到困惑......也许是一个连接选择我想从第二个表中添加哪些列......

library(caret)
training_2 <- subset(training, select = -c(user_name ,timestamp, etc))
corr_matrix <- cor(training_2)
highCorr <- findCorrelation(corr_matrix, .75)
training_2<- training_2[,-highCorr]
Run Code Online (Sandbox Code Playgroud)

编辑:数据

structure(list(X = 1:15, yaw_belt = c(-94.4, -94.4, -94.4, -94.4, 
-94.4, -94.4, -94.4, -94.4, -94.4, -94.4, -94.4, -94.4, -94.4, 
-94.4, -94.4), gyros_belt_x = c(0, 0.02, 0, 0.02, 0.02, 0.02, 
0.02, 0.02, 0.02, 0.03, 0.03, 0.02, 0.02, 0.02, 0), gyros_belt_y = c(0, 
0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), gyros_belt_z = c(-0.02, 
-0.02, …
Run Code Online (Sandbox Code Playgroud)

r correlation

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

标签 统计

python ×2

boost ×1

c++ ×1

carriage-return ×1

correlation ×1

linefeed ×1

newline ×1

numpy ×1

opencv ×1

r ×1