html to image in c,How to Convert OpenCV Image Data from Python to C

本文介绍如何使用OpenCV-Python读取摄像头流,并结合Dynamsoft C/C++ Barcode SDK进行条形码检测。首先,将Dynamsoft Barcode Reader的动态库和Python扩展复制到Python安装目录。接着,通过OpenCV获取帧数据的内存地址,然后构造包含图像信息的数据缓冲区,以便于C/C++调用解码API。最后,展示了如何处理解码结果并释放资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

a7ccd99caa36188ee4ca3106596a23ed.png

Development Environment

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)

>

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:

SETVS90COMNTOOLS=%VS140COMNTOOLS%pythonsetup.pybuildinstall

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("") # 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值