views:

14

answers:

0

Hi,

i try to use OpenCV on Python 3.1 through ctypes, but I do not know how represent pointers. Example, if I want to load an image and print the content of her first pixel, i will write in 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
}

But in Python, on ctypes, I must to represent IplImage, CvScalar structures and more, I must to use "pointers", to do this, for example, : IplImage *img;.

But how ? I tried :

from ctypes import *

cv = cdll.LoadLibrary("libcv.so")
highgui = cdll.LoadLibrary("libhighgui.so")

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

class IplImage(Structure):
    _fields_ = [("nChannels", c_int),
                ("depth", c_int),
                ("width", c_int),
                ("height", c_int),
                ("imageData", c_char_p),
                ("dataOrder", c_int),
                ("origin", c_int),
                ("widthStep", c_int),
                ("imageSize", c_int),
                (IplRoi(), c_char_p),
                ("imageDataOrigin", c_char_p),
                ("align", c_int)]

image = IplImage(highgui.cvLoadImage("/home/michael/connerie.jpg"))
image = byref(image)
cv.cvGet2D(image, 1, 1)

But I do not have the CvScalar structure (I do not kown how represent it) and I'm using bad pointers ctypes, I have a "segfault" error.

Best regards,

tcpc