1

I am expanding a C-extension to use uses OpenCV, and to start, cvtColor, but I am getting an undefined symbol error. I think it may be me mixing up C and C++, or maybe loading the library incorrectly. I have the error boiled down to this C code:

test.c:

#include <cv.h>

void BGR2HLS(const void * srcv, void * dstv, int elems) {
    const uint8_t * src = (uint8_t *) srcv;
    uint8_t * dst = (uint8_t *) dstv;
    cvtColor(src, dst, CV_BGR2HLS);
}

which I compile using:

gcc -fPIC -shared -o test.so test.c -L/usr/lib/ `pkg-config --cflags opencv` -lopencv_imgproc -lopencv_core

I see /usr/lib/libopencv_imgproc.so exists, and everything compiles without error.

I then try to run this Python script:

import numpy as np
import os.path
import ctypes
import cv2

test_module = ctypes.CDLL(
    os.path.join(os.path.dirname(__file__), './test.so'))

test_module.BGR2HLS.argtypes = [
    ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int
]

if __name__ == '__main__':
    img_data = cv2.imread('../test_data/FixedUIDesign.jpg')
    h, w, d = img_data.shape
    img_data_out = np.array(img_data, dtype=np.uint8)
    test_module.BGR2HLS(img_data.ctypes.data, img_data_out.ctypes.data, h*w)

and get the following error (I was experiment with adding the RTLD_GLOBAL flag):

Traceback (most recent call last):
  File "c_tests.py", line 6, in <module>
    os.path.join(os.path.dirname(__file__), './test.so'), ctypes.RTLD_GLOBAL)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./test.so: undefined symbol: cvtColor

I see opencv_imgproc exists, and I can point to it when I compile without errors. I can see there is something similar to the symbol I want as well:

$ nm -D /usr/lib/libopencv_imgproc.so | grep cvtColor
00000000000b65a0 T _ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii

Is the odd name a symptop of the issue? What is wrong/missing with how I'm building/using the extension?

2

1 Answer 1

1

Switching to C++ solves the problem. Luckily I have the flexibility.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.