这部分是在c++中可以正常工作的部分,其声明如下:
extern "C" int LHD_Main(unsigned char *pImage, RECT *rcRoi,int w, int h, char *PlateCharacters, RECT *rectPlateLocation, int *PlateReliability, int nMod);
调用如下
//加载图片
IplImage *image = cvLoadImage(file_name,-1);
if (image == NULL)
{
printf("image is not exist!\n");
return -1;
}
//BGR数据
unsigned char *pRawData = (unsigned char *)malloc(image->width*image->height*image->nChannels);
if (NULL == pRawData)
{
printf("memalloc error\n");
return -1;
}
int pitch = image->nChannels*image->width;
int width = image->width;
int height = image->height;
//转换成3通道图像
for (int y = 0; y < image->height; y++)
{
for (int x = 0; x <image->width; x++)
{
*(pRawData + y*pitch + 3*x) = ((unsigned char *)(image->imageData + image->widthStep*y))[3*x];
*(pRawData + y*pitch + 3*x + 1) = ((unsigned char *)(image->imageData + image->widthStep*y))[3*x+1];
*(pRawData + y*pitch + 3*x + 2) = ((unsigned char *)(image->imageData + image->widthStep*y))[3*x+2];
}
}
char cFileName[128];
sprintf_s(cFileName,"%s","京");
cProvince[0] = cFileName[0];
cProvince[1] = cFileName[1];
LHD_Init(LPR_WIDTH, LPR_HEIGHT, cProvince);
//start license plate recognition module,宽高减少1是图像坐标从0开始
RECT recttROI = {0,0,image->width -1, image->height- 1};//设置区域
RECT rectPlate = {0,0,0,0};//输出定位结果
RECT rectTmp = {0,0,0,0};
int nblePlate = 0;//
char HPHM[128] = {'0'};//
char mHPHM[128] = {'0'};
int mnblePlate = 0;
bool m_bLPR = 0;
if(LHD_Main(pRawData,&recttROI,width,height, HPHM, &rectTmp,&nblePlate, i))
这些代码是可以正常工作的。
下面我在VB.NET中如此声明
<DllImport("LPR.dll", EntryPoint:="LHD_Main", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function LHD_Main(ByRef pImage As Byte, ByRef rcRoi As RECT, ByVal w As Int32, ByVal h As Int32, ByRef PlateCharacters As Byte, ByRef rectPlateLocation As RECT, ByRef PlateReliability As Int32, ByVal nMod As Integer) As Int32
End Function
Public Structure RECT
Dim left As Int32
Dim top As Int32
Dim right As Int32
Dim bottom As Int32
End Structure
调用如下
Dim cProvince(1) As byte
cProvince(0) = &HBE
cProvince(1) = &HA9
LHD_Init(768, 288, cProvince(0))
Dim path As String = "d:\pic\rtemp.jpg"
Dim rImage As IntPtr = Emgu.CV.CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR)
Dim mImage As Emgu.CV.Structure.MIplImage = CType(System.Runtime.InteropServices.Marshal.PtrToStructure(rImage, GetType(Emgu.CV.Structure.MIplImage)), Emgu.CV.Structure.MIplImage)
Dim RawData(mImage.width * mImage.height * mImage.nChannels)
Dim TempData(mImage.imageSize) As Byte
Marshal.Copy(mImage.imageData, TempData, 0, mImage.imageSize)
Dim p = mImage.nChannels * mImage.width
Dim w = mImage.width
Dim h = mImage.height
'转换成3通道图像
For y = 0 To h - 1
For x = 0 To w - 1
RawData(y * p + 3 * x) = TempData(3 * x + mImage.widthStep * y)
RawData(y * p + 3 * x + 1) = TempData(3 * x + mImage.widthStep * y + 1)
RawData(y * p + 3 * x + 2) = TempData(3 * x + mImage.widthStep * y + 2)
Next
Next
Dim recttROI As New RECT
recttROI.left = 0
recttROI.top = 0
recttROI.right = mImage.width - 1
recttROI.bottom = mImage.height - 1
Dim rectPlate As New RECT
Dim rectTmp As New RECT
Dim mnblePlate As Integer = 0
Dim HPHM(127) As Byte
Dim rw As Integer = mImage.width
Dim rh As Integer = mImage.height
For i = 0 To 8
Dim result As Integer = LHD_Main(RawData(0), recttROI, rw, rh, HPHM(0), rectTmp, mnblePlate, i)
运行后在LHD_Main处报错: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
求问怎么解决!