Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3.1上使用OpenCV的ctypes上的指针_Python_Opencv_Python 3.x_Ctypes - Fatal编程技术网

在Python3.1上使用OpenCV的ctypes上的指针

在Python3.1上使用OpenCV的ctypes上的指针,python,opencv,python-3.x,ctypes,Python,Opencv,Python 3.x,Ctypes,我试图通过ctypes在Python3.1上使用OpenCV,但我不知道如何表示指针。例如,如果我想加载图像并打印她的第一个像素的内容,我将用C++编写: #include <opencv/cv.h> #include <opencv/highgui.h> using namespace std; int main() { IplImage *img; img = cvLoadImage("/home/foo/foo.png"); CvScal

我试图通过ctypes在Python3.1上使用OpenCV,但我不知道如何表示指针。例如,如果我想加载图像并打印她的第一个像素的内容,我将用C++编写:

#include <opencv/cv.h>
#include <opencv/highgui.h>

using namespace std;

int main() {
    IplImage *img;
    img = cvLoadImage("/home/foo/foo.png");
    CvScalar pixel = cvGet2D(img, 20, 30);
    printf(pixel)
    return 0
}

但是我没有CvScalar结构(我不知道如何表示它),我使用了错误的指针ctypes,并且我有一个“segfault”错误。

在OpenCV 2.3中,
CvScalar
结构是一个包装在结构中的
double[4]
,问题中显示的其他结构定义明显不同。另外, CVoLaDimeA//COD>具有一个C++中的缺省值<代码> ISCORE < /COD>参数。当使用ctypes调用时,必须将其设置为
CV\u LOAD\u IMAGE.*
常量之一

from ctypes import *

CV_LOAD_IMAGE_UNCHANGED = -1
CV_LOAD_IMAGE_GRAYSCALE = 0
CV_LOAD_IMAGE_COLOR = 1
CV_LOAD_IMAGE_ANYDEPTH = 2
CV_LOAD_IMAGE_ANYCOLOR = 4

CvArr = None

class CvScalar(Structure):
    _fields_ = [('val', c_double * 4)]  # e.g BGRA, see channelSeq

class IplRoi(Structure):
    _fields_ = [
      ('coi', c_int),
      ('xOffset', c_int),
      ('yOffset', c_int),
      ('width', c_int),
      ('height', c_int),
    ]

class IplImage(Structure): pass
IplImage._fields_ = [
      ('nSize', c_int),
      ('ID', c_int),
      ('nChannels', c_int),
      ('alphaChannel', c_int),
      ('depth', c_int),
      ('colorModel', c_char * 4),
      ('channelSeq', c_char * 4),
      ('dataOrder', c_int),
      ('origin', c_int),
      ('align', c_int),
      ('width', c_int),
      ('height', c_int),
      ('roi', POINTER(IplRoi)),
      ('maskROI', POINTER(IplImage)),
      ('imageId', c_void_p),
      ('tileInfo', c_void_p),
      ('imageSize', c_int),
      ('imageData', POINTER(c_char)),
      ('widthStep', c_int),
      ('BorderMode', c_int * 4),
      ('BorderConst', c_int * 4),
      ('imageDataOrigin', POINTER(c_char)),
    ]

为了进行测试,我在中使用了2.3 OpenCV LIB,并使用了以下4通道PNG:

cv = CDLL('libopencv_core.so.2.3')
highgui = CDLL('libopencv_highgui.so.2.3')

highgui.cvLoadImage.restype = POINTER(IplImage)
highgui.cvLoadImage.argtypes = [c_char_p, c_int]

cv.cvGet2D.restype = CvScalar
cv.cvGet2D.argtypes = [POINTER(CvArr), c_int, c_int]

pimg = highgui.cvLoadImage(
    b'data/rgba8_trans.png', CV_LOAD_IMAGE_UNCHANGED)
pixsc = cv.cvGet2D(pimg, 30, 30)

img = pimg[0]
pix = tuple(pixsc.val)
print('width = %d, height = %d\n' % (img.width, img.height))
print('y=30, x=30')
print(*zip(img.channelSeq.decode(), pix))

输出:

width = 85, height = 62

y=30, x=30
('B', 128.0) ('G', 128.0) ('R', 64.0) ('A', 176.0)

在OpenCV 2.3中,
CvScalar
结构是一个包装在结构中的
double[4]
,问题中显示的其他结构定义有很大不同。另外, CVoLaDimeA//COD>具有一个C++中的缺省值<代码> ISCORE < /COD>参数。当使用ctypes调用时,必须将其设置为
CV\u LOAD\u IMAGE.*
常量之一

from ctypes import *

CV_LOAD_IMAGE_UNCHANGED = -1
CV_LOAD_IMAGE_GRAYSCALE = 0
CV_LOAD_IMAGE_COLOR = 1
CV_LOAD_IMAGE_ANYDEPTH = 2
CV_LOAD_IMAGE_ANYCOLOR = 4

CvArr = None

class CvScalar(Structure):
    _fields_ = [('val', c_double * 4)]  # e.g BGRA, see channelSeq

class IplRoi(Structure):
    _fields_ = [
      ('coi', c_int),
      ('xOffset', c_int),
      ('yOffset', c_int),
      ('width', c_int),
      ('height', c_int),
    ]

class IplImage(Structure): pass
IplImage._fields_ = [
      ('nSize', c_int),
      ('ID', c_int),
      ('nChannels', c_int),
      ('alphaChannel', c_int),
      ('depth', c_int),
      ('colorModel', c_char * 4),
      ('channelSeq', c_char * 4),
      ('dataOrder', c_int),
      ('origin', c_int),
      ('align', c_int),
      ('width', c_int),
      ('height', c_int),
      ('roi', POINTER(IplRoi)),
      ('maskROI', POINTER(IplImage)),
      ('imageId', c_void_p),
      ('tileInfo', c_void_p),
      ('imageSize', c_int),
      ('imageData', POINTER(c_char)),
      ('widthStep', c_int),
      ('BorderMode', c_int * 4),
      ('BorderConst', c_int * 4),
      ('imageDataOrigin', POINTER(c_char)),
    ]

为了进行测试,我在中使用了2.3 OpenCV LIB,并使用了以下4通道PNG:

cv = CDLL('libopencv_core.so.2.3')
highgui = CDLL('libopencv_highgui.so.2.3')

highgui.cvLoadImage.restype = POINTER(IplImage)
highgui.cvLoadImage.argtypes = [c_char_p, c_int]

cv.cvGet2D.restype = CvScalar
cv.cvGet2D.argtypes = [POINTER(CvArr), c_int, c_int]

pimg = highgui.cvLoadImage(
    b'data/rgba8_trans.png', CV_LOAD_IMAGE_UNCHANGED)
pixsc = cv.cvGet2D(pimg, 30, 30)

img = pimg[0]
pix = tuple(pixsc.val)
print('width = %d, height = %d\n' % (img.width, img.height))
print('y=30, x=30')
print(*zip(img.channelSeq.decode(), pix))

输出:

width = 85, height = 62

y=30, x=30
('B', 128.0) ('G', 128.0) ('R', 64.0) ('A', 176.0)