How to Convert OpenCV Image Data from Python to C

OpenCV officially provides both C++ and Python APIs for developers. Most of the time, developers just need to use one kind of programming languages to read, write and process images with hundreds of computer vision algorithms. However, if you want to use OpenCV Python APIs with an extended C/C++ library, it will be tricky to pass the data. In this article, I will share how to read camera stream with OpenCV-Python and detect barcode with Dynamsoft C/C++ Barcode SDK.

webcam barcode reader with OpenCV Python

Development Environment

Python Extension for Reading Barcode from OpenCV Image Data

Installation

Copy DynamsoftBarcodeReaderx86.dll and cv2.pyd to Python27\Lib\site-packages.

What is the type of the frame data that output by OpenCV?

To convert the image data, we have to know what type it is. The type is numpy.ndarray:

> rval, frame = vc.read();
> print type(frame)
> <type 'numpy.ndarray'>

How to get the C/C++ pointer that pointing to numpy.ndarray?

According to the OpenCV source file opencv\modules\python\src2\cv2.cv.hpp, we can use the following code to get the memory address of the data in C:

    PyObject *o;
    if (!PyArg_ParseTuple(args, "O", &o))
        return NULL;

    PyObject *ao = PyObject_GetAttrString(o, "__array_struct__");
    PyObject *retval;

    if ((ao == NULL) || !PyCObject_Check(ao)) {
        PyErr_SetString(PyExc_TypeError, "object does not have array interface");
        return NULL;
    }

    PyArrayInterface *pai = (PyArrayInterface*)PyCObject_AsVoidPtr(ao);
    if (pai->two != 2) {
        PyErr_SetString(PyExc_TypeError, "object does not have array interface");
        Py_DECREF(ao);
        return NULL;
    }

    // Construct data with header info and image data 
    char *buffer = (char*)pai->data; // The address of image data
    int width = pai->shape[1];       // image width
    int height = pai->shape[0];      // image height
    int size = pai->strides[0] * pai->shape[0]; // image size = stride * height

How to use the barcode detection API?

You can’t just pass the data pointer directly to DBR_DecodeBuffer(). The data needs to be reconstructed with some extra information:

    char *total = (char *)malloc(size + 40); // buffer size = image size + header size
    memset(total, 0, size + 40);
    BITMAPINFOHEADER bitmap_info = {40, width, height, 0, 24, 0, size, 0, 0, 0, 0};
    memcpy(total, &bitmap_info, 40);

    // Copy image data to buffer from bottom to top
    char *data = total + 40;
    int stride = pai->strides[0];
    for (int i = 1; i <= height; i++) {
        memcpy(data, buffer + stride * (height - i), stride);
        data += stride;
    }

Read barcode from images and return results:

// Dynamsoft Barcode Reader initialization
    __int64 llFormat = (OneD | QR_CODE | PDF417 | DATAMATRIX);
    int iMaxCount = 0x7FFFFFFF;
    ReaderOptions ro = {0};
    pBarcodeResultArray pResults = NULL;
    ro.llBarcodeFormat = llFormat;
    ro.iMaxBarcodesNumPerPage = iMaxCount;
    printf("width: %d, height: %d, size:%d\n", width, height, size);
    int iRet = DBR_DecodeBuffer((unsigned char *)total, size + 40, &ro, &pResults);
    printf("DBR_DecodeBuffer ret: %d\n", iRet);
    free(total); // Do not forget to release the constructed buffer 
    
    // Get results
    int count = pResults->iBarcodeCount;
    pBarcodeResult* ppBarcodes = pResults->ppBarcodes;
    pBarcodeResult tmp = NULL;
    retval = PyList_New(count); // The returned Python object
    PyObject* result = NULL;
    for (int i = 0; i < count; i++)
    {
        tmp = ppBarcodes[i];
        result = PyString_FromString(tmp->pBarcodeData);
        printf("result: %s\n", tmp->pBarcodeData);
        PyList_SetItem(retval, i, Py_BuildValue("iN", (int)tmp->llFormat, result)); // Add results to list
    }
    // release memory
    DBR_FreeBarcodeResults(&pResults);

What if you see the error ‘Unable to find vcvarsall.bat’ when building Python extension on Windows?

According to the answer from StackOverflow, execute the following command based on the version of Visual Studio installed:

  • Visual Studio 2010 (VS10): SET VS90COMNTOOLS=%VS100COMNTOOLS%
  • Visual Studio 2012 (VS11): SET VS90COMNTOOLS=%VS110COMNTOOLS%
  • Visual Studio 2013 (VS12): SET VS90COMNTOOLS=%VS120COMNTOOLS%
  • Visual Studio 2015 (VS14): SET VS90COMNTOOLS=%VS140COMNTOOLS%

I’m using Visual Studio 2015, and thus I can build Python extension as follows:

SET VS90COMNTOOLS=%VS140COMNTOOLS%
python setup.py build install

Python Script for Testing

Open camera:

import cv2
from dbr import *
import time

vc = cv2.VideoCapture(0)

Read and render camera video stream:

cv2.imshow(windowName, frame)
rval, frame = vc.read();

Detect barcode from frame and show results in console:

initLicense("<license>") # Invalid license is fine.
results = decodeBuffer(frame)
if (len(results) > 0):
    print "Total count: " + str(len(results))
    for result in results:
        print "Type: " + types[result[0]]
        print "Value: " + result[1] + "\n"

Source Code

https://github.com/yushulx/opencv-python-webcam-barcode-reader