未加星标

How to Convert OpenCV Image Data from Python to C

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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.


How to Convert OpenCV Image Data from Python to C
Development Environment Dynamsoft Barcode Reader 4.2 Python version: 2.7.0 OpenCV version: 2.4.10.Download cv2.pyd windows 10 USB webcam 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

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:How to Convert OpenCV Image Data from Python to C
本站链接:http://www.codesec.net/view/481258.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(39)